Advertisement Jump to content
Sign in to follow this  

D3DXMesh for many very small models?

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

For my 4E4 RTS entry I'm planning to use .X format models to reduce custom loading/rendering code that I write to a minimum. The default behaviour seems to be that each instance of a mesh has its own VB & IB. This probably isn't optimal but for the most part I won't have 100s of units on-screen at once, and each one will have at least 100 polys which isn't TOO terrible for calling SetVB etc. However for level scnery like trees, there can potentially be 1000 onscreen at a time. I'm already planning them to be very simple (under 20 polys each) but then I really could run into issues with setting VB/IB so many times. I see that I can Clone a mesh and opt to share the VB at least (doesn't seem to say about IB) but then I would still have to set a world transform matrix individually for each tree. What I'd like is that for very simple objects the vertex positions can be pre-transformed at loadtime, and when rendering all that happens is the vertex/index data is copied across to a large buffer and rendered in a few calls. But obviously I still want to use the D3DX .X support, so I'm wondering how to hack the Meshes to let me load with D3DX, but not render the way D3DX would do it normally... ...anyone done this or have some ideas?

Share this post

Link to post
Share on other sites
Maybe I misunderstood you, but here is the code to draw a mesh (NumAttributes is a DWORD and pAttributes is D3DXATTRIBUTERANGE*):

// when loading (bVShader is true if you are planning to use a vertex shader)

// optimize mesh
// retrieve attributes table
pMesh->GetAttributeTable ( NULL, &NumAttributes );
pAttributes = new D3DXATTRIBUTERANGE [NumAttributes];
pMesh->GetAttributeTable ( pAttributes, &NumAttributes );

// when drawing

// get buffers
IDirect3DVertexBuffer9* pVB;
IDirect3DIndexBuffer9* pIB;
pMesh->GetVertexBuffer ( &pVB );
pMesh->GetIndexBuffer ( &pIB );
if ( bVShader )
device->SetFVF ( NULL );
device->SetFVF ( pD3DMesh->GetFVF () );
device->SetStreamSource ( 0, pVB, 0, pMesh->GetNumBytesPerVertex () );
device->SetIndices ( pIB );
for ( DWORD i = 0; i < NumAttributes; i++ )
device->SetMaterial ( pMaterials .MatD3D );
device->SetTexture ( 0, pTextures );

device->DrawIndexedPrimitive ( D3DPT_TRIANGLELIST, 0,
pAttributes .VertexStart,
pAttributes .VertexCount,
pAttributes .FaceStart * 3,
pAttributes .FaceCount );

Share this post

Link to post
Share on other sites
I don't know what that Attribute table is about, but my question basically was - how can I draw many meshes in the same DrawPrimitive call, rather than one by one? I can't tell if you answered that or not so I'll have to check that Attribute stuff...

Share this post

Link to post
Share on other sites
After you've loaded the D3DXMesh you can get the vertex/index buffers and move the data to your own VB for rendering (transforming as needed). D3DXMesh uses the triangle list form in it's VB/IB setup.

However, you might want to consider using instancing (VS 3.0, which works well in software, so you can still meet the 4E4 min. specs) if you are going to have a lot of the same object around your map. In that case you would use the D3DXMesh VB/IBs as is, and have an additional VB of matrices (one for each instance) in a second stream. You would set the first stream (the D3DXMesh one) to a higher frequency to match the number of matrices in the second stream, which is how instancing actually works.

Share this post

Link to post
Share on other sites
That's an interesting idea I didn't know was possible, but since I'm not experienced with shaders I don't think I'll try that route in the short timeframe available. I definitely should look at shaders properly in the future though!

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!