DxVBLibA.D3DXMATH_MATRIX的矩阵为一4×4的Single对象类型,排列如下
m11 m12 m13 m14
m21 m22 m23 m24
m31 m32 m33 m34
m41 m42 m43 m44
m11 m12 m13: 映射X轴
m21 m22 m23: 映射Y轴
m31 m32 m33: 映射Z轴
前三行列也可认为旋转相关
m41: X轴平移
m42: Y轴平移
m43: Z轴平移
第四列可认为与投影相关
D3DXMatrixTranslation(MOut As D3DMATRIX, x As Single, y As Single, z As Single) As Long
创建一个进行平移的矩阵
D3DXMatrixScaling(MOut As D3DMATRIX, x As Single, y As Single, z As Single) As Long
创建一个缩放矩阵
D3DXMatrixMultiply(MOut As D3DMATRIX, M1 As D3DMATRIX, M2 As D3DMATRIX) As Long
计算两个矩阵乘积 ,Mout为输出矩阵 ,如先旋转R在缩放S 则矩阵为 R*S
D3DXMatrixRotationX(MOut As D3DMATRIX, angle As Single) As Long
1 0 0 0
0 cos sin 0
0 -sin cos 0
0 0 0 1
产生了一个绕X轴旋转的转换矩阵,旋转符合左手螺旋定则
D3DXMatrixRotationY(MOut As D3DMATRIX, angle As Single) As Long
y 轴旋转
cos 0 -sin 0
0 1 0 0
sin 0 cos 0
0 0 0 1
D3DXMatrixRotationZ(MOut As D3DMATRIX, angle As Single) As Long
z 轴旋转
cos sin 0 0
-sin cos 0 0
0 0 1 0
0 0 0 1
D3DXMatrixRotationAxis(MOut As D3DMATRIX, VAxis As D3DVECTOR, angle As Single) As Long
创建一个绕一个轴旋转的矩阵,轴不需单位化,旋转符合左手螺旋定则
D3DXMatrixLookAtLH(MOut As D3DMATRIX, VEye As D3DVECTOR, VAt As D3DVECTOR, VUp As D3DVECTOR) As Long
zaxis = normal(At - Eye)
xaxis = normal(cross(Up, zaxis))
yaxis = cross(zaxis, xaxis)
xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
-dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1
建立左手观察矩阵的函数
D3DXMatrixLookAtRH(MOut As D3DMATRIX, VEye As D3DVECTOR, VAt As D3DVECTOR, VUp As D3DVECTOR) As Long
zaxis = normal(Eye - At)
xaxis = normal(cross(Up, zaxis))
yaxis = cross(zaxis, xaxis)
xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
-dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1
建立右手观察矩阵的函数
D3DXMatrixPerspectiveFovLH(MOut As D3DMATRIX, fovy As Single, aspect As Single, zn As Single, zf As Single) As Long
h = cos(fov/2) / sin(fov/2)
w = h / aspect
2*zn/w 0 0 0
0 2*zn/h 0 0
0 0 zf/(zf-zn) 1
0 0 zn*zf/(zn-zf) 0
定义左手透视投影矩阵,参数为纵横张角和远近平面,透视受z值影响,物体越远成像越小,右手系相仿。
pOut:返回的投影矩阵指针
fovY:定义镜头垂直观察范围,以弧度为单位。对于这个参数,理解:如果定义为PI/2 (90度角),那么就是表示以摄像机的观察方向为平分线,上方45度角和下方45度角就是摄像机所能看到的垂直范围了。嗯,可以想象一下自己的眼睛,如果可以把自己眼睛的fovY值设为PI(180度角),那么我们就可以不用抬头就看得见头顶的东西了。
Aspect:设置纵横比。如果定义为1,那么所看到的物体大小不变。如果定义为其它值,你所看到的物体就会变形。不过一般情况下这个值设为显示器屏幕的长宽比。(终于明白为什么有些人会说电视上的自己看起来会比较胖了……)
zn:设置摄像机所能观察到的最远距离
zf:设置摄像机所能观察到的最近距离
D3DXMatrixPerspectiveOffCenterLH(MOut As D3DMATRIX, l As Single, r As Single, b As Single, t As Single, zn As Single, zf As Single) As Long
2*zn/(r-l) 0 0 0
0 2*zn/(t-b) 0 0
(l+r)/(l-r) (t+b)/(b-t) zf/(zf-zn) 1
0 0 zn*zf/(zn-zf) 0
定义2d坐标系透视投影矩阵
l: x最小值
r: x最大值.
b: y最大值
t: y最小值
D3DXMatrixOrthoLH(MOut As D3DMATRIX, w As Single, h As Single, zn As Single, zf As Single) As Long
创建一个以屏幕中心为坐标原点的左手系正交投影矩阵,正交投影不会随物体远近影响物体投影的大小,右手系相仿。
2/w 0 0 0
0 2/h 0 0
0 0 1/(zf-zn) 0
0 0 -zn/(zf-zn) 1
w :宽度
h :高度
D3DXMatrixOrthoOffCenterLH(MOut As D3DMATRIX, l As Single, r As Single, b As Single, t As Single, zn As Single, zf As Single) As Long
创建一个自定义2d坐标系的正交投影矩阵。
2/(r-l) 0 0 0
0 2/(t-b) 0 0
0 0 1/(zf-zn) 0
(l+r)/(l-r) (t+b)/(b-t) zn/(zn-zf) 1
如创建左上角为坐标(0,0) 右下角为(width,height)的投影矩阵:
D3DXMatrixOrthoOffCenterLH(m_matProj,0,Width,m_iHeight,0,0,0)
D3DXMatrixIdentity(MOut As D3DMATRIX) As Long
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
创建一个单位化矩阵
D3DXMatrixIsIdentity(M As D3DMATRIX) As Long
判断一个矩阵是否是单位阵 ,是这个函数返回1,否则返回0
D3DXMatrixInverse(MOut As D3DMATRIX, Determinant As Single, M As D3DMATRIX) As Long
计算矩阵的逆矩阵
D3DXMatrixShadow(MOut As D3DMATRIX, VLight As D3DVECTOR4, plane As D3DPLANE) As Long
P = normalize(Plane);
L = Light;
d = dot(P, L)
P.a * L.x + d P.a * L.y P.a * L.z P.a * L.w
P.b * L.x P.b * L.y + d P.b * L.z P.b * L.w
P.c * L.x P.c * L.y P.c * L.z + d P.c * L.w
P.d * L.x P.d * L.y P.d * L.z P.d * L.w + d
创建一个阴影 投影的平面矩阵
D3DXMatrixReflect(MOut As D3DMATRIX, plane As D3DPLANE) As Long
P = normalize(Plane);
-2 * P.a * P.a + 1 -2 * P.b * P.a -2 * P.c * P.a 0
-2 * P.a * P.b -2 * P.b * P.b + 1 -2 * P.c * P.b 0
-2 * P.a * P.c -2 * P.b * P.c -2 * P.c * P.c + 1 0
-2 * P.a * P.d -2 * P.b * P.d -2 * P.c * P.d 1从平面方程创建一个平面反射矩阵。
如果做一个物体沿一个平面做镜像,可以用此函数;
D3DXMatrixTranspose(MOut As D3DMATRIX, M As D3DMATRIX) As Long
对矩阵进行转置
D3DXMatrixfDeterminant(M As D3DMATRIX) As Single
计算矩阵行列式值
D3DXMatrixRotationQuaternion(MOut As D3DMATRIX, Q As D3DQUATERNION) As Long
从一个四元数创建矩阵
Function D3DXMatrixRotationYawPitchRoll(MOut As D3DMATRIX, yaw As Single, Pitch As Single, roll As Single) As Long
从欧拉角创建的一个矩阵,注意旋转的次序是先roll,再pitch,即先绕Z轴转,再绕X轴旋转,再绕Z轴旋转
D3DXMatrixTransformation(MOut As D3DMATRIX, VScaleCenter As D3DVECTOR, QScaleRotation As D3DQUATERNION, VScale As D3DVECTOR, VRotationCenter As D3DVECTOR, QRotation As D3DQUATERNION, VTranslation As D3DVECTOR) As Long
创建一个变换矩阵。如果参数设置为NULL,就当作单位矩阵处理。
参数:
pOut [out] 指向D3DXMATRIX 结构的操作结果矩阵。
pScalingCenter [in] 指向D3DXVECTOR3 结构的缩放中心点向量。
pScalingRotation [in] 指向D3DXQUATERNION 结构的缩放和旋转的四元组。
pScaling [in] 指向D3DXVECTOR3 结构的缩放向量。
pRotationCenter [in] 指向D3DXVECTOR3 结构的旋转中心向量。
pRotation [in] 指向D3DXQUATERNION 结构的旋转的四元组。
pTranslation [in] 指向D3DXVECTOR3 结构的平移向量。
说明:本函数用下面的公式计算变换矩阵:
Mout = (Msc)-1 * (Msr)-1 * Ms * Msr * Msc * (Mrc)-1 * Mr * Mrc * Mt
其中:
Mout = 输出矩阵 (pOut)
Msc = 缩放中心矩阵 (pScalingCenter)
Msr = 缩放旋转矩阵 (pScalingRotation)
Ms = 缩放矩阵 (pScaling)
Mrc = 旋转中心矩阵 (pRotationCenter)
Mr = 旋转矩阵 (pRotation)
Mt = 平移矩阵 (pTranslation)
可以让函数D3DXMatrixTransformation作为其它函数的参数使用。