Skip to main content
gamedev.net
🔒 Locked

D3DXComputeBoundingBox

Started by GameDev135 Jan 7, 2002 at 6:00 PM 4 replies 550+ views
Original Post
GameDev135
GameDev135
I have been trying to compute the bounding box for a mesh. However, whenever I run it, it crashes and the debugger tells me I have a "First-chance exception in game.exe: 0xC0000005:" Access Violation.. I think somehow I am dereferencing a null pointer, but I''m not sure...Anyway I have been looking through the SDK files for about 4 hours and tried many different ways, yet none of them work. I have tried locking, unlocking, the vertex buffer, and many other things. What am I doing wrong? What is the order that I have to do things in (I.E. does the vertex buffer have to be locked when I use the computeboundingbox function) I also think a possible problem is with the FVF flag. Anyway, here is the code. I am desperate for any suggestions at this point. (sorry if it is hard to read) void CLevelObject::StoreBoundingBox() { //create a vertex buffer LPDIRECT3DVERTEXBUFFER8 vertexBuffer =0; BYTE *pData; DWORD numVertices=m_ObjectMesh.m_pMesh->GetNumVertices(); //store the number of vertices HRESULT r; r= m_ObjectMesh.m_pMesh->GetVertexBuffer(&vertexBuffer); if (FAILED(r)) SetError("Unable to get vertex buffer"); /* GetVertexBuffer() at this point gives a succeed message */ r = m_ObjectMesh.m_pMesh->LockVertexBuffer(D3DLOCK_READONLY,&pData); if (FAILED(r)) SetError("Unable to lock vertex buffer"); // this also gives a succeed message r=D3DXComputeBoundingBox(&vertexBuffer, numVertices, CUSTOMVERTEX_TYPE, &m_minCoords,&m_maxCoords); /* This is CUSTOMVERTEX_TYPE: #define CUSTOMVERTEX_TYPE (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1 ) */ if (FAILED(r)) SetError("im sure the problem is here with bounding box"); /* this message never shows up (neither does a succeed message though) because the program has already crashed at this point */ r = m_ObjectMesh.m_pMesh->UnlockVertexBuffer(); if (FAILED(r)) SetError("Unable to unlock vertex buffer"); /* this gives a succeeded message also */ } Any suggestions would be appreciated.
Feel free to email me at NYYanks432@hotmail.com if you have any questions
AdmiralBinary
AdmiralBinary
I dunno if this is what u need, but this works for me:

  
LPDIRECT3DVERTEXBUFFER8 buf;
D3DXVECTOR3 vMin, vMax;
DWORD numVertices = mMesh->GetNumVertices();
BYTE * byteBuffer = new BYTE[numVertices];
DWORD fvf = mMesh->GetFVF();

mMesh->GetVertexBuffer(&buf);
buf->Lock(0,0,&byteBuffer,D3DLOCK_READONLY);
D3DXComputeBoundingBox((PVOID)byteBuffer,numVertices,fvf,&vMin,&vMax);

rBound.fMinX = vMin.x;
rBound.fMinY = vMin.y;
rBound.fMinZ = vMin.z;

rBound.fMaxX = vMax.x;
rBound.fMaxY = vMax.y;
rBound.fMaxZ = vMax.z;

buf->Unlock();

delete [] byteBuffer;


Hope it helps...

P.S. It was ripped straight from my source, but it has the stuff u want in it i think

---------------

I finally got it all together...
...and then forgot where I put it.
Gorky
Gorky
A few things, perhaps.

the variables named:&m_minCoords,&m_maxCoords are able to be written too right?

The number of verts is right, when you debug?
Are you sure the FVF matches the mesh?

Instead of FAILED()
Try to use the DX equivs.

Edited by - Gorky on January 7, 2002 7:47:08 PM
GameDev135
GameDev135
Ok, I copied the code from AdmiralBinary (btw, it is exactly the type of code I wanted) and only changed the names of the variables. However, I still get an error. This time, however, it is a message box saying that there is an assertion error. After looking it up to see what it means, Gorky''s theory about m_minCoords and m_MaxCoords being unwriteable makes sense. But what could cause them to do that? They are public variables.

Additionally, using Admiral Binary''s code, the assertion error comes in the call to lock the vertex buffer. (that is the first line that needs to be commented out.) I am wondering about the D3DLOCK_READONLY flag, since it is an assertion error, but it does seem as if it is only reading it-- the vertex buffer is an "in" argument.

Finally, when I run it in the release version, it does not crash. However, I have not tested to see if the code actually works and either way I would like to solve this problem.

I can show you the constructor of the class as well, I doubt that will help, but here is the whole class as it is now:

class CLevelObject {
public:
CLevelObject();
void StoreBoundingBox();
CMesh m_ObjectMesh;
CFrame m_ObjectFrame;
D3DXVECTOR3 m_minCoords;
D3DXVECTOR3 m_maxCoords;

};
CLevelObject::CLevelObject() {
m_minCoords.x=0;
m_minCoords.y=0;
m_minCoords.z =0;
m_maxCoords.x=0;
m_maxCoords.y=0;
m_maxCoords.z =0;
/*I''m not sure about initializing these or not, i have tried it both ways and neither works, so im guessing it doesnt make much difference */
}

//stores in m_minCoords and m_maxCoords vectors with the
//min and max vertices of the object/mesh
void CLevelObject::StoreBoundingBox() {
//create a vertex buffer

LPDIRECT3DVERTEXBUFFER8 buf;
D3DXVECTOR3 vMin, vMax;
DWORD numVertices = m_ObjectMesh.m_pMesh->GetNumVertices();
char string[5];
ZeroMemory( &string, sizeof( string ) );
/* Convert the number of vertices to a string (for debugging)*/
itoa( numVertices, string, 10 );
SetError("The number of vertices is :");
SetError(string);
BYTE * byteBuffer = new BYTE[numVertices];
DWORD fvf = m_ObjectMesh.m_pMesh->GetFVF();
m_ObjectMesh.m_pMesh->GetVertexBuffer(&buf);
buf->Lock(0,0,&byteBuffer,D3DLOCK_READONLY);
D3DXComputeBoundingBox((PVOID)byteBuffer,numVertices,fvf,&vMin,&vMax);
buf->Unlock();
delete [] byteBuffer;
}


Thanks for your help so far, hopefully we can now fix the problem completely.
Feel free to email me at NYYanks432@hotmail.com if you have any questions
Gorky
Gorky
A vertice is bigger than a byte!

BYTE * byteBuffer = new BYTE[numVertices];


Make it bigger
GameDev135
GameDev135
Ok, now it works properly. After correcting the change that Gorky pointed out, all i had to do was delete the line which is
delete [] byteBuffer;

I guess I need to actually move that line to the Game_Shutdown() function.

Thanks for all your help!

Edited by - GameDev135 on January 8, 2002 5:43:06 PM
Feel free to email me at NYYanks432@hotmail.com if you have any questions

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.