Sign in to follow this  

Bounding Box applying non 0,0,0 offset

Recommended Posts

hey, I found this way to generate bounding boxes... but it did not calculate the fact that the mesh may not be centered around {0,0,0} so i decided to move the vertices after the fact.. i think the basic "idea" i wrote for the offset is ok but i know that The value i add to the m_pOBB verticies is not right.. it does not crash.. but it does not work right it actually moved maybe two of the m_pOBB vertecies and a bunch of the m_pMesh verticies.. any ideas? I guess i could find the point that the m_pMesh is centered arround and add the difference to each vertex .... how do you find the centerpoint of a mesh?
void WereMesh::GenerateBoundingBox(LPDIRECT3DDEVICE9 pD3dDevice)
	D3DXVECTOR3 min, max, boxMeshOffset ;
	BYTE* v;
	// lock mesh to compute boundingBox
	m_pMesh->LockVertexBuffer(0, (void**)&v);
	D3DXComputeBoundingBox((D3DXVECTOR3*)v, m_pMesh->GetNumVertices(), D3DXGetFVFVertexSize(m_pMesh->GetFVF()), &min, &max);
	// the offset from 0,0,0
	boxMeshSize = max - min;
	// creates box centered at 0,0,0
	D3DXCreateBox(pD3dDevice, boxMeshSize.x, boxMeshSize.y, boxMeshSize.z, &m_pOBB, /*addjacency??*/NULL); 
	// center around m_pMesh  ... m_pMesh may not be centered 0,0,0
	int at = 0, vertexSize = 0;
	m_pOBB->LockVertexBuffer(0, (void**)&v);
	vertexSize = D3DXGetFVFVertexSize(m_pOBB->GetFVF());
	for(UINT i = 0; i < m_pOBB->GetNumVertices(); i ++)
		at = i * vertexSize;
		((D3DXVECTOR3*)v)[at] += boxMeshSize;  // <-- not the right value I know

	// Optimize the mesh for better attribute access



Share this post

Link to post
Share on other sites
ok i have a little prob now...

when i lock m_OBB (is a bounding box :P ) created from D3DXCreateBox()) and try to change the vertices ::SEE BELOW:: in the loop the vPtr never has any data.. I dont get it?? note the m_OBB will render so it has data..

boxMeshOffset *= 0.5f // created above is D3DXVECTOR3
BYTE* ptr = NULL;
// lock the vertex buffer
if(FAILED(m_pOBB->LockVertexBuffer(0, (void**)&ptr)))

DWORD vertexSize = D3DXGetFVFVertexSize(m_pOBB->GetFVF());
DWORD numVertices = m_pOBB->GetNumVertices();

// Loop through each vertex and apply offset
for(DWORD i = 0; i < numVertices; i ++)
D3DXVECTOR3* vPtr = (D3DXVECTOR3*)ptr;
*vPtr += boxMeshOffset; <--- crashes here vPtr has no data

ptr += vertexSize;


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this