DX 提供了D3DXMatrixPerspectiveLH,D3DXMatrixPerspectiveOffCenterLH以及D3DXMatrixPerspectiveFovLH来实现透视投影。提供了D3DXMatrixOrthoLH和D3DXMatrixOrthoOffCenterLH来实现正投影
(以上各函数都有右手坐标系下面的版本,只是把后面的LH改成RH)
先看D3DXMatrixPerspectiveLH
Builds a left-handed perspective projection matrix
D3DXMATRIX * D3DXMatrixPerspectiveLH( D3DXMATRIX * pOut, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf );
Pointer to a D3DXMATRIX structure that is a left-handed perspective projection matrix.
这个函数的投影平面在z=n处
最后看下MSDN的结果
OK,为了完整把D3DXMatrixPerspectiveRH也推导下吧.囧....
Builds a right-handed perspective projection matrix.
D3DXMATRIX * D3DXMatrixPerspectiveRH( D3DXMATRIX * pOut, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf );
Pointer to a D3DXMATRIX structure that is a right-handed perspective projection matrix.
这个函数的投影平面在z=-n处
最后看下MSDN的结果
再看下D3DXMatrixPerspectiveOffCenterLH
Builds a customized, left-handed perspective projection matrix.
D3DXMATRIX * D3DXMatrixPerspectiveOffCenterLH( D3DXMATRIX *pOut, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf );
Pointer to a D3DXMATRIX structure that is a customized, left-handed perspective projection matrix.
最后看下MSDN
接着看下D3DXMatrixPerspectiveOffCenterRH
Builds a customized, right-handed perspective projection matrix.
D3DXMATRIX * D3DXMatrixPerspectiveOffCenterRH( D3DXMATRIX *pOut, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf );
Pointer to a D3DXMATRIX structure that is a customized, right-handed perspective projection matrix.
看下MSDN
继续看D3DXMatrixPerspectiveFovLH
Builds a left-handed perspective projection matrix based on a field of view.
D3DXMATRIX * D3DXMatrixPerspectiveFovLH( D3DXMATRIX *pOut, FLOAT fovy, FLOAT Aspect, FLOAT zn, FLOAT zf );
Pointer to a D3DXMATRIX structure that is a left-handed perspective projection matrix.
看MSDN
继续看D3DXMatrixPerspectiveFovRH
Builds a right-handed perspective projection matrix based on a field of view.
D3DXMATRIX * D3DXMatrixPerspectiveFovRH( D3DXMATRIX *pOut, FLOAT fovy, FLOAT Aspect, FLOAT zn, FLOAT zf );
Pointer to a D3DXMATRIX structure that is a right-handed perspective projection matrix.
看结果
接着看几个正投影
先看D3DXMatrixOrthoLH
Builds a left-handed orthographic projection matrix.
D3DXMATRIX * D3DXMatrixOrthoLH( D3DXMATRIX *pOut, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf );
Pointer to the resulting D3DXMATRIX.
接着看D3DXMatrixOrthoRH
Builds a right-handed orthographic projection matrix.
D3DXMATRIX * D3DXMatrixOrthoRH( D3DXMATRIX *pOut, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf );
Pointer to the resulting D3DXMATRIX.
看结果
接着看D3DXMatrixOrthoOffCenterLH
Builds a customized, left-handed orthographic projection matrix.
D3DXMATRIX * D3DXMatrixOrthoOffCenterLH( D3DXMATRIX *pOut, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf );
Pointer to the resulting D3DXMATRIX.
看结果
终于到最后一个了D3DXMatrixOrthoOffCenterRH
Builds a customized, right-handed orthographic projection matrix.
D3DXMATRIX * D3DXMatrixOrthoOffCenterRH( D3DXMATRIX *pOut, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf );
Pointer to the resulting D3DXMATRIX.
看结果
尼玛,终于搞完了,本来还有世界坐标到视图坐标的转换,标准设备坐标到视口的转换..今晚先到这里,伤不起啊,6点了,以后补上
继续补上,先看世界坐标到视图坐标的转换
先看D3DXMatrixLookAtLH
Builds a left-handed, look-at matrix.
D3DXMATRIX * D3DXMatrixLookAtLH( D3DXMATRIX *pOut, CONST D3DXVECTOR3 *pEye, CONST D3DXVECTOR3 *pAt, CONST D3DXVECTOR3 *pUp );
Pointer to a D3DXMATRIX structure that is a left-handed, look-at matrix.
看结果
另外D3DXMatrixLookAtRH推导是一样的,只不过把Z''取负即可
接着看标准设备坐标到视口坐标的转换,DX提供了SetViewport方法来设置视口矩阵
Sets the viewport parameters for the device.
HRESULT SetViewport( CONST D3DVIEWPORT9 * pViewport );
If the method succeeds, the return value is D3D_OK. If the method fails, it will return D3DERR_INVALIDCALL. This will happen if pViewport is invalid, or if pViewport describes a region that cannot exist within the render target surface.
D3DVIEWPORT9结构体如下:
typedef struct D3DVIEWPORT9 { DWORD X; DWORD Y; DWORD Width; DWORD Height; float MinZ; float MaxZ; } D3DVIEWPORT9, *LPD3DVIEWPORT9;
所以,视口变换
就是将
x''->x''':(-1,1)->(X,X+Width)
y''->y''':(-1,1)->(Y+Height,Y)//这里注意DX中视口坐标中的y值是从上到下递增的
z''->z''':(0,1)->(MinZ,MaxZ)
由于(x'',y'',z'')满足线性插值,所以设
x'''=A'x''+B'
y'''=A''y''+B''
z'''=A'''z''+B'''
代入变换条件可求得:
A'=Width/2 B'=Width/2+X
A''=-Height/2 B''=Height/2+Y
A'''=MaxZ-MinZ B'''=MinZ
所以视口矩阵为
也就是龙书上所说的
OK,渲染管线里面用到的变换基本上讲完了,当然还有神马旋转变换矩阵,镜像映射矩阵,平面投影矩阵到时候我会再开专题讲敬请期待哦!