Using D3DXComputeBoundingBox()

Started by
20 comments, last by Medo Mex 11 years, 3 months ago
Hi,

I have been trying to use D3DXComputeBoundingBox() to calculate a box width, length and depth.

I have tried the following code, but it's not working:



LPD3DXMESH mesh = GetBoxMesh();
BYTE* pData;
mesh->LockVertexBuffer( NULL, (LPVOID*)pData );

D3DXVECTOR3 vecMin;
D3DXVECTOR3 vecMax;
D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(), mesh->GetNumBytesPerVertex(), &vecMin, &vecMax);
float fWidth = vecMax.x - vecMin.x;
float fLength = vecMax.y - vecMin.y;
float fDepth = vecMax.z - vecMin.z;
mesh->UnlockVertexBuffer();



I'm not getting a valid values for (fWidth, fLength, fDepth), any idea what's wrong?
Advertisement
This is wrong:


BYTE* pData;
mesh->LockVertexBuffer( NULL, (LPVOID*)pData );


You're taking a value of type "BYTE*" and casting it to type "void**". In other words you're casting from a pointer to a double pointer. You need to take the address of your pointer and cast that to (void**).
I tried changing the code to:

BYTE *pData;
mesh->LockVertexBuffer( NULL, (void**)&pData );


I'm still not getting valid values for (fWidth, fLength, fDepth).
I even tried to create a box using D3DXCreateBox() and tried to retrieve the values, but can't get it to work:
LPD3DXMESH mesh;
D3DXCreateBox(d3ddev, 100.0f, 200.0f, 300.0f, &mesh, NULL);
BYTE *pData = NULL;
mesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&pData);

D3DXVECTOR3 vecMin;
D3DXVECTOR3 vecMax;
D3DXComputeBoundingBox((D3DXVECTOR3*)pData, mesh->GetNumVertices(), D3DXGetFVFVertexSize(mesh->GetFVF()), &vecMin, &vecMax);
mesh->UnlockVertexBuffer();
float fWidth = vecMax.x - vecMin.x;
float fLength = vecMax.y - vecMin.y;
float fDepth = vecMax.z - vecMin.z;



fWidth, fLength and fDepth are not 100, 200, 300 as given to D3DXCreateBox().
I notice you aren't telling the compute routine which part of the vertex buffer to get it's data from. A vertex buffer holds multiple items such as the vertices, UV coordinates, normals etc...

Instead of using a BYTE for the pData, use a structure matching your vertex layout such as the following...

struct VERTEX { D3DXVECTOR3 p; D3DXVECTOR3 n; FLOAT tu; FLOAT tv; static const DWORD FVF;};

VERTEX *pData;

In the D3DXComputeBoundingBox part change the (D3DXVECTOR3*)pData to &pData[0].p

Let me know if that worked, it works fine for me (in a slightly different way)...

******************************************************************************************
Youtube Channel

I got this problem resolved, but I have another problem that I have been trying to fix since yesterday.

I'm trying to create btBoxShape from the box information (boxWidth, boxHeight, boxDepth), but I see a larger box a result.


float boxWidth = (vecMax.x - vecMin.x);
float boxHeight = (vecMax.y - vecMin.y);
float boxDepth = (vecMax.z - vecMin.z);

btVector3 boxHalfExtents(boxWidth, boxHeight, boxDepth);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);

Forgive me if I am stating the obvious but...

You do realize that you are giving the boxHalfExtents, don't you? So the width of the box will be 2 * boxWidth, for example.

Oh, I want to mention that was a mistake since I wrote the code here instead of copying it.

Yes, I tried:

btVector3 boxHalfExtents(boxWidth*.5f, boxHeight*.5f, boxDepth*.5f);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);

I also tried:

btVector3 boxHalfExtents(boxWidth/2.0f, boxHeight/2.0f, boxDepth/2.0f);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);

Both are not working as expected, the box is larger than the real one.
I also want to mention that when I create a box in 3Ds Max with (Length, Width, Height) of 800 and export it to .x file then load it into DirectX 9 program, then I create btBoxShape by using 400 which is half 800 as the following:

btVector3 boxHalfExtents(400, 400, 400);
btBoxShape* boxShape = new btBoxShape(boxHalfExtents);

The physical box appears much larger than the rendered one, I am trying to get the physical box to be exactly the same size as the box that I create in 3Ds Max so I can have a box behaving like in physical world.

Well, all I can say is that it seems your physical world scale and renderer scale are out of sync. Are you sure you export with the correct units in 3DS Max? If I remember correctly Bullet uses the metric system.

This topic is closed to new replies.

Advertisement