顶点缓冲
/**-----------------------------------------------------------------------------
* 全局参数
*------------------------------------------------------------------------------
*/
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; /// 储存顶点的顶点缓冲
/// 定义用户顶点的结构体
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; /// 顶点的变换坐标(具有rhw值是完成变换的顶点).
DWORD color; /// 顶点的颜色
};
/// 表现用户顶点结构体信息的FVF值
/// 结构体由X、Y、Z、RHW值和Diffuse颜色值组成.
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
/**-----------------------------------------------------------------------------
* 创建顶点缓冲,设置顶点值.
* 顶点缓冲就是基本包含顶点数据的存储器块.
* 创建顶点缓冲之后,必须调用Lock()函数和Unlock()函数来定义指针
* 将顶点数据写入顶点缓冲.
* D3D可以使用索引缓冲.
* 基本系统存储器之外,顶点缓冲和索引缓冲也可以在设备存储器(显卡存储器)创建,
* 不过,这种情况下,大部分显卡的速度会受到很大的影响.
*
*------------------------------------------------------------------------------
*/
HRESULT InitVB()
{
/// 渲染三角形的三个顶点声明
CUSTOMVERTEX vertices[] =
{
{ 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
};
/// 创建顶点缓冲
/// 分配储存三个用户顶点的存储器.
/// 指定储存FVF的数据格式.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}
/// 将数据写入顶点缓冲.
/// 调用顶点缓冲的Lock()函数,定义指针.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof(vertices) );
g_pVB->Unlock();
return S_OK;
}
/**-----------------------------------------------------------------------------
* 删除初始化对象
*------------------------------------------------------------------------------
*/
VOID Cleanup()
{
if( g_pVB != NULL )
g_pVB->Release();
…
}
/**-----------------------------------------------------------------------------
* 绘图
*------------------------------------------------------------------------------
*/
VOID Render()
{
/// 将后置缓冲xx,同时设置为蓝色(0,0,255).
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
/// 开始渲染
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
/// 绘制顶点缓冲的三角形.
/// 1. 使包含顶点信息的顶点缓冲绑定一个设备数据流.
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
/// 2. 向D3D指定顶点着色信息,大多数情况下只指定FVF格式.
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
/// 3. 调用输出几何信息的DrawPrimitive()函数
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
/// 结束渲染
g_pd3dDevice->EndScene();
}
/// 显示后置缓冲的画面!
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//---------------------------------------备注---------------------------------------------
HRESULT IDirect3DDevice9::CreateVertexBuffer( //Creates a vertex buffer.
UINT Length, //[in] Size of the vertex buffer, in bytes. For FVF vertex buffers, Length must be large enough to contain at least one vertex, but it need not be a multiple of the vertex size. Length is not validated for non-FVF buffers.
DWORD Usage, //[in] Usage can be 0, which indicates no usage value. However, if usage is desired, use a combination of one or more D3DUSAGE constants.
DWORD FVF, //[in] Combination of D3DFVF, a usage specifier that describes the vertex format of the vertices in this buffer.
D3DPOOL Pool, //[in] Member of the D3DPOOL enumerated type, describing a valid memory class into which to place the resource.
IDirect3DVertexBuffer9** ppVertexBuffer, //[out, retval] Address of a pointer to an IDirect3DVertexBuffer9 interface, representing the created vertex buffer resource.
HANDLE* pSharedHandle //[in] Reserved. Set this parameter to NULL.
);
HRESULT IDirect3DVertexBuffer9::Lock( //Locks a range of vertex data and obtains a pointer to the vertex buffer memory.
UINT OffsetToLock, //[in] Offset into the vertex data to lock, in bytes. To lock the entire vertex buffer, specify 0 for both parameters, SizeToLock and OffsetToLock.
UINT SizeToLock, //[in] Size of the vertex data to lock, in bytes.
VOID ** ppbData, //[out] VOID* pointer to a memory buffer containing the returned vertex data.
DWORD Flags //[in] Combination of zero or more locking flags that describe the type of lock to perform.
);
HRESULT IDirect3DVertexBuffer9::Unlock(); //Unlocks vertex data.
HRESULT IDirect3DDevice9::SetStreamSource( //Binds a vertex buffer to a device data stream.
UINT StreamNumber, //[in] Specifies the data stream, in the range from 0 to the maximum number of streams -1.
IDirect3DVertexBuffer9 * pStreamData, //[in] Pointer to an IDirect3DVertexBuffer9 interface, representing the vertex buffer to bind to the specified data stream.
UINT OffsetInBytes, //[in] Offset from the beginning of the stream to the beginning of the vertex data, in bytes.
UINT Stride //[in] Stride of the component, in bytes.
);
HRESULT IDirect3DDevice9::SetVertexShader( //Sets the vertex shader.
IDirect3DVertexShader9* pShader //[in] Vertex shader interface.
);
HRESULT IDirect3DDevice9::DrawPrimitive( //Renders a sequence of nonindexed, geometric primitives of the specified type from the current set of data input streams.
D3DPRIMITIVETYPE PrimitiveType, //[in] Member of the D3DPRIMITIVETYPE enumerated type, describing the type of primitive to render.
UINT 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.
UINT PrimitiveCount //[in] Number of primitives to render.
);