【转】Direct3D顶点结构使用总结
D3D里面最基本的就是頂點(diǎn)了,雖說(shuō)一直在用,可是卻也是自己比較模糊的一個(gè)點(diǎn),知道其中的意思,卻不是很清楚,今天就總結(jié)一下,掃一下這個(gè)盲區(qū):
D3D中的頂點(diǎn)緩沖區(qū)的聲明:
LPDIRECT3DVERTEXBUFFER9 g_pVB??????? = NULL;??? //頂點(diǎn)緩沖區(qū)對(duì)象
通常都是用LPDIRECT3DVERTEXBUFFER9 來(lái)聲明頂點(diǎn)緩沖區(qū),它其實(shí)就是IDirect3DVertexBuffer9的指針類型,這兩個(gè)起到的效果是一樣的。用LPDIRECT3DVERTEXBUFFER9 聲明之后,只是鎮(zhèn)定了一個(gè)緩沖區(qū)的指針,下面還需要開(kāi)辟一個(gè)緩沖區(qū)給這個(gè)指針。
在開(kāi)辟真正的內(nèi)存之前,我們先看一下頂點(diǎn)格式的定義,D3D里面是采用的靈活頂點(diǎn)格式,這點(diǎn)大家應(yīng)該都是知道的,下面就來(lái)總結(jié)一下這些靈活頂點(diǎn)格式都具體有哪些,有什么用處。
一般定義頂點(diǎn)結(jié)構(gòu)的時(shí)候都是用一個(gè)結(jié)構(gòu)體,當(dāng)然用類去定義也可以,但是一般沒(méi)有那個(gè)必要。
struct CUSTOMVERTEX
{
??? FLOAT x, y, z, rhw;
??? DWORD color;
};
在還需要定義一個(gè)宏,來(lái)向D3D說(shuō)明一下,自己定義的頂點(diǎn)的格式到底有哪些。
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)? //頂點(diǎn)格式
上面這一句話的意思就是,定義的頂點(diǎn)結(jié)構(gòu)包含:位置變換信息(D3DFVF_XYZRHW)和漫反射顏色信息(D3DFVF_DIFFUSE);
那么,一共都有哪些類型可以定義呢,都有什么樣的用呢。
?
---------------------------------------------------------------------------------------------------------------------------------------
?
Vertex Data Flags
| D3DFVF_DIFFUSE | Vertex format includes a diffuse color component. | DWORD in ARGB order. See D3DCOLOR_ARGB. | 
| D3DFVF_NORMAL | Vertex format includes a vertex normal vector. This flag cannot be used with the D3DFVF_XYZRHW flag. | float, float, float | 
| D3DFVF_PSIZE | Vertex format specified in point size. This size is expressed in camera space units for vertices that are not transformed and lit, and in device-space units for transformed and lit vertices. | float | 
| D3DFVF_SPECULAR | Vertex format includes a specular color component. | DWORD in ARGB order. See D3DCOLOR_ARGB. | 
| D3DFVF_XYZ | Vertex format includes the position of an untransformed vertex. This flag cannot be used with the D3DFVF_XYZRHW flag. | float, float, float. | 
| D3DFVF_XYZRHW | Vertex format includes the position of a transformed vertex. This flag cannot be used with the D3DFVF_XYZ or D3DFVF_NORMAL flags. | float, float, float, float. | 
| D3DFVF_XYZB1 through D3DFVF_XYZB5 | Vertex format contains position data, and a corresponding number of weighting (beta) values to use for multimatrix vertex blending operations. Currently, Direct3D can blend with up to three weighting values and four blending matrices. For more information about using blending matrices, see Indexed Vertex Blending (Direct3D 9). | 1, 2, or 3 floats. When D3DFVF_LASTBETA_UBYTE4 is used, the last blending weight is treated as a DWORD. | 
| D3DFVF_XYZW | Vertex format contains transformed and clipped (x, y, z, w) data. ProcessVertices does not invoke the clipper, instead outputting data in clip coordinates. This constant is designed for, and can only be used with, the programmable vertex pipeline. | float, float, float, float | 
?
Texture Flags
?
| D3DFVF_TEX0 - D3DFVF_TEX8 | Number of texture coordinate sets for this vertex. The actual values for these flags are not sequential. | 
| D3DFVF_TEXCOORDSIZEN(coordIndex) | Define a texture coordinate data set. n indicates the dimension of the texture coordinates. coordIndex indicates texture coordinate index number. See D3DFVF_TEXCOORDSIZEN and Texture coordinates and Texture Stages. | 
?
Mask Flags
?
| D3DFVF_POSITION_MASK | Mask for position bits. | 
| D3DFVF_RESERVED0, D3DFVF_RESERVED2 | Mask values for reserved bits in the FVF. Do not use. | 
| D3DFVF_TEXCOUNT_MASK | Mask value for texture flag bits. | 
?
Miscellaneous Flags
?
| D3DFVF_LASTBETA_D3DCOLOR | The last beta field in the vertex position data will be of type D3DCOLOR. The data in the beta fields are used with matrix palette skinning to specify matrix indices. | 
| D3DFVF_LASTBETA_UBYTE4 | The last beta field in the vertex position data will be of type UBYTE4. The data in the beta fields are used with matrix palette skinning to specify matrix indices.  // Given the following vertex data definition: 
struct VERTEXPOSITION
{float pos[3];union {float beta[5];struct{float weights[4];DWORD MatrixIndices;  // Used as UBYTEs}}
}; Given the FVF is declared as: D3DFVF_XYZB5 | D3DFVF_LASTBETA_UBYTE4. Weight and MatrixIndices are included in beta[5], where D3DFVF_LASTBETA_UBYTE4 says to interpret the last DWORD in beta[5] as type UBYTE4. | 
| D3DFVF_TEXCOUNT_SHIFT | The number of bits by which to shift an integer value that identifies the number of texture coordinates for a vertex. This value might be used as shown below. DWORD dwNumTextures = 1; // Vertex has only one set of coordinates.// Shift the value for use when creating a // flexible vertex format (FVF) combination. dwFVF = dwNumTextures << D3DFVF_TEXCOUNT_SHIFT;// Now, create an FVF combination using the shifted value. | 
?
Examples
The following examples show other common flag combinations.
// Untransformed vertex for lit, untextured, Gouraud-shaded content. dwFVF = ( D3DFVF_XYZ | D3DFVF_DIFFUSE ); // Untransformed vertex for unlit, untextured, Gouraud-shaded // content with diffuse material color specified per vertex. dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE ); // Untransformed vertex for light-map-based lighting. dwFVF = ( D3DFVF_XYZ | D3DFVF_TEX2 ); // Transformed vertex for light-map-based lighting with shared rhw. dwFVF = ( D3DFVF_XYZRHW | D3DFVF_TEX2 ); // Heavyweight vertex for unlit, colored content with two // sets of texture coordinates. dwFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX2 );?
------------------------------------------------------------------------------------------------------------------
?
?
在頂點(diǎn)結(jié)構(gòu)體中沒(méi)有RHW時(shí),Direct3D將執(zhí)行視、投影、世界等變換以及進(jìn)行光線計(jì)算,之后你才能在窗口中得到你所繪制的物體。當(dāng)頂點(diǎn)結(jié)構(gòu)體中有RHW時(shí),就像上面那段英文所述,告知Direct3D使用的頂點(diǎn)已經(jīng)在屏幕坐標(biāo)系中了,不再執(zhí)行視圖、投影、世界等變換和光線計(jì)算,因?yàn)镈3DFVF_XYZRHW標(biāo)志告訴它頂點(diǎn)已經(jīng)經(jīng)過(guò)了這些處理,并直接將頂點(diǎn)進(jìn)行光柵操作,任何用SetTransform進(jìn)行的轉(zhuǎn)換都對(duì)其無(wú)效。不過(guò)這時(shí)的原點(diǎn)就在客戶區(qū)的左上角了,其中x向右為正,y向下為正,而z的意義已經(jīng)變?yōu)閦-buffer的象素深度。
??? 值得注意的是,D3DFVF_XYZRHW和D3DFVF_XYZ、D3DFVF_NORMAL不能共存,因?yàn)楹髢蓚€(gè)標(biāo)志與前一個(gè)矛盾。在使用這種頂點(diǎn)時(shí),系統(tǒng)需要頂點(diǎn)的位置已經(jīng)經(jīng)過(guò)變換了,也就是說(shuō)x、y必須在屏幕坐標(biāo)系中,z必須是z-buffer中的象素深度,取值范圍:0.0-1.0,離觀察者最近的地方為0.0,觀察范圍內(nèi)最遠(yuǎn)可見(jiàn)的地方為1.0。(不過(guò)我測(cè)試的時(shí)候似乎z值不起作用。)引自:http://www.cppblog.com/lovedday/archive/2009/03/22/48507.html
在定義完頂點(diǎn)格式以后,就要開(kāi)辟一塊頂點(diǎn)緩沖區(qū):
g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
????????????????????????????????????????????????? 0, D3DFVF_CUSTOMVERTEX,
????????????????????????????????????????????????? D3DPOOL_DEFAULT, &g_pVB, NULL )
開(kāi)辟緩沖區(qū)后,就需要對(duì)這個(gè)緩沖區(qū)進(jìn)行填寫(xiě),那么填寫(xiě)的數(shù)據(jù)呢,也需要先指定出來(lái):
CUSTOMVERTEX vertices[] =
??? {
{ 100.0f, 400.0f, 0.5f, 1.0f, 0xffff0000, },
??????? { 300.0f,? 50.0f, 0.5f, 1.0f, 0xff00ff00, },
??????? { 500.0f, 400.0f, 0.5f, 1.0f, 0xff0000ff, },
??? };
然后將數(shù)據(jù)寫(xiě)入緩沖區(qū):
VOID* pVertices;
??? if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
??????? return E_FAIL;
??? memcpy( pVertices, vertices, sizeof(vertices) );
??? g_pVB->Unlock();
?
posted on 2012-10-29 17:37 Lilac_F 閱讀(...) 評(píng)論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/Lilac-F/archive/2012/10/29/2745128.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【转】Direct3D顶点结构使用总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 演练:在组件设计器中创建 Windows
- 下一篇: DIV的边距属性在Chrome和IE中的
