Multiple objects w/ multiple index buffers, only one object at a time works?

Started by
0 comments, last by Foxman 20 years, 8 months ago
Here's the situation: I load up a model composed of multiple objects, and stuff all of the vertices into a single vertex buffer. I then create an index buffer for each object (after trying unsuccessfully to use a single index buffer for all 16 objects). The rendering of all the objects is done in a for loop, where I call SetIndices() to the corresponding index buffer, set the world position to that object's matrix, then call DrawIndexedPrimitive(). Everything works all fine and good until I try to render all of the objects. The first object in the hierarchy will be drawn correctly, and then everything else will look like its indices are all out of whack. However, if I change to code to draw only a single object from the hierarchy, any single one at all, it will draw perfectly. I tried experimenting with other object hierarchies and found that if I had a bunch of identical objects loaded (cubes in one case, cube/sphere things duplicated a bunch of times in another case), then everything worked fine, all objects rendered perfectly at the same time. But as soon as I added or removed some triangles from one of the spheroids, that particular object would get distorted... unless I only rendered that single object. Here's where I create the index buffers and the vertex buffer: // object is the overall entity, and contained within object.m_hierarchy[0-through-m_numObjects-1] are the individual pieces of geometry to be rendered. /////////////////////////////////////////////////////// BYTE *Ptr; int vtotal = 0; ib = new IDirect3DIndexBuffer8*[object.m_numObjects]; for (int i = 0; i < object.m_numObjects; ++i) { m_Graphics.m_pDevice->CreateIndexBuffer((object.m_hierarchy.m_numPolys * 3) * sizeof(short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &ib); // Copy indices into the index buffer. ib->Lock(0,0, (BYTE**)&Ptr, 0); memcpy(Ptr, &object.m_hierarchy.m_indices[0], (object.m_hierarchy.m_numPolys * 3) * sizeof(short)); ib->Unlock(); // Increment the total number of vertices vtotal += object.m_hierarchy.m_numVerts; } m_Graphics.m_pDevice->CreateVertexBuffer(sizeof(FVertex) * vtotal, D3DUSAGE_WRITEONLY, CUSTOM_VERTEX_FVF, D3DPOOL_DEFAULT, &m_pVB); vtotal = 0; for (i = 0; i < object.m_numObjects; ++i) { m_pVB->Lock(vtotal, object.m_hierarchy.m_numVerts * sizeof(FVertex), (BYTE**)&Ptr, 0); memcpy(Ptr, &object.m_hierarchy.m_vertices[0], (object.m_hierarchy.m_numVerts * sizeof(FVertex))); m_pVB->Unlock(); // Increment the offset by the number of vertices just copied. vtotal += object.m_hierarchy.m_numVerts * sizeof(FVertex); } And here's where I render the objects: //////////////////////////////////////////////////////// for (int i = 0; i < object.m_numObjects; ++i) { m_Graphics.m_pDevice->SetIndices(ib, 0); D3DXMATRIX a = *object.m_hierarchy.GetMatrix(); m_Graphics.m_pDevice->SetTransform(D3DTS_WORLD,object.m_hierarchy.GetMatrix() ); // Draw the vertex buffer m_Graphics.m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, object.m_hierarchy.m_numVerts, 0, object.m_hierarchy.m_numPolys); // <—– Draw the loaded vertices. } I've run through the debugger numerous times to make sure I haven't overwritten any sort of memory or create the wrong size vertex/index buffers, I've double checked my model loader many times (although that seems to work just fine, because each object can be rendered individually), and I just can't seem to figure out where I've gone wrong. Many, many thanks to anyone who can help shed some light &#111;n this issue. </i> <SPAN CLASS=editedby>[edited by - Foxman &#111;n August 14, 2003 4:31:29 AM]</SPAN>
Advertisement
It looks like you need to set the base vertex index before rendering the geometry...

Your original index data thinks that the vertices are numbered 0..N, but once you concantenate your vertices into a single buffer they are numbered M..M+N

For each index buffer, you need to store ''vtotal'' when you enter the first "copy the index data" loop. When you come to draw the geometry, use this as the base vertex index:

DWORD *baseVertex = new DWORD [object.m_numObjects];

// in loop to copy index data before vtotal is icremented
baseVertex = vtotal;

// in draw loop instead of current SetIndices call
m_Graphics.m_pDevice->SetIndices(ib, baseVertex);<br> </i>

This topic is closed to new replies.

Advertisement