标签:des style blog http color os io 使用 strong
使用索引缓冲区绘制图形
当绘制一个比较复杂的图形时,需要使用许多相互邻接的三角形。如果为每个三角形准备三个顶点数据,显然有许多数据是重复的,这样会浪费大量的内存和系统带宽。为了解决这一问题,可以先创建一个顶点缓冲区,将不重复的顶点数据写入顶点缓冲区,然后创建一个顶点索引缓冲区(index buffer),存放各个三角形的顶点索引信息,最后通过顶点索引和顶点数据共同完成图形绘制。
在Direct3D中一个顶点的索引只需要用一个16位或32位的整数表示,因此当多边形的顶点有较多重复使用时,使用索引数组通常能够比直接绘制顶点序列节省一部分内存空间和系统带宽。同时,Direct3D渲染流水线避免了对相同顶点进行重复计算,从而可以相应地提高图形程序的整体性能。
首先定义顶点结构和灵活顶点格式:
struct sCustomVertex
{
float x, y, z, rhw;
DWORD color;
};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
接着创建顶点缓冲区和索引缓冲区,各顶点位置如下图所示:
void init_vertices()
{
sCustomVertex vertices[9];
vertices[0].x = 300.0f;
vertices[0].y = 250.0f;
vertices[0].z = 0.5f;
vertices[0].rhw = 1.0f;
vertices[0].color = 0xffff0000;
for(int i = 0; i < 8; i++)
{
vertices[i+1].x = (200 * sin(i * 3.14159f / 4.0f)) + 300;
vertices[i+1].y = -(200 * cos(i * 3.14159f / 4.0f)) + 250;
vertices[i+1].z = 0.5f;
vertices[i+1].rhw = 1.0f;
vertices[i+1].color = 0xff00ff00;
}
WORD indices[] = { 0,1,2, 0,2,3, 0,3,4, 0,4,5, 0,5,6, 0,6,7, 0,7,8, 0,8,1 };
// push vertex data into vertex buffer
g_device->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);
void* ptr;
g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();
// push vertex index into index buffer
g_device->CreateIndexBuffer(sizeof(indices), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_index_buffer, NULL);
void* index_ptr;
g_index_buffer->Lock(0, sizeof(indices), (void**)&index_ptr, 0);
memcpy(index_ptr, indices, sizeof(indices));
g_index_buffer->Unlock();
}
在上面的代码段中,使用IDirect3DDevice9::CreateIndexBuffer()函数创建所需要的索引缓冲区,该函数的声明如下:
Creates an index buffer.
HRESULT CreateIndexBuffer(
UINT Length,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DIndexBuffer9** ppIndexBuffer,
HANDLE* pSharedHandle
);
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DERR_OUTOFVIDEOMEMORY, D3DXERR_INVALIDDATA, E_OUTOFMEMORY.
Index buffers are memory resources used to hold indices, they are similar to both surfaces and vertex buffers. The use of index buffers enables Direct3D to avoid unnecessary data copying and to place the buffer in the optimal memory type for the expected usage.
To use index buffers, create an index buffer, lock it, fill it with indices, unlock it, pass it to IDirect3DDevice9::SetIndices, set up the vertices, set up the vertex shader, and call IDirect3DDevice9::DrawIndexedPrimitive for rendering.
The MaxVertexIndex member of the D3DCAPS9 structure indicates the types of index buffers that are valid for rendering.
接着使用索引缓冲区绘制图形:
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_X#050505, 1.0f, 0);
g_device->BeginScene();
g_device->SetRenderState(D3DRS_FILLMODE, g_fill_mode);
g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
g_device->SetIndices(g_index_buffer);
g_device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 9, 0, 8);
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
函数IDirect3DDevice9::DrawIndexedPrimitive()使用当前设置的索引缓冲区绘制图形:
Based on indexing, renders the specified geometric primitive into an array of vertices.
HRESULT DrawIndexedPrimitive(
D3DPRIMITIVETYPE Type,
INT BaseVertexIndex,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount
);
If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be the following: D3DERR_INVALIDCALL.
This method draws indexed primitives from the current set of data input streams. MinIndex and all the indices in the index stream are relative to the BaseVertexIndex.
The MinIndex and NumVertices parameters specify the range of vertex indices used for each IDirect3DDevice9::DrawIndexedPrimitive call. These are used to optimize vertex processing of indexed primitives by processing a sequential range of vertices prior to indexing into these vertices. It is invalid for any indices used during this call to reference any vertices outside of this range.
IDirect3DDevice9::DrawIndexedPrimitive fails if no index array is set.
The D3DPT_POINTLIST member of the D3DPRIMITIVETYPE enumerated type is not supported and is not a valid type for this method.
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.
完整源码如下:
实体模式
线框模式
也可以只使用顶点缓冲区通过三角扇形图元来绘制,改动init_vertices()和render()即可,需要增加一个顶点使多边形封闭(即,使v9==v1)。
void init_vertices()
{
sCustomVertex vertices[10];
vertices[0].x = 300.0f;
vertices[0].y = 250.0f;
vertices[0].z = 0.5f;
vertices[0].rhw = 1.0f;
vertices[0].color = 0xffff0000;
for(int i = 0; i < 9; i++)
{
vertices[i+1].x = (200 * sin(i * 3.14159f / 4.0f)) + 300;
vertices[i+1].y = -(200 * cos(i * 3.14159f / 4.0f)) + 250;
vertices[i+1].z = 0.5f;
vertices[i+1].rhw = 1.0f;
vertices[i+1].color = 0xff00ff00;
}
// push vertex data into vertex buffer
g_device->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);
void* ptr;
g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();
}
void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_X#050505, 1.0f, 0);
g_device->BeginScene();
g_device->SetRenderState(D3DRS_FILLMODE, g_fill_mode);
g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
g_device->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 8);
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
标签:des style blog http color os io 使用 strong
原文地址:http://www.cnblogs.com/sanghai/p/3948893.html