各种变换的原理----DX版本

DX 提供了D3DXMatrixPerspectiveLH,D3DXMatrixPerspectiveOffCenterLH以及D3DXMatrixPerspectiveFovLH来实现透视投影。提供了D3DXMatrixOrthoLH和D3DXMatrixOrthoOffCenterLH来实现正投影


(以上各函数都有右手坐标系下面的版本,只是把后面的LH改成RH)


先看D3DXMatrixPerspectiveLH

D3DXMatrixPerspectiveLH

Builds a left-handed perspective projection matrix

D3DXMATRIX * D3DXMatrixPerspectiveLH(
  D3DXMATRIX * pOut,
  FLOAT w,
  FLOAT h,
  FLOAT zn,
  FLOAT zf
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
w
[in] Width of the view volume at the near view-plane.
h
[in] Height of the view volume at the near view-plane.
zn
[in] Z-value of the near view-plane.
zf
[in] Z-value of the far view-plane.

Return Values

Pointer to a D3DXMATRIX structure that is a left-handed perspective projection matrix.

这个函数的投影平面在z=n处

各种变换的原理----DX版本_第1张图片

最后看下MSDN的结果

各种变换的原理----DX版本_第2张图片


OK,为了完整把D3DXMatrixPerspectiveRH也推导下吧.囧....

D3DXMatrixPerspectiveRH

Builds a right-handed perspective projection matrix.

D3DXMATRIX * D3DXMatrixPerspectiveRH(
  D3DXMATRIX * pOut,
  FLOAT w,
  FLOAT h,
  FLOAT zn,
  FLOAT zf
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
w
[in] Width of the view volume at the near view-plane.
h
[in] Height of the view volume at the near view-plane.
zn
[in] Z-value of the near view-plane.
zf
[in] Z-value of the far view-plane.

Return Values

Pointer to a D3DXMATRIX structure that is a right-handed perspective projection matrix.



这个函数的投影平面在z=-n处

各种变换的原理----DX版本_第3张图片

最后看下MSDN的结果

各种变换的原理----DX版本_第4张图片

再看下D3DXMatrixPerspectiveOffCenterLH

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
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
l
[in] Minimum x-value of the view volume.
r
[in] Maximum x-value of the view volume.
b
[in] Minimum y-value of the view volume.
t
[in] Maximum y-value of the view volume.
zn
[in] Minimum z-value of the view volume.
zf
[in] Maximum z-value of the view volume.

Return Values

Pointer to a D3DXMATRIX structure that is a customized, left-handed perspective projection matrix.


各种变换的原理----DX版本_第5张图片

最后看下MSDN

各种变换的原理----DX版本_第6张图片


接着看下D3DXMatrixPerspectiveOffCenterRH

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
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
l
[in] Minimum x-value of the view volume.
r
[in] Maximum x-value of the view volume.
b
[in] Minimum y-value of the view volume.
t
[in] Maximum y-value of the view volume.
zn
[in] Minimum z-value of the view volume.
zf
[in] Maximum z-value of the view volume.

Return Values

Pointer to a D3DXMATRIX structure that is a customized, right-handed perspective projection matrix.


各种变换的原理----DX版本_第7张图片

看下MSDN

各种变换的原理----DX版本_第8张图片

继续看D3DXMatrixPerspectiveFovLH

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
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
fovy
[in] Field of view in the y direction, in radians.
Aspect
[in] Aspect ratio, defined as view space width divided by height.
zn
[in] Z-value of the near view-plane.
zf
[in] Z-value of the far view-plane.

Return Values

Pointer to a D3DXMATRIX structure that is a left-handed perspective projection matrix.


各种变换的原理----DX版本_第9张图片

看MSDN

各种变换的原理----DX版本_第10张图片

继续看D3DXMatrixPerspectiveFovRH

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
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
fovy
[in] Field of view in the y direction, in radians.
Aspect
[in] Aspect ratio, defined as view space width divided by height.
zn
[in] Z-value of the near view-plane.
zf
[in] Z-value of the far view-plane.

Return Values

Pointer to a D3DXMATRIX structure that is a right-handed perspective projection matrix.

各种变换的原理----DX版本_第11张图片

看结果

各种变换的原理----DX版本_第12张图片

接着看几个正投影

先看D3DXMatrixOrthoLH

D3DXMatrixOrthoLH

Builds a left-handed orthographic projection matrix.

D3DXMATRIX * D3DXMatrixOrthoLH(
  D3DXMATRIX *pOut,
  FLOAT w,
  FLOAT h,
  FLOAT zn,
  FLOAT zf
);

Parameters

pOut
[in, out] Pointer to the resulting D3DXMATRIX.
w
[in] Width of the view volume.
h
[in] Height of the view volume.
zn
[in] Minimum z-value of the view volume which is referred to as z-near.
zf
[in] Maximum z-value of the view volume which is referred to as z-far.

Return Values

Pointer to the resulting D3DXMATRIX.

各种变换的原理----DX版本_第13张图片


看结果 

各种变换的原理----DX版本_第14张图片

接着看D3DXMatrixOrthoRH

D3DXMatrixOrthoRH

Builds a right-handed orthographic projection matrix.

D3DXMATRIX * D3DXMatrixOrthoRH(
  D3DXMATRIX *pOut,
  FLOAT w,
  FLOAT h,
  FLOAT zn,
  FLOAT zf
);

Parameters

pOut
[in, out] Pointer to the resulting D3DXMATRIX.
w
[in] Width of the view volume.
h
[in] Height of the view volume.
zn
[in] Minimum z-value of the view volume.
zf
[in] Maximum z-value of the view volume.

Return Values

Pointer to the resulting D3DXMATRIX.

 
 各种变换的原理----DX版本_第15张图片 
 

看结果 

各种变换的原理----DX版本_第16张图片

接着看D3DXMatrixOrthoOffCenterLH

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
);

Parameters

pOut
[in, out] Pointer to the resulting D3DXMATRIX.
l
[in] Minimum x-value of view volume.
r
[in] Maximum x-value of view volume.
b
[in] Minimum y-value of view volume.
t
[in] Maximum y-value of view volume.
zn
[in] Minimum z-value of the view volume.
zf
[in] Maximum z-value of the view volume.

Return Values

Pointer to the resulting D3DXMATRIX.

各种变换的原理----DX版本_第17张图片

看结果

各种变换的原理----DX版本_第18张图片

终于到最后一个了D3DXMatrixOrthoOffCenterRH

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
);

Parameters

pOut
[in, out] Pointer to the resulting D3DXMATRIX.
l
[in] Minimum x-value of view volume.
r
[in] Maximum x-value of view volume.
b
[in] Minimum y-value of view volume.
t
[in] Maximum y-value of view volume.
zn
[in] Minimum z-value of the view volume.
zf
[in] Maximum z-value of the view volume.

Return Values

Pointer to the resulting D3DXMATRIX.

各种变换的原理----DX版本_第19张图片

看结果

各种变换的原理----DX版本_第20张图片


尼玛,终于搞完了,本来还有世界坐标到视图坐标的转换,标准设备坐标到视口的转换..今晚先到这里,伤不起啊,6点了,以后补上


继续补上,先看世界坐标到视图坐标的转换

先看D3DXMatrixLookAtLH

D3DXMatrixLookAtLH

Builds a left-handed, look-at matrix.

D3DXMATRIX * D3DXMatrixLookAtLH(
  D3DXMATRIX *pOut,
  CONST D3DXVECTOR3 *pEye,
  CONST D3DXVECTOR3 *pAt,
  CONST D3DXVECTOR3 *pUp
);

Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that is the result of the operation.
pEye
[in] Pointer to the D3DXVECTOR3 that defines the eye point. This value is used in translation.
pAt
[in] Pointer to the D3DXVECTOR3 structure that defines the camera look-at target.
pUp
[in] Pointer to the D3DXVECTOR3 structure that defines the current world's up, usually [0, 1, 0].

Return Values

Pointer to a D3DXMATRIX structure that is a left-handed, look-at matrix.

各种变换的原理----DX版本_第21张图片

各种变换的原理----DX版本_第22张图片

看结果

各种变换的原理----DX版本_第23张图片

另外D3DXMatrixLookAtRH推导是一样的,只不过把Z''取负即可

接着看标准设备坐标到视口坐标的转换,DX提供了SetViewport方法来设置视口矩阵

IDirect3DDevice9::SetViewport

Sets the viewport parameters for the device.

HRESULT SetViewport(
  CONST D3DVIEWPORT9 * pViewport
);

Parameters

pViewport
[in] Pointer to a D3DVIEWPORT9 structure, specifying the viewport parameters to set.

Return Values

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

所以视口矩阵为

各种变换的原理----DX版本_第24张图片


也就是龙书上所说的


各种变换的原理----DX版本_第25张图片


OK,渲染管线里面用到的变换基本上讲完了,当然还有神马旋转变换矩阵,镜像映射矩阵,平面投影矩阵到时候我会再开专题讲微笑敬请期待哦!


你可能感兴趣的:(各种变换的原理----DX版本)