渲染顶点缓冲区图形
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_X#050505, 1.0f, 0);
g_device->BeginScene();
g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
g_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
函数IDirect3DDevice9::SetStreamSource()将顶点缓冲区和渲染数据流链接。
Binds a vertex buffer to a device data stream. For more information, see Setting the Stream Source (Direct3D 9).
HRESULT SetStreamSource(
UINT StreamNumber,
IDirect3DVertexBuffer9 * pStreamData,
UINT OffsetInBytes,
UINT Stride
);
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.
When a FVF vertex shader is used, the stride of the vertex stream must match the vertex size, computed from the FVF. When a declaration is used, the stride should be greater than or equal to the stream size computed from the declaration.
When calling SetStreamSource, the stride is normally required to be equal to the vertex size. However, there are times when you may want to draw multiple instances of the same or similar geometry (such as when using instancing to draw). For this case, use a zero stride to tell the runtime not to increment the vertex buffer offset (ie: use the same vertex data for all instances). For more information about instancing, see Efficiently Drawing Multiple Instances of Geometry (Direct3D 9).
函数IDirect3DDevice9::SetFVF()的作用是声明当前的渲染数据流中的灵活顶点格式。
Sets the current vertex stream declaration.
HRESULT SetFVF(
DWORD FVF
);
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be: D3DERR_INVALIDCALL.
Here are the steps necessary to initialize and use vertices that have a position, diffuse and specular color, and texture coordinates:
struct LVertex
{
FLOAT x, y, z;
D3DCOLOR specular, diffuse;
FLOAT tu, tv;
};
const DWORD VertexFVF = (D3DFVF_XYZ | D3DFVF_DIFFUSE |
D3DFVF_SPECULAR | D3DFVF_TEX1 );
g_d3dDevice->CreateVertexBuffer( 4*sizeof(LVertex),
D3DUSAGE_WRITEONLY, VertexFVF, D3DPOOL_DEFAULT, &pBigSquareVB, NULL );
LVertex * v;
pBigSquareVB->Lock( 0, 0, (BYTE**)&v, 0 );
v[0].x = 0.0f; v[0].y = 10.0; v[0].z = 10.0f;
v[0].diffuse = 0xffff0000;
v[0].specular = 0xff00ff00;
v[0].tu = 0.0f; v[0].tv = 0.0f;
v[1].x = 0.0f; v[1].y = 0.0f; v[1].z = 10.0f;
v[1].diffuse = 0xff00ff00;
v[1].specular = 0xff00ffff;
v[1].tu = 0.0f; v[1].tv = 0.0f;
v[2].x = 10.0f; v[2].y = 10.0f; v[2].z = 10.0f;
v[2].diffuse = 0xffff00ff;
v[2].specular = 0xff000000;
v[2].tu = 0.0f; v[2].tv = 0.0f;
v[3].x = 0.0f; v[3].y = 10.0f; v[3].z = 10.0f;
v[3].diffuse = 0xffffff00;
v[3].specular = 0xffff0000;
v[3].tu = 0.0f; v[3].tv = 0.0f;
pBigSquareVB->Unlock();
g_d3dDevice->SetFVF(VertexFVF);
g_d3dDevice->SetStreamSource(0, pBigSquareVB, 0, sizeof(LVertex));
g_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0 ,2);
Here are the steps necessary to initialize and use vertices that have a position, a normal, and texture coordinates:
struct Vertex
{
FLOAT x, y, z;
FLOAT nx, ny, nz;
FLOAT tu, tv;
};
const DWORD VertexFVF = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );
Vertex * v;
pBigSquareVB->Lock(0, 0, (BYTE**)&v, 0);
v[0].x = 0.0f; v[0].y = 10.0; v[0].z = 10.0f;
v[0].nx = 0.0f; v[0].ny = 1.0f; v[0].nz = 0.0f;
v[0].tu = 0.0f; v[0].tv = 0.0f;
v[1].x = 0.0f; v[1].y = 0.0f; v[1].z = 10.0f;
v[1].nx = 0.0f; v[1].ny = 1.0f; v[1].nz = 0.0f;
v[1].tu = 0.0f; v[1].tv = 0.0f;
v[2].x = 10.0f; v[2].y = 10.0f; v[2].z = 10.0f;
v[2].nx = 0.0f; v[2].ny = 1.0f; v[2].nz = 0.0f;
v[2].tu = 0.0f; v[2].tv = 0.0f;
v[3].x = 0.0f; v[3].y = 10.0f; v[3].z = 10.0f;
v[3].nx = 0.0f; v[3].ny = 1.0f; v[3].nz = 0.0f;
v[3].tu = 0.0f; v[3].tv = 0.0f;
pBigSquareVB->Unlock();
函数IDirect3DDevice9::DrawPrimitive()用来绘制当前的渲染数据流中的图元。
示例程序VertexBuffer的完整源码如下:
运行截图:
原文:http://www.cnblogs.com/sanghai/p/3948871.html