Archived

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

steg

Rendering lots of objects

Recommended Posts

Hi, I was wondering what is the best way of rendering lots of objects in d3d. 1. Should each object have it''s own DrawPrimitive method or should they be held in one big vertex list ? 2. Should each object be translated to world space then within BeginScene() : use SetStreamSource(nIndex....) where nIndex is which objects index ? then DrawPrimitive(......) ? I''m just a little confused how you would draw multiple objects as each object has its own world matrix ? My main rendering code looks like this (only for one object) : MakeWorldMatrix(&WorldMatrix, &Pyramid); g_pDevice->SetTransform( D3DTS_WORLD, &WorldMatrix ); camera.Update(); // view transformation g_pDevice->SetTransform( D3DTS_VIEW, &camera.GetView()); g_pDevice->BeginScene(); g_pDevice->SetStreamSource(0,g_pVB, sizeof(ZENVERTEX)); g_pDevice->SetVertexShader(CUSTOM_ZENVERTEX); g_pDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 4); g_pDevice->EndScene(); g_pDevice->Present(NULL,NULL,NULL,NULL); Where MakeWorldMatrix takes a pointer to the object being rendered, in this case a Pyramid object - note CObject is the base class and has nothing to do with MFC! void CWorldSpace::MakeWorldMatrix(D3DXMATRIX* pMatWorld, CObject* pObject) { D3DXMATRIX MatTemp; D3DXMATRIX MatRot; D3DXMatrixTranslation(pMatWorld, pObject->m_XPos, pObject->m_YPos, pObject->m_ZPos); D3DXMatrixIdentity(&MatRot); D3DXMatrixRotationX(&MatTemp, pObject->m_Pitch); D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationY(&MatTemp, pObject->m_Yaw); D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationZ(&MatTemp, pObject->m_Roll); D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixMultiply(pMatWorld, &MatRot, pMatWorld); } As can be seen, this will render one object, I want to render n amount of objects each with it''s own world transformation. Hope I have not made this sound confusing ? Any help is very much appreciated. Kind Regards, sTeVe

Share this post


Link to post
Share on other sites
BeginScene()
SetWorldMatrix(ForFirstObject)
DrawFirstObject()
SetWorldMatrix(ForSecondObject)
DrawSecondObject()
SetWorldMatrix(ForThirdObject)
DrawThirdObject()

etc. etc.

Don''t think of the world matrix as "The World!!!", think of it as "Where/how in the world is the following object". You can set the matrix as often as you need to.

Share this post


Link to post
Share on other sites
Thanks CrazedGenius,

Just one thought though, should all the objects be in one big vertex buffer i.e. when I''m calling :

m_pDevice->CreateVertexBuffer(.....)

Or should I do this for each object ?

Many thanks,
sTeVe

Share this post


Link to post
Share on other sites
G''day!

If the objects all have the same vertex format, then if they''re all in the same VB you don''t have to keep switching VBs and that is a performance gain.

BTW, the Zen idea of creating a vertex format that supports every FVF component is a performance loss. In general, if your object doesn''t need a FVF component, don''t use it. Though it does make VB management easy since all objects can theoretically be stored in a single VB.


Stay Casual,

Ken
Drunken Hyena

Share this post


Link to post
Share on other sites
Thanks Ken,

Also thanks for a cool site, I''ve learnt a lot from the tutorials there :-)

sTeVe

Share this post


Link to post
Share on other sites
Ken - your point about switching FVFs could possibly be untrue in some cases. Switching FVFs (SetVertexShader) incurs some cost as well. There may be cases where adding an extra member to an FVF *may* be cheaper than continually resetting FVFs. This sort of optimization would be very dependent on the exact circumstances...

Share this post


Link to post
Share on other sites
quote:
Original post by CrazedGenius
Ken - your point about switching FVFs could possibly be untrue in some cases. Switching FVFs (SetVertexShader) incurs some cost as well. There may be cases where adding an extra member to an FVF *may* be cheaper than continually resetting FVFs. This sort of optimization would be very dependent on the exact circumstances...


That''s true, I was referring only to the extreme case of including everything you might need in a single FVF. The cases where that would be a win (in terms of performance) are pretty rare.






Stay Casual,

Ken
Drunken Hyena

Share this post


Link to post
Share on other sites