indexbuffer does not render anything? c

Started by
5 comments, last by roczhao 18 years, 12 months ago
//this is c code ,pls give me a idea.

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

};
WORD IndexData3[ ] = 
{
0,1,2,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 );


HRESULT hr;
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;

hr = pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices3) * sizeof(CUSTOMVERTEX),
0,
D3DFVF_XYZ|D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&vertexBuffer,
NULL );

VOID* pVertices;

hr = vertexBuffer->Lock(0,sizeof(g_Vertices3)*sizeof(CUSTOMVERTEX),(void**)&pVertices,0);

memcpy ( pVertices, g_Vertices3, sizeof(g_Vertices3)*sizeof(CUSTOMVERTEX) );

vertexBuffer->Unlock();

// Create the index buffer 
LPDIRECT3DINDEXBUFFER9 iBuffer;
hr = pd3dDevice->CreateIndexBuffer(sizeof(IndexData3)*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX32,
D3DPOOL_DEFAULT,
&iBuffer,
NULL );

VOID* IndexPtr;

hr = iBuffer->Lock(0,sizeof(IndexData3),(void**) &IndexPtr,0);

memcpy ( IndexPtr, IndexData3, sizeof(IndexData3)*sizeof(WORD) );

iBuffer->Unlock();

pd3dDevice->BeginScene();

pd3dDevice->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE );
pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
pd3dDevice->SetIndices(iBuffer);

hr=pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP,
0, // BaseVertexIndex
0, // MinIndex
4, // NumVertices
0, // StartIndex
2 ); // primitive count

pd3dDevice->EndScene();

pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
[Edited by - Coder on April 27, 2005 2:39:21 AM]
Advertisement
First, you use WORD for your indices, so it should be D3DFMT_INDEX16 instead of D3DFMT_INDEX32 (WORDs are 16 bits if i'm right)

Second, not an answer to your question, but you musn't create your buffers in the render function ! Create it once at the beginning of the program, and delete it at the end. Only lock / unlock during rendering, if you have to.


The first point should make your piece of code to work (I hope ^^)


.:: Paic Citron ::.

[Edited by - paic on April 27, 2005 8:12:55 AM]
In addition to what Paic said, you need 6 indices to represent 2 triangles. The content of an index buffer representing a quad would look something like this: 0, 1, 2, 0, 2, 3.

The code you have now could cause a system crash as the DrawPrimitive would try to draw the second triangle with partially undefined data. Then again, it might just not draw anything.

Niko Suni

@Nik02: that is not true. What you say goes for triangle lists. However, the OP is using a triangle strip which reuses every last two indices. So four indices for two triangles is fine here.

Greetz,

Illco
Have you set your View and Projection matrices?

using D3DXMatrixLookAtLH() and D3DXMatrixPerspectiveFovLH(), for example.
Chris PergrossiMy Realm | "Good Morning, Dave"
Quote:Original post by Illco
@Nik02: that is not true. What you say goes for triangle lists. However, the OP is using a triangle strip which reuses every last two indices. So four indices for two triangles is fine here.

Greetz,

Illco


Oh yeah, didn't notice that :)

Sorry for the mis-information, it wasn't intentional.

By the way, triangle lists are much more intuitive, especially for a beginner, than triangle strips - IMHO.

Niko Suni

THANKS,I KNOW MISTAKE ABOUT MY CODE.
//MY STRUCT
//A:
struct CUSTOMVERTEX
{
FLOAT x, y, z,rhw;
DWORD color;
};
//B:
CUSTOMVERTEX g_Vertices3[] = {
// X Y Z U V
{-200.0f,-200.0f,-200.0f,1, D3DCOLOR_ARGB(0,0,255,0)},
......
.....}
//C:
pd3dDevice->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE );

I THINK IT IS RIGHT,I TRY IT.

This topic is closed to new replies.

Advertisement