Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

onnel

DrawIndexedPrimitive Problem

This topic is 6152 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

Got my octree and textures working wonderfully. Now I''ve realized that my call to DrawIndexedPrimitive is only rendering the first two triangles in the list. I''m assuming it has something to do with the way triangles are lined up in the vertices and indices, but I''m in need of some help! I''ll paste my rendering code below. Basically, each object is a square consisting of two triangles (4 vertices and 6 indices). If I render with a COUNT_TO_RENDER of 1 (meaning I render every square (every 2 triangles), it renders fine, since the first square of each rendering works just fine...all further squares (pairs of triangles) are not seen. Do I need to insert "fake" triangles or the like in between the real triangles of the squares? The basic rendering loop is more or less as follows: int count = 0; int totalCount = 0; for (theIterator = m_VisibleRenderableObjects->begin(); theIterator != end; theIterator++) { // Add the vertices for the current RenderableObject // to our temporary vertice[] for (int i=0; i<4; i++) { vertexArray[count*4+i] = (*theIterator).m_Vertices; } // Same for indices for (i=0; i<6; i++) { indexArray[count*6+i] = (*theIterator).m_Indices[i]; } count++; totalCount++; // Render every so often if (count == COUNT_TO_RENDER) { // Vertices VOID* pVertices; if( FAILED( (*g_VertexBuffer).Lock( 0, sizeof (D3DVERTEX)*count*4, (BYTE**)&pVertices, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE) ) ) { return E_FAIL; } memcpy( pVertices, vertexArray, sizeof(D3DVERTEX)*count*4 ); (*g_VertexBuffer).Unlock(); // Indices VOID* pIndices; if( FAILED( (*g_IndexBuffer).Lock( 0, // Fill from start of the buffer. sizeof(indexArray), // Size of the data to load. (BYTE**)&pIndices, // Returned index data. D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE) ) ) // Send default flags to the lock. return E_FAIL; memcpy( pIndices, indexArray, sizeof(WORD)*count*6 ); g_IndexBuffer->Unlock(); m_pd3dDevice->SetStreamSource( 0, g_VertexBuffer, sizeof(D3DVERTEX) ); m_pd3dDevice->SetIndices( g_IndexBuffer, 0L ); if( FAILED(m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,count*4, 0, count*2))) { OutputDebugString("Failed DrawIndexedPrimitive\n"); } } Thanks! Onnel

Share this post


Link to post
Share on other sites
Advertisement
As a test, set D3DRS_CULLMODE to D3DCULL_NONE. If the triangles appear, you have a problem with creating your triangles. Just rearange the order you create them in, and turn culling on again.

Z.

Share this post


Link to post
Share on other sites
quote:
Original post by Zaei
As a test, set D3DRS_CULLMODE to D3DCULL_NONE. If the triangles appear, you have a problem with creating your triangles. Just rearange the order you create them in, and turn culling on again.



No, sadly that wasn't it. I didn't expect it was as the indices and vertices are the same whether they are rendered in groups of two triangles (where everything renders correctly) or in groups of ten (where only the first two triangles render).

Maybe it's something about the way indices get shared when switching from the first square of two triangles to the second...

I know in the INVIDIA pp presentation on using vertex and index buggers, there was one slide (which I honestly didn't fully understand) on the need to create" fake" triangles to connect non-contiguous triangles which are being rendered. Could this be my problem? Can anyone explain?



Edited by - onnel on July 21, 2001 3:15:25 AM

Share this post


Link to post
Share on other sites
I figured it out! Dumb stuff, but after staring at it for ages and reevaluating the logic of how it should work, I found the problem:

quote:
Original post by onnel
// Add the vertices for the current RenderableObject
// to our temporary vertice[]
for (int i=0; i<4; i++) {
vertexArray[count*4+i] = (*theIterator).m_Vertices;
}
// Same for indices
for (i=0; i<6; i++) {
indexArray[count*6+i] = (*theIterator).m_Indices[i];
}



The setting of the index arrays needs to be indented into the vertice list. Otherwise, all I was doing was redrawing with the same first 4 vertices over and over. m_Indices for each object is simply an array of the numbers 0 to 3 indicating which LOCAL vertice it will use. Since the vertices and the indices are both being put into arrays, we need to make sure the indexes pointed to by the index values are offset correctly into the vertex array. Hopefully this makes sense...

So:
indexArray[count*6+i] = (*theIterator).m_Indices[i];

should have been:

indexArray[count*6+i] = (count*4) +(*theIterator).m_Indices[i];

Remember, I'm working with squares of two triangles, which is why I have 4 vertices and 6 indices per object.


Hopefully this dumb mistake and posting can help other less experienced people figure out how to make this work. It was actually good, as I was forced to step back and think about what my code was really doing.

Thanks for the feedback as always!

Onnel


Edited by - onnel on July 21, 2001 7:24:44 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!