Original Post
Following is some code from a "live" mesh exporter I am writing to take the vertex information from objects in my 3D modelling software and copy that into my DX8.1 scene. Everything compiles with no warnings or errors, but upon running it, I get Windows memory access violations at seemingly random places. I know it has to be mis-allocated pointers somewhere in it, but I have looked through it for several hours and tried changing a little here and a little there with no success. If some of you experienced C++ programmers would not mind looking through this, perhaps it will be easy to spot for someone who does not have his head buried in this project.
[edited by - MatthewB on December 25, 2002 9:34:24 AM]
// ***** From the top of the file ******
//
// A structure for our custom vertex type
struct CUSTOMVERT
{
FLOAT x, y, z; // The untransformed, 3D position for the vertex
DWORD color; // The vertex color
};
// Our custom FVF, which describes our custom vertex structure
#define MYFVF (D3DFVF_XYZ|D3DFVF_DIFFUSE)
LPDIRECT3DVERTEXBUFFER8 g_vertBufferArray;
LPDIRECT3DDEVICE8 OurDXDevice;
LPDIRECT3D8 OurDXInterface;
// **** Ok, now the "meat" of the class ****
//
HRESULT MyDXInterface::ImportGeometry()
{
CUSTOMVERT* imp_verts;
int icount = 0, iinnercount, numVerts;
HRESULT retval = E_FAIL;
CtsxVector3f *myVertices;
char message[255];
imp_verts = new CUSTOMVERT[];
numModels = 0;
tsxGNODE *pGNode = tsxSceneGetFirstNode();
//Loop through all the objects in the scene
for(; pGNode != NULL; pGNode = tsxGNodeGetNext(pGNode))
{
sprintf(message, "numModels = %d",numModels);
AfxMessageBox(message);
switch(tsxSobjGetType((tsxSOBJ*)pGNode))
{
case e_tsxPOLYHEDRON:
AfxMessageBox("Inside Case e_tsxPolyhedron");
break;
case e_tsxSUBDIV:
//maybe someday
continue;
break;
case e_tsxGROUP:
continue;
break;
default:
continue;
break;
}
//tsxPOLYHEDRON* myPoly = (tsxPOLYHEDRON*) pGNode;
//Get the number of vertices in this model.
numVerts = tsxPolyhGetNbrVxs((tsxPOLYHEDRON*)pGNode);
// Get the array of vertices
myVertices = tsxPolyhGetVxArray((tsxPOLYHEDRON*)pGNode);
//Loop through these vertices and assign the members
//of the imp_verts structure to the appropriate values.
for(iinnercount=0;iinnercount<numVerts;iinnercount++)
{
sprintf(message, "Inner loop # %d and newX = %6.3f",iinnercount, myVertices[iinnercount].x);
AfxMessageBox(message);
imp_verts[iinnercount].x = (float)myVertices[iinnercount].x;
imp_verts[iinnercount].y = (float)myVertices[iinnercount].y;
imp_verts[iinnercount].z = (float)myVertices[iinnercount].z;
//imp_verts[iinnercount].color = 0xBDF021;//Greenish-Yellow maybe?
}
//Place the completed vertex buffer into the g_vertBufferArray
/*if( FAILED( OurDXDevice->CreateVertexBuffer( numVerts*sizeof(CUSTOMVERT),
0, MYFVF,
D3DPOOL_DEFAULT, &g_vertBufferArray[numModels] ) ) )
{
return E_FAIL;
}*/
numModels++;
}
delete imp_verts;
return S_OK;
}
//
// **** Class Definition ****
//
class MyDXInterface
{
public:
HRESULT ImportGeometry();
void PaintDX();
void MainDX(HWND);
MyDXInterface();
virtual ~MyDXInterface();
void UninitDX();
int numModels;
private:
int InitDX();
HWND wHandle;
RECT OurDXClip;
};