Index buffers, drawindexedprimitive, etc . . .

Started by
12 comments, last by d000hg 18 years, 11 months ago
Quote:Original post by Coder
This is a big problem then. I wonder how I can make the indicies get sorted the same as the vertices. See if I sort the vertices by z value, indicies don't have a z value to sort by. I guess I can make a struct that contains the index and a z value, then sort the list of structs and then fill another list (of shorts) with the indicies (now sorted) and then memcpy the sorted indicies over the existing data in the index buffer.

This won't work. Consider the following vertices:
<0, 0, 20>
<0, 0, 2.5>
<0, 0, 15>

And the indices: 0, 1, 2

If you sort vertices by Z, and the indices by the Z of their indexed vertices you'd get:
<0, 0, 2.5>
<0, 0, 15>
<0, 0, 20>

Indices: 1, 2, 0

Which is not what you want. You want them to be "2, 0, 1", to preserve the winding (clockwise or anti-clockwise).

EDIT: Why do you want to z-sort the vertices, anyway? It makes sense to z-sort triangles/faces, but not vertices. Unless you're doing point sprites, but then you don't need index buffers for point sprites.

[Edited by - Coder on May 26, 2005 9:33:21 AM]

Advertisement
Quote:
You don't need memset. Lock the index buffer, overwrite the old data with the new index data, and unlock it.

Like this:
//fill index buffervoid *vb_indicies;g_pIndexBuffer->Lock(0,0,&vb_indicies,0);//lstIndexList is a vector list of WORDSmemcpy(vb_indicies,&(m_lstIndexList[0]),m_lstIndexList.size()*sizeof(WORD));g_pIndexBuffer->Unlock();

Would that overwrite the index buffer?

Quote:Why do you want to z-sort the vertices, anyway?

Currently all vertices are in a vector list. I sort them by z value so that things closer to 0 are in the front, etc (this is a 2d app) then I fill the vertex buffer with the sorted vertices.

HTML5, iOS and Android Game Development using Corona SDK and moai SDK

Quote:Original post by utilae
Like this:
*** Source Snippet Removed ***
Would that overwrite the index buffer?

Yes.

But you don't need to sort the vertices like that even if your logic is sound, becuase with indexing, it's the indices which will do the sorting. So leave the vertices, and then index themv in a way to draw triangles in Z-order if you must. Although I can't see whay you can't z-buffer...

This topic is closed to new replies.

Advertisement