|
|
//关于DX的索引渲染,不知道错再那?
truct CUSTOMVERTEX
{
FLOAT x, y, z; // the untransformed, 3D position for the vertex
DWORD color; // the color of the vertex
};
CUSTOMVERTEX g_Vertices3[] = {
// X Y Z U V
{-200.0f,-200.0f,-200.0f, D3DCOLOR_ARGB(0,0,255,0)}, // 0
{-200.0f, 200.0f,-200.0f, D3DCOLOR_ARGB(0,0,255,0)}, // 1
{200.0f, 200.0f,-200.0f, D3DCOLOR_ARGB(0,0,255,0)}, // 2
{200.0f,-200.0f,-200.0f, D3DCOLOR_ARGB(0,0,255,0)}, // 3
};
void IndexBufferRender(void)
{
// Check to make sure you have a valid Direct3D device
if( NULL == pd3dDevice )
return;// Clear the back buffer to a blue color
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
// Create the vertex buffer
HRESULT hr;
//CREATE VERTEXT BUFFER
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
hr = pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices3) * sizeof(CUSTOMVERTEX),
0,
D3DFVF_DIFFUSE|D3DFVF_XYZRHW,
D3DPOOL_DEFAULT,
&vertexBuffer,
NULL );
// Check the return code of CreateVertexBuffer call to make sure it succeeded
if FAILED (hr)
hr=NULL;
// Prepare to copy the vertices into the vertex buffer
VOID* pVertices;
// Lock the vertex buffer
hr = vertexBuffer->Lock(0,
sizeof(g_Vertices3)*sizeof(CUSTOMVERTEX),
(void**) &pVertices,
0);
// Check to make sure the vertex buffer can be locked
if FAILED (hr)
hr=NULL;
// Copy the vertices into the buffer
memcpy ( pVertices, g_Vertices3, sizeof(g_Vertices3)*sizeof(CUSTOMVERTEX) );
// Unlock the vertex buffer
vertexBuffer->Unlock();
// Present the back buffer contents to the display
LPDIRECT3DINDEXBUFFER9 iBuffer;
// index buffer data
WORD IndexData3[ ] = {
0,1,2, // triangle 1
2,3,0 // triangle 2
};
// Create the index buffer
//MessageBox(NULL,"gg","D",MB_ABORTRETRYIGNORE);
hr = pd3dDevice->CreateIndexBuffer(sizeof(IndexData3)*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_DEFAULT,
&iBuffer,
NULL );
// Check the return code of CreateVertexBuffer call to make sure it succeeded
if FAILED (hr)
hr=NULL;
// // Prepare to copy the indexes into the index buffer
VOID* IndexPtr;;
// Lock the vertex buffer
hr = iBuffer->Lock(0,
0,
(void**) &IndexPtr,
0);
// Check to make sure the vertex buffer can be locked
if FAILED (hr)
hr=NULL;
// // Copy the indices into the buffer
memcpy ( IndexPtr, g_Vertices3, sizeof(IndexData3)*sizeof(WORD) );
// Unlock the vertex buffer
iBuffer->Unlock();
// Present the back buffer contents to the display
pd3dDevice->BeginScene();
// Set the vertex format
pd3dDevice->SetFVF( D3DFVF_DIFFUSE|D3DFVF_XYZRHW );
pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
// Set the indices to use
pd3dDevice->SetIndices(iBuffer);
// Call DrawPrimitive to draw the cube
pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0, // BaseVertexIndex
0, // MinIndex
4, // NumVertices
0, // StartIndex
2 ); // primitive count*/
pd3dDevice->EndScene();
pd3dDevice-> resent( NULL, NULL, NULL, NULL );
} |
-
|