Direct3D提供了多種渲染狀態,它影響幾何物體怎樣被渲染。渲染狀態有默認值,因此假如你的應用程序需要不同于默認設置的渲染時,你僅僅改變它即可。一種渲染效果會一直起作用,直到你下一次改變渲染狀態為止。為了設置一個渲染狀態,我們使用下面的方法:
Sets a single device render-state parameter.
HRESULT SetRenderState(
D3DRENDERSTATETYPE State,
DWORD Value
);
Parameters
- State
- [in] Device state variable that is being modified.
This parameter can be any member of the D3DRENDERSTATETYPE enumerated type.
- Value
- [in] New value for the device render state to be
set. The meaning of this parameter is dependent on the value specified for
State. For example, if State were D3DRS_SHADEMODE, the second
parameter would be one member of the D3DSHADEMODE enumerated type.
Return Values
If the method succeeds, the return value is D3D_OK.
D3DERR_INVALIDCALL is returned if one of the arguments is invalid.
例如,在下面的例子中我們將使用線框模式渲染我們的物體。因此,我們設置如下的渲染狀態:
_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
|
注意:查看DirectX
SDK中關于D3DRENDERSTATETYPE的信息。其中詳細介紹了所有的渲染狀態。
一旦我們創建好一個頂點緩存以及一個索引緩存(可選的)后,我們就為渲染其中的內容準備得差不多了,但是在渲染前我們還有3個步驟必須先做。
1、
設置資源流。設置資源流與一個頂點緩存掛鉤,此流就是一個流入渲染管線的幾何信息的流。
下面的方法是用于設置一個資源流:
HRESULT
IDirect3DDevice9::SetStreamSource(
UINT StreamNumber,
IDirect3DVertexBuffer9* pStreamData,
UINT OffsetInBytes,
UINT Stride
);
|
StreamNumber——確定我們的頂點緩存與哪一個資源流掛鉤。我們不使用多重流;因此我們總是使用0號流。
pStreamData——一個指向我們想與流掛鉤的那個頂點緩存的指針。
OffsetInBytes——相對流開始處的偏移量。以字節為單位,它指定被填入渲染管線的頂點數據的開始位置。通過檢查D3DCAPS9結構中的D3DDEVCAPS2_STREAMOFFSET標志,假如你的設備支持,那么這個參數就有一些非0值。
Stride——我們在頂點緩存中操作的每個部分的流的字節大小。
例如,假設vb是一個已經填充了頂點信息的頂點緩存:
_device->SetStreamSource(
0, vb, 0, sizeof( Vertex ) );
|
2、
設置索引緩存。假如我們使用了索引緩存,我們必須設置后面要用于繪制操作的索引緩存。每次我們只能使用一個索引緩存;因此假如你需要用一個不同的索引緩存繪制一個物體時,你必須轉換到另一個上。下面的代碼設置一個索引緩存:
_device->SetIndices( _ib );
//
傳遞一個索引緩存指針的拷貝
|
3.4用頂點/索引緩存繪制
在我們創建好頂點/索引緩存以及做好準備工作以后,我們就能繪制我們的幾何物體了。這是通過使用DrawPrimitive或者DrawIndexedPrimitive傳送幾何信息到達渲染管線。這些方法從頂點流中獲得頂點信息以及從索引緩存中獲得索引信息。
3.4.1
IDirect3DDevice9::DrawPrimitive
這個方法不使用索引信息繪制圖元。
HRESULT
IDirect3DDevice9::DrawPrimitive(
D3DPRIMITIVETYPE
PrimitiveType,
UINT StartVertex,
UINT PrimitiveCount
);
|
PrimitiveType——我們繪制的圖元類型。比如,我們能繪制點和線以及三角形。以后我們使用三角形,用D3DPT_TRIANGLELIST參數。
StartVertex——索引到在頂點流中的一個元素。設置渲染頂點中的開始點。這個參數給予我們一定的機動性,可以繪制一個頂點緩存中的某部分。
PrimitiveCount——繪制圖元的個數。
例子:
//
繪制4個三角形
_device->DrawPrimitive(
D3DPT_TRIANGLELIST, 0, 4);
|
Renders a sequence of nonindexed, geometric primitives
of the specified type from the current set of data input streams.
HRESULT DrawPrimitive(
D3DPRIMITIVETYPE PrimitiveType,
UINT StartVertex,
UINT PrimitiveCount
);
Parameters
- PrimitiveType
- [in] Member of the D3DPRIMITIVETYPE enumerated
type, describing the type of primitive to render.
- StartVertex
- [in] Index of the first vertex to load. Beginning
at StartVertex the correct number of vertices will be read out of the vertex
buffer.
- PrimitiveCount
- [in] Number of primitives to render. The maximum
number of primitives allowed is determined by checking the MaxPrimitiveCount
member of the D3DCAPS9 structure. PrimitiveCount is the number of primitives
as determined by the primitive type. If it is a line list, each primitive
has two vertices. If it is a triangle list, each primitive has three
vertices.
Return Values
If the method succeeds, the return value is D3D_OK. If
the method fails, the return value can be D3DERR_INVALIDCALL.
Remarks
When converting a legacy application to Direct3D 9, you
must add a call to either IDirect3DDevice9::SetFVF to use the fixed function
pipeline, or IDirect3DDevice9::SetVertexDeclaration to use a vertex shader
before you make any Draw calls.
Defines the primitives supported by Direct3D.
typedef enum D3DPRIMITIVETYPE
{
D3DPT_POINTLIST = 1,
D3DPT_LINELIST = 2,
D3DPT_LINESTRIP = 3,
D3DPT_TRIANGLELIST = 4,
D3DPT_TRIANGLESTRIP = 5,
D3DPT_TRIANGLEFAN = 6,
D3DPT_FORCE_DWORD = 0x7fffffff,
} D3DPRIMITIVETYPE, *LPD3DPRIMITIVETYPE;
Constants
- D3DPT_POINTLIST
- Renders the vertices as a collection of isolated
points. This value is unsupported for indexed primitives.
- D3DPT_LINELIST
- Renders the vertices as a list of isolated
straight line segments.
- D3DPT_LINESTRIP
- Renders the vertices as a single polyline.
- D3DPT_TRIANGLELIST
Renders the specified vertices as a sequence of
isolated triangles. Each group of three vertices defines a separate
triangle.
- Back-face culling is affected by the current
winding-order render state.
- D3DPT_TRIANGLESTRIP
- Renders the vertices as a triangle strip. The
backface-culling flag is automatically flipped on even-numbered triangles.
- D3DPT_TRIANGLEFAN
- Renders the vertices as a triangle fan.
- D3DPT_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
Using Triangle Strips (Direct3D 9) or Triangle Fans
(Direct3D 9) is often more efficient than using triangle lists because fewer
vertices are duplicated.
3.4.2
IDirect3DDevice9::DrawIndexedPrimitive
這個方法使用索引信息來繪制圖元。
HRESULT
IDirect3DDevice9::DrawIndexedPrimitive(
D3DPRIMITIVETYPE
Type,
INT BaseVertexIndex,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount
);
|
Type——我們繪制的圖元類型。比如,我們能繪制點和線以及三角形。以后我們使用三角形,用D3DPT_TRIANGLELIST參數。
BaseVertexIndex——一個基本數字,在調用中用它去加上索引。參看下面的說明。
MinIndex——將被引用的最小索引值。
NumVertices——在此調用中將被引用的頂點數。
StartIndex——索引到索引緩存中的某個位置,它標記開始渲染的開始索引點。
PrimitiveCount——繪制圖元的個數。
例子:
_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
|
注意:BaseVertexIndex參數需要一些特別的解釋。在解釋過程中將會用到的圖3.2。

在索引緩存中定位頂點相應的也就在頂點緩存中定位了。然而,假設我們想將球,盒子,圓柱體的頂點放置到一個公共的頂點緩存中。對于每一個物體,我們將不得不再計算在公共頂點緩存中的索引。這個新的索引值是通過與一個偏移量相加得到。注意這個偏移量是標準的頂點,而不是字節。
我們需要計算物體在公共頂點緩存中的索引值。Direct3D允許我們通過設置BaseVertexIndex參數得到一個頂點偏移量,隨后Direct3D就能利用頂點自身的索引重新計算新的索引。
3.4.3
開始/結束場景
最后一點就是所有繪制方法都必須在IDirect3DDevice9::BeginScene和IDirect3DDevice9::EndScene方法之間被調用。例如,我們將這樣寫:
_device->BeginScene();
//
繪制場景
_device->DrawPrimitive(...);
_device->EndScene();
|
通過在代碼中建造每個三角形來建造3D物體是一件非??菰锏氖隆P疫\的是,D3DX庫已經為我們提供了一些方法來產生簡單3D物體的網格數據。
D3DX庫提供如下6種網格生成函數。
D3DXCreateBox
D3DXCreateSphere
D3DXCreateCylinder
D3DXCreateTeapot
D3DXCreatePolygon
D3DXCreateTorus

這6種函數的使用都很類似,并且使用D3DX網格數據結構ID3DXMesh就象使用ID3DXBuffer接口一樣?,F在,我們忽視它們的詳細信息,只需簡單使用它們即可。
HRESULT D3DXCreateTeapot(
LPDIRECT3DDEVICE9
pDevice, //
與mesh關聯的設備
LPD3DXMESH* ppMesh,
//
返回的mesh
LPD3DXBUFFER*
ppAdjacency //
現在設成0
);
|
一個使用D3DXCreateTeapot函數的例子:
ID3DXMesh* mesh = 0;
D3DXCreateTeapot(_device,
&mesh, 0);
|
一旦生成了網格數據,我們就能使用ID3DXMesh::DrawSubset方法繪制圖形了。這個方法有一個參數,它用來識別網格的一個子集。這個網格是通過上面的D3DXCreate*函數中的一個子集創建的,因此可以給這個參數指定0值。一個渲染網格的例子:
_device->BeginScene();
mesh->DrawSubset(0);
_device->EndScene();
|
使用了網格以后,必須釋放(release)它:
mesh->Release();
_mesh = 0;
|
實例程序:三角形
這是非常簡單的應用程序,它示范了在線框模式下怎樣創建并渲染一個三角形。
/**************************************************************************************
Renders a triangle in wireframe mode.
Demonstrates vertex buffers, render states, and drawing commands.
**************************************************************************************/
#include "d3dUtility.h"
#pragma warning(disable : 4100)
const int WIDTH = 640;
const int HEIGHT = 480;
IDirect3DDevice9* g_d3d_device = NULL;
IDirect3DVertexBuffer9* g_triangle_vb = NULL;
class cVertex
{
public:
float m_x, m_y, m_z;
cVertex() {}
cVertex(float x, float y, float z)
{
m_x = x;
m_y = y;
m_z = z;
}
};
const DWORD VERTEX_FVF = D3DFVF_XYZ;
////////////////////////////////////////////////////////////////////////////////////////////////////
bool setup()
{
g_d3d_device->CreateVertexBuffer(3 * sizeof(cVertex), D3DUSAGE_WRITEONLY, VERTEX_FVF,
D3DPOOL_MANAGED, &g_triangle_vb, NULL);
// fill the buffers with the triangle data
cVertex* vertices;
g_triangle_vb->Lock(0, 0, (void**)&vertices, 0);
vertices[0] = cVertex(-1.0f, 0.0f, 2.0f);
vertices[1] = cVertex( 0.0f, 1.0f, 2.0f);
vertices[2] = cVertex( 1.0f, 0.0f, 2.0f);
g_triangle_vb->Unlock();
// set the projection matrix
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)WIDTH/HEIGHT, 1.0f, 1000.0f);
g_d3d_device->SetTransform(D3DTS_PROJECTION, &proj);
// set wireframe mode render state
g_d3d_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
return true;
}
void cleanup()
{
safe_release<IDirect3DVertexBuffer9*>(g_triangle_vb);
}
bool display(float time_delta)
{
g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
g_d3d_device->BeginScene();
g_d3d_device->SetStreamSource(0, g_triangle_vb, 0, sizeof(cVertex));
g_d3d_device->SetFVF(VERTEX_FVF);
// draw one triangle
g_d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_d3d_device->EndScene();
g_d3d_device->Present(NULL, NULL, NULL, NULL);
return true;
}
LRESULT CALLBACK wnd_proc(HWND hwnd, UINT msg, WPARAM word_param, LPARAM long_param)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
if(word_param == VK_ESCAPE)
DestroyWindow(hwnd);
break;
}
return DefWindowProc(hwnd, msg, word_param, long_param);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, PSTR cmd_line, int cmd_show)
{
if(! init_d3d(inst, WIDTH, HEIGHT, true, D3DDEVTYPE_HAL, &g_d3d_device))
{
MessageBox(NULL, "init_d3d() - failed.", 0, MB_OK);
return 0;
}
if(! setup())
{
MessageBox(NULL, "Steup() - failed.", 0, MB_OK);
return 0;
}
enter_msg_loop(display);
cleanup();
g_d3d_device->Release();
return 0;
}
截圖:

下載三角形源程序