DrawPrimitive画图 没有颜色的问题
主要代码如下:
1 顶点结构
struct CUSTOMVERTEX
{
FLOAT x, y, z;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE) //顶点格式
2 顶点buf创建,填充
CUSTOMVERTEX vertices[] =
{
{ 1.0f, 3.0f, 0.0f, 0xffff0000, },
{ 2.0f, 1.0f, 0.0f, 0xff00ff00, },
{ 1.5f, 0.2f, 0.0f, 0xff0000ff, },
};
//创建顶点缓冲区
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}
//填充顶点缓冲区
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, vertices, sizeof(vertices) );
g_pVB->Unlock();
3 渲染环境的设置和绘制
//清空后台缓冲区
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(45, 50, 170), 1.0f, 0 );
//开始在后台缓冲区绘制图形
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
//在后台缓冲区绘制图形
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
//结束在后台缓冲区绘制图形
g_pd3dDevice->EndScene();
}
//将在后台缓冲区绘制的图形提交到前台缓冲区显示
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
==============================================================================
D3DFVF_XYZ默认的用户区中心坐标是(0,0),而D3DFVF_XYZRHW是左上角为(0,0)
用D3DFVF_XYZ默认的为非光照的,而D3DFVF_XYZRHW是高洛德光照。
当使用D3DFVF_XYZ时,要设置渲染状态:
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE)
而且要设置观察矩阵和投影矩阵,例如:
D3DXMATRIX matProj;
D3DXMATRIX matView;
// Create and set the projection matrix
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4.0f, 1.33333f, 1.0f, 1000.0f);
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);
// Create and set the view matrix
D3DXMatrixLookAtLH(&matView, \
&D3DXVECTOR3(0.0f, 0.0f, -500.0f), \
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), \
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView); 否则没有渲染结果。