空間中的物體需要使用三維坐標來描述,而顯示器是一個二維的表面,所以在屏幕上渲染一個三維場景時,首先需要將描述空間物體的三維坐標變換為二維坐標(世界坐標到屏幕坐標),這在Direct3D中稱為頂點坐標變換。頂點坐標變換通常通過矩陣來完成。可以把頂點坐標變換想象成攝像過程,三維世界的景物通過攝像機的拍攝顯示在二維的相片上,所不同的是把相片換成了屏幕。
頂點坐標變換和光照流水線概述
Direct3D中渲染三維對象的過程可分為兩個階段。第一階段稱為坐標變換和光照(Transforming
and Lighting T&L)階段。在這個階段,每個對象的頂點被從一個抽象的、浮點坐標空間轉換到基于像素的屏幕空間(坐標變換不僅包含物體頂點位置的坐標變換,它還可能包含頂點法線、紋理坐標等的坐標變換),并根據場景中光源和物體表面的材質對物體頂點應用不同類型的光照效果。還有其他一些比較重要的任務,如裁剪和視口縮放也在第一階段進行。第二階段稱為光柵化處理階段,Direct3D將經過T&L處理的頂點組織以點、線、面為基礎的圖元,應用紋理貼圖和物體頂點的顏色屬性,根據相應渲染狀態設置(如著色模式),決定每個像素的最終顏色值,并在屏幕上顯示出來。
有時根據特殊的需要,可以跳過其中的某些步驟。如果愿意,也可以提供自己的坐標變換和光照過程,并將處理后的頂點直接傳給Direct3D光柵化處理程序,而繞過Direct3D的T&L階段。
T&L的過程在Direct3D中通常稱為頂點變換流水線,在這個過程中,未經過變換和光照的頂點從一端進入,在內部這些頂點將完成幾個連續操作,然后經過轉換和光照的頂點從另一端出來。應用程序通過指定幾個矩陣、視口以及所使用的光線來建立T&L流水線,然后應用程序將頂點送入流水線,對這些頂點進行變換、照明和裁剪,將其投影到屏幕空間,并根據視口的規定對其進行縮放。我們認為經過流水線的頂點是已經經過處理的,并且已經準備好傳送給光柵化處理程序。
下面首先介紹T&L流水線涉及到的一些基本概念:
(1)世界變換和世界坐標系:物體在三維空間的運動和變形過程稱為世界變換,如平移、旋轉、縮放等。物體在其中運動的三維空間稱為世界空間,它的三維坐標系表示稱為世界坐標系,物體頂點在世界坐標系里的坐標變換稱為世界變換。
(2)取景變換和觀察坐標系:把圖形顯示想象成攝像過程,取景變換就像攝像機中攝像機的擺放一樣,在三維圖形顯示中,需要設置一個虛擬攝像機,屏幕顯示的圖形就是虛擬攝像機拍攝在膠片上的景物。以攝像機位置為參考原點,攝像機觀察的方向為坐標軸,建立的坐標系稱為觀察坐標系,物體在觀察坐標系中的相對坐標稱為觀察坐標,頂點從世界坐標到觀察坐標的轉換稱為取景變換。
(3)投影坐標和投影坐標系:物體從世界坐標描述轉換到觀察坐標后,可將三維物體投影到二維表面上,即投影到虛擬攝像機的膠片上,這個過程就是投影變換。以膠片中心為參考原點的空間坐標系稱為投影坐標系,物體在投影坐標系中的坐標稱為投影坐標。
(4)視區變換和屏幕坐標系:物體在投影坐標系中的表示為浮點坐標,通過定義屏幕顯示區域(一般為顯示窗口大小),將浮點坐標轉化為像素坐標的過程稱為視區變換,該像素坐標值稱為屏幕坐標。例如,如果定義視區大小為寬640像素、高480像素,那么投影坐標(1.0f,
0.5f)經過視區變換后的屏幕坐標為(640, 240),如果定義視區大小為寬1024像素、高800像素,經過視區變換后的屏幕坐標為(1204,
400)。
世界空間的三維物體頂點坐標經過世界變換、取景變換、投影變換和視區變換,轉化為以像素為單位的屏幕坐標,就可以進行光柵化顯示了。在Direct3D程序中,只要定義并設置好相應的變換矩陣和視區信息,即構建好T&L流水線,剩余的各種頂點變換操作由Direct3D自動完成。
IDirect3DDevice9::SetTransform()函數用來設置頂點變換矩陣,該函數的聲明如下:
Sets a single device transformation-related state.
HRESULT SetTransform(
D3DTRANSFORMSTATETYPE State,
CONST D3DMATRIX * pMatrix
);
Parameters
- State
- [in] Device-state variable that is being modified.
This parameter can be any member of the D3DTRANSFORMSTATETYPE enumerated
type, or the D3DTS_WORLDMATRIX macro.
- pMatrix
- [in] Pointer to a D3DMATRIX structure that
modifies the current transformation.
Return Values
If the method succeeds, the return value is D3D_OK.
D3DERR_INVALIDCALL is returned if one of the arguments is invalid.
D3DTRANSFORMSTATETYPE
Defines constants that describe transformation state
values.
typedef enum D3DTRANSFORMSTATETYPE
{
D3DTS_VIEW = 2,
D3DTS_PROJECTION = 3,
D3DTS_TEXTURE0 = 16,
D3DTS_TEXTURE1 = 17,
D3DTS_TEXTURE2 = 18,
D3DTS_TEXTURE3 = 19,
D3DTS_TEXTURE4 = 20,
D3DTS_TEXTURE5 = 21,
D3DTS_TEXTURE6 = 22,
D3DTS_TEXTURE7 = 23,
D3DTS_FORCE_DWORD = 0x7fffffff,
} D3DTRANSFORMSTATETYPE, *LPD3DTRANSFORMSTATETYPE;
Constants
- D3DTS_VIEW
- Identifies the transformation matrix being set as
the view transformation matrix. The default value is NULL (the identity
matrix).
- D3DTS_PROJECTION
- Identifies the transformation matrix being set as
the projection transformation matrix. The default value is NULL (the
identity matrix).
- D3DTS_TEXTURE0
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_TEXTURE1
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_TEXTURE2
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_TEXTURE3
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_TEXTURE4
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_TEXTURE5
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_TEXTURE6
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_TEXTURE7
- Identifies the transformation matrix being set for
the specified texture stage.
- D3DTS_FORCE_DWORD
- Forces this enumeration to compile to 32 bits in
size. Without this value, some compilers would allow this enumeration to
compile to a size other than 32 bits. This value is not used.
Remarks
The transform states in the range 256 through 511 are
reserved to store up to 256 world matrices that can be indexed using the
D3DTS_WORLDMATRIX and D3DTS_WORLD macros.
Macros |
|
D3DTS_WORLD |
Equivalent to
D3DTS_WORLDMATRIX(0). |
D3DTS_WORLDMATRIX (index) |
Identifies the
transform matrix to set for the world matrix at index. Multiple world
matrices are used only for vertex blending. Otherwise only D3DTS_WORLD
is used. |
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.
Remarks
Direct3D sets the following default values for the
viewport.
D3DVIEWPORT9 vp;
vp.X = 0;
vp.Y = 0;
vp.Width = RenderTarget.Width;
vp.Height = RenderTarget.Height;
vp.MinZ = 0.0f;
vp.MaxZ = 1.0f;
IDirect3DDevice9::SetViewport can be used to draw on
part of the screen. Make sure to call it before any geometry is drawn so the
viewport settings will take effect.
To draw multiple views within a scene, repeat the
IDirect3DDevice9::SetViewport and draw geometry sequence for each view.