Archived

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

Foxman

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

Recommended Posts

Foxman    122
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[i]); // Copy indices into the index buffer. ib[i]->Lock(0,0, (BYTE**)&Ptr, 0); memcpy(Ptr, &object.m_hierarchy[i].m_indices[0], (object.m_hierarchy[i].m_numPolys * 3) * sizeof(short)); ib[i]->Unlock(); // Increment the total number of vertices vtotal += object.m_hierarchy[i].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[i].m_numVerts * sizeof(FVertex), (BYTE**)&Ptr, 0); memcpy(Ptr, &object.m_hierarchy[i].m_vertices[0], (object.m_hierarchy[i].m_numVerts * sizeof(FVertex))); m_pVB->Unlock(); // Increment the offset by the number of vertices just copied. vtotal += object.m_hierarchy[i].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[i], 0); D3DXMATRIX a = *object.m_hierarchy[i].GetMatrix(); m_Graphics.m_pDevice->SetTransform(D3DTS_WORLD,object.m_hierarchy[i].GetMatrix() ); // Draw the vertex buffer m_Graphics.m_pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, object.m_hierarchy[i].m_numVerts, 0, object.m_hierarchy[i].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 on this issue. [edited by - Foxman on August 14, 2003 4:31:29 AM]

Share this post


Link to post
Share on other sites
Osc    122
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[i], baseVertex[i]);

Share this post


Link to post
Share on other sites