Jump to content
  • Advertisement
Sign in to follow this  
roczhao

I can not use "DrawIndexedPrimitive" render anything?pls help me.

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

struct CUSTOMVERTEX { FLOAT x, y, z,hrw; // 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 }; 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 ); // Create the vertex buffer HRESULT hr; //CREATE VERTEXT BUFFER LPDIRECT3DVERTEXBUFFER9 vertexBuffer; hr = pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices3) * sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, 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(); // Create the index buffer LPDIRECT3DINDEXBUFFER9 iBuffer; 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,sizeof(IndexData3),(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, IndexData3, 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_XYZ|D3DFVF_DIFFUSE ); pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) ); // Set the indices to use pd3dDevice->SetIndices(iBuffer); //pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); // Call DrawPrimitive to draw the face pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, // BaseVertexIndex 0, // MinIndex 4, // NumVertices 0, // StartIndex 2 ); // primitive count pd3dDevice->EndScene(); pd3dDevice->Present( NULL, NULL, NULL, NULL ); }

Share this post


Link to post
Share on other sites
Advertisement
1) Your vertex format uses Transformed (screen space) coordinates, but the rest of your code doesn't.

2) What the heck are you trying to achieve with this:
if FAILED (hr) hr=NULL;

hr is a value, not a pointer. Setting it to 0 and ignoring the error is BAD. If there's an error, maybe you'd like to handle it. That way when calls fail, you'd know it.


3) Read the Forum FAQ, especially the bit about debugging. The debug runtime would have pointed out some of your errors.


Share this post


Link to post
Share on other sites
Quote:
Original post by roczhao
thanks.you can tell me how i can do it right about before code or how i can fix my code.



nobody help me?

Share this post


Link to post
Share on other sites
1) You're defining a vertex structure with RHW and diffuse (transformed and lit vertices), yet when defining an array, you don't define an RHW value. I can't see how the code compiles.

2) You create your buffers every time you want to render, which is a very bad thing.

3) You don't Release() your buffers after rendering, which means you'll have memory leaks.

4) "hr = pd3dDevice->CreateVertexBuffer(sizeof(g_Vertices3) * sizeof(CUSTOMVERTEX)..."
Given that g_Vertices3 is a static array, sizeof(g_Vertices3) gives the whole size of the array, so no need for the multiplication by sizeof(CUSTOMVERTEX)

Same goes for the vertex buffer locking, index buffer creation and locking.

5) In general, you need to read some tutorials. Refer to this section in the FAQ for a list of tutorial sites. I'd recommend DrunkenHyena's tutorials.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!