Originally Posted by
Bubba
What I said to do will work. When you load the mesh initially you are relying on ID3DXMesh's default vertex type which may not be the type you want. It is most likely not the type you want. To alleviate this you clone the mesh. You can get away with using the default type until you try to do more advanced things with the mesh like you are trying to do. All D3DXComputeBoundingSphere is doing is finding the min and max vertex positions, computing center off of that, and then computing radius from that as well. In order to do that it must have an array of D3DXVECTOR3 vectors so it can compute correctly. In order to get these you must provide a vertex type and stride.
You should take notice what D3DXComputeBoundingSphere does. It computes a bounding sphere for the 'mesh' not for a specific ID3DXMesh. In other words it has nothing to do with ID3DXMesh.
I need only provide the address of the vertex in the buffer to start computing with, the number of verteces to compute, and the size of each vertex. I can get all of this information directly from my mesh as follows (using code from Using the ID3DXMesh Class)...
Code:
void Mesh::getBoundingRadius(float* pBoundingRadius){
D3DXVECTOR3 center;
LPDIRECT3DVERTEXBUFFER9 pVBuf;//pointer to vertex buffer
if (SUCCEEDED(g_pMesh->GetVertexBuffer(&pVBuf))) {//get the address
D3DXVECTOR3* pVert;//pointer to first vertex in buffer
if (SUCCEEDED(pVBuf->Lock(0,0,(void **) &pVert,D3DLOCK_READONLY))) {
//compute bounding radius
D3DXComputeBoundingSphere(
pVert,
g_pMesh->GetNumVertices(),
g_pMesh->GetNumBytesPerVertex(),
¢er,
pBoundingRadius
);
//add the largest component of the center vect to the radius so the center can be 0 and the radius will encompas entire mesh
*pBoundingRadius+=max(max(abs(center.x),abs(center.y)),abs(center.z));
pVBuf->Unlock();
}else{
MessageBox( NULL, L"could not lock vertex buffer", L"getBoundingSphere", MB_OK );
}
}else{
MessageBox( NULL, L"error getting vertex buffer address", L"getBoundingSphere", MB_OK );
}
}
I've tried it and the above code works perfectly for getting the bounding radius (I'm not sure if the rest of my math is working though). An even better way would be to use the mesh's LockVertexBuffer method, which locks the vertex buffer and returns a pointer to it. I'll try that next.