頂點(diǎn)坐標(biāo)變換(1)
空間中的物體需要使用三維坐標(biāo)來描述,而顯示器是一個(gè)二維的表面,所以在屏幕上渲染一個(gè)三維場(chǎng)景時(shí),首先需要將描述空間物體的三維坐標(biāo)變換為二維坐標(biāo)(世界坐標(biāo)到屏幕坐標(biāo)),這在Direct3D中稱為頂點(diǎn)坐標(biāo)變換。頂點(diǎn)坐標(biāo)變換通常通過矩陣來完成。可以把頂點(diǎn)坐標(biāo)變換想象成攝像過程,三維世界的景物通過攝像機(jī)的拍攝顯示在二維的相片上,所不同的是把相片換成了屏幕。
頂點(diǎn)坐標(biāo)變換和光照流水線概述
Direct3D中渲染三維對(duì)象的過程可分為兩個(gè)階段。第一階段稱為坐標(biāo)變換和光照(Transforming and Lighting T&L)階段。在這個(gè)階段,每個(gè)對(duì)象的頂點(diǎn)被從一個(gè)抽象的、浮點(diǎn)坐標(biāo)空間轉(zhuǎn)換到基于像素的屏幕空間(坐標(biāo)變換不僅包含物體頂點(diǎn)位置的坐標(biāo)變換,它還可能包含頂點(diǎn)法線、紋理坐標(biāo)等的坐標(biāo)變換),并根據(jù)場(chǎng)景中光源和物體表面的材質(zhì)對(duì)物體頂點(diǎn)應(yīng)用不同類型的光照效果。還有其他一些比較重要的任務(wù),如裁剪和視口縮放也在第一階段進(jìn)行。第二階段稱為光柵化處理階段,Direct3D將經(jīng)過T&L處理的頂點(diǎn)組織以點(diǎn)、線、面為基礎(chǔ)的圖元,應(yīng)用紋理貼圖和物體頂點(diǎn)的顏色屬性,根據(jù)相應(yīng)渲染狀態(tài)設(shè)置(如著色模式),決定每個(gè)像素的最終顏色值,并在屏幕上顯示出來。
有時(shí)根據(jù)特殊的需要,可以跳過其中的某些步驟。如果愿意,也可以提供自己的坐標(biāo)變換和光照過程,并將處理后的頂點(diǎn)直接傳給Direct3D光柵化處理程序,而繞過Direct3D的T&L階段。
T&L的過程在Direct3D中通常稱為頂點(diǎn)變換流水線,在這個(gè)過程中,未經(jīng)過變換和光照的頂點(diǎn)從一端進(jìn)入,在內(nèi)部這些頂點(diǎn)將完成幾個(gè)連續(xù)操作,然后經(jīng)過轉(zhuǎn)換和光照的頂點(diǎn)從另一端出來。應(yīng)用程序通過指定幾個(gè)矩陣、視口以及所使用的光線來建立T&L流水線,然后應(yīng)用程序?qū)㈨旤c(diǎn)送入流水線,對(duì)這些頂點(diǎn)進(jìn)行變換、照明和裁剪,將其投影到屏幕空間,并根據(jù)視口的規(guī)定對(duì)其進(jìn)行縮放。我們認(rèn)為經(jīng)過流水線的頂點(diǎn)是已經(jīng)經(jīng)過處理的,并且已經(jīng)準(zhǔn)備好傳送給光柵化處理程序。
下面首先介紹T&L流水線涉及到的一些基本概念:
(1)世界變換和世界坐標(biāo)系:物體在三維空間的運(yùn)動(dòng)和變形過程稱為世界變換,如平移、旋轉(zhuǎn)、縮放等。物體在其中運(yùn)動(dòng)的三維空間稱為世界空間,它的三維坐標(biāo)系表示稱為世界坐標(biāo)系,物體頂點(diǎn)在世界坐標(biāo)系里的坐標(biāo)變換稱為世界變換。
(2)取景變換和觀察坐標(biāo)系:把圖形顯示想象成攝像過程,取景變換就像攝像機(jī)中攝像機(jī)的擺放一樣,在三維圖形顯示中,需要設(shè)置一個(gè)虛擬攝像機(jī),屏幕顯示的圖形就是虛擬攝像機(jī)拍攝在膠片上的景物。以攝像機(jī)位置為參考原點(diǎn),攝像機(jī)觀察的方向?yàn)樽鴺?biāo)軸,建立的坐標(biāo)系稱為觀察坐標(biāo)系,物體在觀察坐標(biāo)系中的相對(duì)坐標(biāo)稱為觀察坐標(biāo),頂點(diǎn)從世界坐標(biāo)到觀察坐標(biāo)的轉(zhuǎn)換稱為取景變換。
(3)投影坐標(biāo)和投影坐標(biāo)系:物體從世界坐標(biāo)描述轉(zhuǎn)換到觀察坐標(biāo)后,可將三維物體投影到二維表面上,即投影到虛擬攝像機(jī)的膠片上,這個(gè)過程就是投影變換。以膠片中心為參考原點(diǎn)的空間坐標(biāo)系稱為投影坐標(biāo)系,物體在投影坐標(biāo)系中的坐標(biāo)稱為投影坐標(biāo)。
(4)視區(qū)變換和屏幕坐標(biāo)系:物體在投影坐標(biāo)系中的表示為浮點(diǎn)坐標(biāo),通過定義屏幕顯示區(qū)域(一般為顯示窗口大小),將浮點(diǎn)坐標(biāo)轉(zhuǎn)化為像素坐標(biāo)的過程稱為視區(qū)變換,該像素坐標(biāo)值稱為屏幕坐標(biāo)。例如,如果定義視區(qū)大小為寬640像素、高480像素,那么投影坐標(biāo)(1.0f, 0.5f)經(jīng)過視區(qū)變換后的屏幕坐標(biāo)為(640, 240),如果定義視區(qū)大小為寬1024像素、高800像素,經(jīng)過視區(qū)變換后的屏幕坐標(biāo)為(1204, 400)。
世界空間的三維物體頂點(diǎn)坐標(biāo)經(jīng)過世界變換、取景變換、投影變換和視區(qū)變換,轉(zhuǎn)化為以像素為單位的屏幕坐標(biāo),就可以進(jìn)行光柵化顯示了。在Direct3D程序中,只要定義并設(shè)置好相應(yīng)的變換矩陣和視區(qū)信息,即構(gòu)建好T&L流水線,剩余的各種頂點(diǎn)變換操作由Direct3D自動(dòng)完成。
IDirect3DDevice9::SetTransform()函數(shù)用來設(shè)置頂點(diǎn)變換矩陣,該函數(shù)的聲明如下:
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()函數(shù)用來設(shè)置視區(qū)信息,該函數(shù)聲明如下:
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.
posted on 2008-05-02 09:35 lovedday 閱讀(2477) 評(píng)論(0) 編輯 收藏 引用

