Archived

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

How to calculate Bounding Box from a frame Hierachy?

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

Hi, all I have an xfile containing a frame hierachy. How to calculate the Bbox of each frame in a better way? in SDK, i only find func of calculating BSphere of frame...

Share this post


Link to post
Share on other sites
I added a function to the CD3DMesh class
(this is straight from the culling example in the SDK)


HRESULT CD3DMesh::ComputeBoundingBox( LPDIRECT3DDEVICE9 pd3dDevice )
{
// Catch possible exception
if( m_pSysMemMesh == NULL )
return E_FAIL;

HRESULT hr;

// Create a mesh box to visualize the bounding box
if( m_pMeshBox == NULL )
{
if( FAILED( hr = D3DXCreateBox( pd3dDevice, 1.0f, 1.0f, 1.0f, &m_pMeshBox, NULL ) ) )
return SendConsole( "Failed to create a mesh to visualize a bounding box\n", hr );

// Compute normals of the mesh box
D3DXComputeNormals( m_pMeshBox, NULL );
}

// vecMin and vecMax hold the bounding box vectors (vecMin is the lower-left-front vector of the bounding box, and vecMax is the upper-right-back vector of the bounding box)
D3DXVECTOR3 vecMin;
D3DXVECTOR3 vecMax;

// Lock the vertex buffer
BYTE* pVertices;
if( FAILED( hr = m_pSysMemMesh->LockVertexBuffer( 0, (void**)&pVertices ) ) )
return E_FAIL; // add a message like "failed to lock vb"

// Here''s where we actually compute the bounding box (which just sets the values of vecMin and vecMax)
hr = D3DXComputeBoundingBox( (D3DXVECTOR3*)pVertices, m_dwNumVerts, m_dwFVFSize, &vecMin, &vecMax );
if( FAILED( hr ) )
SendConsole( "Failed to compute the bounding box of a mesh\n" );

// Unlock the vertex buffer
hr = m_pSysMemMesh->UnlockVertexBuffer();
if( FAILED( hr ) )
return SendConsole( "Failed to unlock the vertex buffer of a mesh\n" );

// Setup the bounding box matrix (used to position the bounding box)
D3DXMATRIXA16 matTrans;
D3DXMATRIXA16 matScale;
D3DXMatrixTranslation( &matTrans, (vecMin.x + vecMax.x) / 2, (vecMin.y + vecMax.y) / 2, (vecMin.z + vecMax.z) / 2 );
D3DXMatrixScaling( &matScale, vecMax.x - vecMin.x, vecMax.y - vecMin.y, vecMax.z - vecMin.z );
m_matBox = matScale * matTrans;

return S_OK;
}

Thats just for creating the bounding box, there is much more that you must do...Check the cull sample in the DXSDK samples
directory for more (like visualizing the bounding box, etc..)

Mike Cunningham (First year student)

Share this post


Link to post
Share on other sites
Not sure if this would work...But create the bounding box for each mesh and create a matrix to represent each bounding box.

Then multiply all the matrices of the frame into 1 matrix

i.e = matFrame = matBox1 * matBox2 * matBox3* matBox4, etc..


Sorry if this advice is totally wrong, but it might be worth a try

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i''ve done this and it''s not any easy thing to write out the steps necessary, but it basically involves traversing through the CD3DFile''s frame/mesh hierarchy in a manner similar to that used by the Render functions of the 3 mesh-related classes.

if you''d like i would be willing to post my code.

Share this post


Link to post
Share on other sites
Just recurse the frame hierarchy. On each frame, if it has a mesh container, use D3DXComputeBoundingBox(). If each mesh is in the same model space, it''s a simple matter of keeping track of the smallest/largest min and max values from each call. Basically, a union of all the bounding boxes you find.

So, you''re walking the tree, and you encounter mesh#1, whose bounding box is (-20, -30, -25 ), (20, 20, 40). Then you encounter mesh#2, whose bb is (-30, -10, -10), (15, 30, 45). Taking the largest of each min and max axis, you get (-30, -30, -25), (20, 30, 45). Just do that for each bb you find.

I like pie.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
but each mesh is not in the same model space. each mesh has its own frame of reference. and the hierarchy is developed as a tree (two links at each frame, one for the first child and one for the next sibling) so that must be considered as well.

Share this post


Link to post
Share on other sites
Then simply transform the min and max points to a common basis.

And having siblings and chlidren is just a matter of walking the tree.

void UpdateBB( D3DXFRAME * pFrame, D3DXVECTOR3 * pvMin, D3DXVECTOR3 * pvMax )
{
if( pFrame->pMeshContainer )
{
// TODO: get vb of mesh inside container
D3DXVECTOR3 vMin, vMax;
D3DXCalculateBoundingBox( pData, dwNumVx, dwStride, & vMin, & vMax );
// TODO: translate vMin and vMax into a common space, maybe world space

D3DXVec3Minimize( pvMin, pvMin, & vMin );
D3DXVec3Maximize( pvMax, pvMax, & vMax );
}

if( pFrame->pFrameSibling )
UpdateBB( pFrame->pFrameSibling, pvMin, pvMax );

if( pFrame->pFrameFirstChild )
UpdateBB( pFrame->pFrameFirstChild, pvMin, pvMax );
}


I like pie.

[edited by - RenderTarget on August 20, 2003 5:33:34 PM]

Share this post


Link to post
Share on other sites
But I thinks there''s still something wrong.
the min and max points of a bb will not be the same after rotating it.
So should I consider all the 8 points?
And if I use the recursive method above, should I calculate the combined matrix from the top frame to the mesh previously?
It seems inefficient...

Share this post


Link to post
Share on other sites
You''re going to have to pretend I don''t know how you''re using this. Because I don''t.

Of course if a mesh rotates you''ll have to readjust the bb. What I showed you gives you a bb for a mesh at the beginning; this is something that should probably be preprocessed anyway. If you want to recurse the tree every time you move something, you''ll have to do something different. Just handle it.

You have geometry. That has bounds. If it rotates, and your bb is axial, those bounds change. If it''s oriented, those bounds rotate with the mesh. What''s the question?

I like pie.

Share this post


Link to post
Share on other sites