原文地址:http://blog.sina.com.cn/s/blog_5e35f1db0100e4h3.html
向量函数在物理模拟,人工智能,图形转换等方面都要用到。通过不同算法,三维空间向量可以表示为一方向,也可表示为位置,使用很灵活,所以将在学习过程中的大部分算法与以简单介绍以利游戏开发。
D3DXVec3Add
原型:D3DXVec3Add( VOut As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR)
作用:将两个3-D向量相加,VOut为结果,V1,V2为原向量。
代码计算过程:
Dim VOut As D3DVECTOR
VOut.x = V1.x + V2.x
VOut.y = V1.y + V2.y
VOut.z = V1.z + V2.z
D3DXVec3Subtract
D3DXVec3Subtract( VOut As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR)
作用:将V1向量相减V2向量
VOut.x = V1.x - V2.x
VOut.y = V1.y - V2.y
VOut.z = V1.z - V2.z
D3DXVec3Cross
原型:D3DXVec3Cross( VOut As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR)
作用:计算两个3-D向量的叉积,叉积结果VOut垂直于原向量V1和V2
VOut.x = V1.y * V2.z - V1.z * V2.y
VOut.y = V1.z * V2.x - V1.x * V2.z
VOut.z = V1.x * V2.y - V1.y * V2.x
D3DXVec3Dot
原型:D3DXVec3Dot( V1 As D3DVECTOR, V2 As D3DVECTOR) As Single
作用:计算两个3-D向量的点积,结果VOut=|V1||V2|COS(theta).|V1|,|V2|为两个向量的长度,theta为它们之间的夹角。注意:结果为一单精度浮点数。
D3DXVec3Dot=V1.x * V2.x + V1.y * V2.y + V1.z * V2.z
D3DXVec3Maximize
原型:D3DXVec3Maximize( VOut As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR)
作用:返回两个3-D向量的大的一方(每个组件中)
If V1.x > V2.x Then VOut.x = V1.x Else VOut.x = V2.x
If V1.y > V2.y Then VOut.y = V1.y Else VOut.y = V2.y
If V1.z > V2.z Then VOut.z = V1.z Else VOut.z = V2.z
D3DXVec3Minimize
原型:D3DXVec3Minimize( VOut As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR)
作用:与D3DXVec3Maximize类似,只是返回小的一方
D3DXVec3Scale
原型:D3DXVec3Scale( VOut As D3DVECTOR2, V1 As D3DVECTOR2, s As Single)
作用:缩放一个3-D向量,s为缩放比例
VOut.x = s * V1.x
VOut.y = s * V1.y
VOut.z = s * V1.z
D3DXVec3Length
原型:D3DXVec3Length( v As D3DVECTOR) As Single
作用:返回向量的长度
D3DXVec3Length = Sqr((V.x * V.x) + (V.y * V.y) + (V.z * V.z))
D3DXVec3LengthSq
原型:D3DXVec3LengthSq( v As D3DVECTOR) As Single
作用:返回向量的长度的平方。因为开方的计算比较耗时,所以才有这个求平方的函数
D3DXVec3LengthSq = (V.x * V.x) + (V.y * V.y) + (V.z * V.z)
D3DXVec3Lerp
原型:D3DXVec3Lerp( VOut As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR, s As Single)
作用:完成两个3-D向量的线性插值
V1 + s(V2-V1).
D3DXVec3CatmullRom
原型:D3DXVec3CatmullRom( VOut As D3DVECTOR, V0 As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR, V3 As D3DVECTOR, s As Single)
作用:使用4个指定的3-D向量,返回Catmull-Rom插值.当s趋向于0时,插值接近V1,当s趋向于1时,插值接近V2.
D3DXVec3Hermite
原型:D3DXVec3Hermite( VOut As D3DVECTOR, V1 As D3DVECTOR, T1 As D3DVECTOR, V2 As D3DVECTOR, T2 As D3DVECTOR, s As Single)
作用:这个函数从(位置V1,切线A)到(位置V1,切线A)用Hermite样条线内插.Hermite样条线对控制动画很有用,因为位置及切线已明确,所以你可以很方便匹配每段曲线的起点与终点创建C2连续的曲线,
D3DXVec3BaryCentric
原型:D3DXVec3BaryCentric( VOut As D3DVECTOR, V1 As D3DVECTOR, V2 As D3DVECTOR, V3 As D3DVECTOR, f As Single, g As Single)
作用:返回重心坐标点,使用3个指定的向量.f ,g为权重值,参数f 控制 V2 获得多少权重值到结果中,参数g 控制 V3 获得多少权重值到结果中.最后 1-f-g 控制 V1 获得多少权重值到结果中.使用重心坐标可以简单的表明坐标的改变.
看下面关系:
If (f>=0 And g>=0 And 1-f-g>=0), 点在三角形V1V2V3中.
If (f==0 And g>=0 And 1-f-g>=0), 点在线V1V3上.
If (f>=0 And g==0 And 1-f-g>=0), 点在线V1V2上.
If (f>=0 And g>=0 And 1-f-g==0), 点在线V2V3上.
VOut =V1 + f(V2-V1) + g(V3-V1) = (1-f-g)V1 + fV2 + gV3
D3DXVec3TransformCoord
D3DXVec3TransformCoord( VOut As D3DVECTOR, V1 As D3DVECTOR, M As D3DMATRIX)
转换一个向量V1(x, y, z, 1)通过指定的矩阵,投影 w = 1.
例子如图:先创建一旋转平移组合的矩阵,当向量通过该矩阵后就完成了旋转及平移变换.
VOut.x = V1.x * M.11 + V1.y * M.21 + V1.z * M.31 + M.41
VOut.y = V1.x * M.12 + V1.y * M.22 + V1.z * M.32 + M.42
VOut.z = V1.x * M.13 + V1.y * M.23 + V1.z * M.33 + M.43
D3DXVec3TransformNormal
D3DXVec3TransformNormal( VOut As D3DVECTOR, V1 As D3DVECTOR, M As D3DMATRIX)
转换一个向量(x, y, z, 1)通过指定的矩阵,投影 w = 0.可以看到因为w = 0,所以矩阵M.41,M.42,M.43不进行计算
VOut.x = V1.x * M.11 + V1.y * M.21 + V1.z * M.31
VOut.y = V1.x * M.12 + V1.y * M.22 + V1.z * M.32
VOut.z = V1.x * M.13 + V1.y * M.23 + V1.z * M.33
D3DXVec3Transform
D3DXVec3Transform( VOut As D3DVECTOR4, V1 As D3DVECTOR, M As D3DMATRIX)
转换一个向量V1(x, y, z, 1)通过指定的矩阵,投影 w = 1,这个函数与D3DXVec3TransformCoord类似,但返回的是4-D向量
VOut.x = V1.x * M.11 + V1.y * M.21 + V1.z * M.31 + M.41
VOut.y = V1.x * M.12 + V1.y * M.22 + V1.z * M.32 + M.42
VOut.z = V1.x * M.13 + V1.y * M.23 + V1.z * M.33 + M.43
VOut.w = V1.x * M.14 + V1.y * M.24 + V1.z * M.34 + M.44
希望大家多提提意见,TKS
,QQ:774328369