The float4 structure
encapsulates 4 float values and can
be used to represent a 4-element vector or a row of a 4-column matrix:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
structfloat4{ float4()
{}; float4(floats)
: x(s), y(s), z(s), w(s) {} float4(floatx,floaty,floatz,floatw)
: x(x), y(y), z(z), w(w) {} floatx,
y, z, w; inlinefloat4
operator*(floats)const{returnfloat4(x*s,
y*s, z*s, w*s); } inlinefloat4
operator+(constfloat4&
a) const{returnfloat4(x+a.x,
y+a.y, z+a.z, w+a.w); }};//
dot product of two float4 vectorsinlinefloatdot(constfloat4&
a, constfloat4&
b) { returna.x
* b.x + a.y * b.y + a.z * b.z + a.w * b.w;} |
Similar
to the float4 datatype, we define another
common type: float3. It can be used,
for example, for representing 3D vectors.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
structfloat3{ float3()
{}; float3(floats)
: x(s), y(s), z(s) {} float3(floatx,floaty,floatz)
: x(x), y(y), z(z) {} floatx,
y, z; inlinefloat3
operator*(floats)const{returnfloat3(x*s,
y*s, z*s); } inlinefloat3
operator+(constfloat3&
a) const{returnfloat3(x+a.x,
y+a.y, z+a.z); }};//
dot product of two float3 vectorsinlinefloatdot(constfloat3&
a, constfloat3&
b) { returna.x
* b.x + a.y * b.y + a.z * b.z;} |
The float2 datatype
contains 2 float elements:
|
1
2
3
4
5
6
7
|
structfloat3{ float2()
{}; float2(floats)
: x(s), y(s) {} float2(floatx,floaty)
: x(x), y(y) {} floatx,
y;}; |
本文介绍了浮点数向量结构float2、float3及float4的定义与使用方法,包括初始化、运算符重载及点乘运算实现,并探讨了这些结构在3D图形和通用计算中的应用。


被折叠的 条评论
为什么被折叠?



