Original Post
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.