Original Post
I am trying to generate .x file polygon linked list which will be further used for BSP tree. I am having undefined value error instead of polygon vertex value. follwing code shows the way i generated mesh's polygons linked list.
//-----------------------------------------------------------------------------
// SYMBOLIC CONSTANTS
// Enumerated Data type
//-----------------------------------------------------------------------------
enum PointPosition
{
POINTPOSITION_FRONT, // Point is positioned in front of plane
POINTPOSITION_BACK, // Point is positioned behind plane
POINTPOSITION_ONPLANE, // Point is positioned on plane
POINTPOSITION_SPANNING // Point is spanning plane
};
struct VERTEX // Vertex Structure
{
float x; // X Coordinate Component
float y; // Y Coordinate Component
float z; // Z Coordinate Component
};
struct POLYGON // Polygon Structure
{
VERTEX VertexList[4]; // Actual Vertex Data
D3DXVECTOR3 Normal; // Polygon Normal
long int nNumberOfVertices; // Number of Vertices
POLYGON *Next; // Linked List Next Poly
};
struct NODE // Node structure
{
POLYGON *Splitter; // Splitter poly for this node
NODE *Front; // Front Node Pointer
NODE *Back; // Back Node Pointer
bool bIsLeaf; // Is this a leaf node
bool bIsSolid; // Is this leaf node solid.
};
//----------------------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
// Builds polygons linked list (g_polygonList) according to what's stored in Mesh
//----------------------------------------------------------------------------------------
HRESULT My3DApplication::InitDeviceObjects()
{
HRESULT hr = S_OK;
LPD3DXMESH pMeshSysMem = NULL;
LPD3DXBUFFER pAdjacencyBuffer = NULL;
VERTEX VERTLIST[3];
POLYGON *child = NULL;
g_polygonList = NULL;
// Initialize the font
m_pFont->InitDeviceObjects( m_pd3dDevice );
hr = LoadMeshData(&pMeshSysMem, &pAdjacencyBuffer);
if (FAILED(hr))
{
// ignore load errors, just draw blank screen if mesh is invalid
hr = S_OK;
goto End;
}
// array of vertices is stored in vertex buffer.
// Lock vertex & index buffer
// pMeshSysMem is valid LPD3DXMESH pointer
BYTE *lpVB, *lpIB;
DWORD idxVbVert0, idxVbVert1, idxVbVert2;
long int numIndices;
if( pMeshSysMem->LockVertexBuffer( D3DLOCK_READONLY, (VOID**)&lpVB ) == D3D_OK )
if( pMeshSysMem->LockIndexBuffer ( D3DLOCK_READONLY, (VOID**)&lpIB ) == D3D_OK )
{
LPDIRECT3DINDEXBUFFER9 lpIndexBuffer;
pMeshSysMem->GetIndexBuffer( &lpIndexBuffer );
D3DINDEXBUFFER_DESC ibdesc;
lpIndexBuffer->GetDesc( &ibdesc );
// ibdesc.Size is Size of the index buffer, in bytes
// numIndices = sizeof(IB) / sizeof(INDICE); //sizeof(INDICE) must be either 2 or 4
// remember if the mesh is 32 or 16 bit, to be added in on the clones
if( ibdesc.Format == D3DFMT_INDEX32 )
// index buffer is 32 bit
numIndices = ibdesc.Size / sizeof(DWORD);
else
// index buffer is 16 ibt
numIndices = ibdesc.Size / sizeof(WORD);
DWORD numBytesPerVertex = pMeshSysMem->GetNumBytesPerVertex();
for( long int i=0; i<numIndices; i += 3 )
{
if (ibdesc.Format == D3DFMT_INDEX32 )
{
idxVbVert0 = ((DWORD*)lpIB)[ i ];
idxVbVert1 = ((DWORD*)lpIB)[ i+1 ];
idxVbVert2 = ((DWORD*)lpIB)[ i+2 ];
}
else{
idxVbVert0 = ((WORD*)lpIB)[ i ];
idxVbVert1 = ((WORD*)lpIB)[ i+1 ];
idxVbVert2 = ((WORD*)lpIB)[ i+2 ];
}
// lpVB is BYTE* and points to previously locked vertex data
D3DXVECTOR3 v0 = *(D3DXVECTOR3*) &lpVB[ idxVbVert0 * numBytesPerVertex ];
D3DXVECTOR3 v1 = *(D3DXVECTOR3*) &lpVB[ idxVbVert1 * numBytesPerVertex ];
D3DXVECTOR3 v2 = *(D3DXVECTOR3*) &lpVB[ idxVbVert2 * numBytesPerVertex ];
//[ now use vetex coordinates v0, v1, v2 for u own purpose ]
// do collision detection against polygon or keep extracting them
// Fill array of vertices
//Should be like this, but requires also a copy operator for the VERTEX class.
VERTLIST[0].x = v0.x;
VERTLIST[0].y = v0.y;
VERTLIST[0].z = v0.z;
VERTLIST[1].x = v1.x;
VERTLIST[1].y = v1.y;
VERTLIST[1].z = v1.z;
VERTLIST[2].x = v2.x;
VERTLIST[2].y = v2.y;
VERTLIST[2].z = v2.z;
if( g_polygonList == NULL )
{
g_polygonList = addPolygon(NULL, VERTLIST);
child = g_polygonList;
}
else
{
child = addPolygon(child, VERTLIST);
}
}
child->Next=NULL; //end of linked list
pMeshSysMem->UnlockVertexBuffer(); //what was locked must be unlocked or else things i'll go bad
pMeshSysMem->UnlockIndexBuffer();
}
hr = OptimizeMeshData(pMeshSysMem, pAdjacencyBuffer, D3DXMESHOPT_VERTEXCACHE, &m_MeshVertexCacheOptimized);
if (FAILED(hr))
goto End;
End:
SAFE_RELEASE( pMeshSysMem );
SAFE_RELEASE( pAdjacencyBuffer );
return hr;
}
[\source]