I've been working on my animated model class and i'm having an issue with calculating the AxisAlignedBoundingBox for the entire frame Hierachy.
I've got the bounding box being calculated upon loading the model, and it is working to an extent. The problem is the resulting box is too large for some reason.
The box is working in that it is the right shape and is adjusting itself as the model moves or rotates but its about 3-4x the size it should be.
The code calculating the Box is as follows,
This first part is at the end of the load function shortly after the call to D3DXLoadMeshHierarchyFromX()
Vector3 min = Vector3(0, 0, 0), max = Vector3(0, 0, 0);FrameCalculateBoundingBox(pFrameRoot, &min, &max);boundsBox.arrBoundsPoints[0] = Vector3(min.x, min.y, min.z);boundsBox.arrBoundsPoints[1] = Vector3(max.x, min.y, min.z);boundsBox.arrBoundsPoints[2] = Vector3(min.x, max.y, min.z);boundsBox.arrBoundsPoints[3] = Vector3(max.x, max.y, min.z);boundsBox.arrBoundsPoints[4] = Vector3(min.x, min.y, max.z);boundsBox.arrBoundsPoints[5] = Vector3(max.x, min.y, max.z);boundsBox.arrBoundsPoints[6] = Vector3(min.x, max.y, max.z);boundsBox.arrBoundsPoints[7] = Vector3(max.x, max.y, max.z);
The following is the function that calls its self recursivly to loop through the hieracrhy and get the min and max vertex positions to use for the BoundingBox.
void AnimatedMesh::FrameCalculateBoundingBox(LPD3DXFRAME frame, Vector3 *min, Vector3 *max){ LPD3DXMESHCONTAINER meshContainer = frame->pMeshContainer; // If this Frame contains a mesh... while (meshContainer) { BYTE* pData = NULL; meshContainer->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&pData); // Compute the Min and Max values for this frames mesh Vector3 tempMin, tempMax; D3DXComputeBoundingBox((Vector3*)pData, meshContainer->MeshData.pMesh->GetNumVertices(), D3DXGetFVFVertexSize(meshContainer->MeshData.pMesh->GetFVF()), &tempMin, &tempMax); meshContainer->MeshData.pMesh->UnlockVertexBuffer(); // If the min/max values are lower/greater then the current stored values, update them. min->x = min(min->x, tempMin.x); min->y = min(min->y, tempMin.y); min->z = min(min->z, tempMin.z); max->x = max(max->x, tempMax.x); max->y = max(max->y, tempMax.y); max->z = max(max->z, tempMax.z); meshContainer = meshContainer->pNextMeshContainer; } // Recurse for sibblings if (frame->pFrameSibling != NULL) FrameCalculateBoundingBox(frame->pFrameSibling, min, max); // Recurse for children if (frame->pFrameFirstChild != NULL) FrameCalculateBoundingBox(frame->pFrameFirstChild, min, max);}
(Just to note the above code isn't handling the axis alignment thats done every frame created from this initial box dependant on the rotation/scaling/positioning of the model in question.)
The bounding Sphere was easy enough with a call to D3DXFrameCalculateBoundingSphere()
However I'm fairly sure there is no such function implemented for Bounding Boxes (I could be wrong).
Thanks in advance for any help.
(edited post to place code in source brackets instead of code)
[Edited by - liamf1986 on October 28, 2010 11:49:46 AM]