   Mesh Polygon linked list
  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 intnNumberOfVertices; // 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    boolbIsLeaf;  // Is this a leaf node    boolbIsSolid; // 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 fontm_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 pointerBYTE *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 clonesif( ibdesc.Format == D3DFMT_INDEX32  )// index buffer is 32 bitnumIndices = ibdesc.Size / sizeof(DWORD);else// index buffer is 16 ibtnumIndices = 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);idxVbVert1 = ((DWORD*)lpIB)[ i+<span class="cpp-number">1</span> ];idxVbVert2 = ((DWORD*)lpIB)[ i+<span class="cpp-number">2</span> ];   }<span class="cpp-keyword">else</span>{idxVbVert0 = ((WORD*)lpIB);idxVbVert1 = ((WORD*)lpIB)[ i+<span class="cpp-number">1</span> ];idxVbVert2 = ((WORD*)lpIB)[ i+<span class="cpp-number">2</span> ];}<span class="cpp-comment">// lpVB is BYTE* and points to previously locked vertex data</span>D3DXVECTOR3 v0 = *(D3DXVECTOR3*) &amp;lpVB[ idxVbVert0 * numBytesPerVertex ];D3DXVECTOR3 v1 = *(D3DXVECTOR3*) &amp;lpVB[ idxVbVert1 * numBytesPerVertex ];D3DXVECTOR3 v2 = *(D3DXVECTOR3*) &amp;lpVB[ idxVbVert2 * numBytesPerVertex ];<span class="cpp-comment">//[ now use vetex coordinates v0, v1, v2 for u own purpose ]</span>    <span class="cpp-comment">// do collision detection against polygon or keep extracting them</span><span class="cpp-comment">// Fill array of vertices</span><span class="cpp-comment">//Should be like this, but requires also a copy operator for the VERTEX class. </span>VERTLIST[<span class="cpp-number">0</span>].x = v0.x;VERTLIST[<span class="cpp-number">0</span>].y = v0.y;VERTLIST[<span class="cpp-number">0</span>].z = v0.z;VERTLIST[<span class="cpp-number">1</span>].x = v1.x;VERTLIST[<span class="cpp-number">1</span>].y = v1.y;VERTLIST[<span class="cpp-number">1</span>].z = v1.z;VERTLIST[<span class="cpp-number">2</span>].x = v2.x;VERTLIST[<span class="cpp-number">2</span>].y = v2.y;VERTLIST[<span class="cpp-number">2</span>].z = v2.z;<span class="cpp-keyword">if</span>( g_polygonList == NULL )    {  g_polygonList = addPolygon(NULL, VERTLIST);  child = g_polygonList;    }  <span class="cpp-keyword">else</span>{  child = addPolygon(child, VERTLIST);    }}child-&gt;Next=NULL;<span class="cpp-comment">//end of linked list</span> pMeshSysMem-&gt;UnlockVertexBuffer();<span class="cpp-comment">//what was locked must be unlocked or else things i'll go bad</span>pMeshSysMem-&gt;UnlockIndexBuffer();    }    hr = OptimizeMeshData(pMeshSysMem, pAdjacencyBuffer, D3DXMESHOPT_VERTEXCACHE, &amp;m_MeshVertexCacheOptimized);    <span class="cpp-keyword">if</span> (FAILED(hr))   <span class="cpp-keyword">goto</span> End;End:    SAFE_RELEASE( pMeshSysMem );    SAFE_RELEASE( pAdjacencyBuffer );  <span class="cpp-keyword">return</span> hr;}[\source]</pre></div><!–ENDSCRIPT–> 
  Where do you get that error? Is it a D3D error, or an invalid index? If it's a D3D error, are you using the debug runtimes? If so, do they give any information? If not, why not [smile]? What line does it occur on?   ;Yes, could you run the debugger and tell where the program actually hangs.You could also use something like std::list or std::vector for collecting the triangles. Your linked list method looks a bit error prone.Just a question : is there a reason to have 4 sided polygons ?   ;  Actually, while running the code in debug mode it breaks in d3dx9math.inl (d3dx math inline function)D3DXINLINE D3DXVECTOR3D3DXVECTOR3::operator - ( CONST D3DXVECTOR3& v ) const{    return D3DXVECTOR3(x - v.x, y - v.y, z - v.z);}classifypolygon() function  calls  d3dx math inline function while substracting    D3DXVECTOR3 Direction = (*vec1) - (*vec2);Below is classify polygon function.//-----------------------------------------------------------------------------// Name : classifyPoly()// Desc : Classifies a polygon against the plane passed//-----------------------------------------------------------------------------long int My3DApplication::classifyPoly( POLYGON *Plane, POLYGON *Poly ){   double Infront = 0;    double Behind  = 0;    double OnPlane = 0;    float result;    D3DXVECTOR3 *vec1 = (D3DXVECTOR3*)&Plane->VertexList[0];    // Loop round each of the vertices    for( long int i = 0; i < Poly->nNumberOfVertices; ++i ) {   D3DXVECTOR3 *vec2= (D3DXVECTOR3 *)&Poly->VertexList;   D3DXVECTOR3 Direction = (*vec1) - (*vec2); result = D3DXVec3Dot(&Direction,&Plane->Normal);   // Tally up the position of each vertex   if( result > 0.001f )   ++Behind;   else if( result < -0.001f )   ++Infront;    else    {  ++OnPlane;  ++Infront;  ++Behind;   }    }    if( OnPlane == Poly->nNumberOfVertices )    return POINTPOSITION_FRONT;    if( Behind  == Poly->nNumberOfVertices )   return POINTPOSITION_BACK;    if( Infront == Poly->nNumberOfVertices )   return POINTPOSITION_FRONT;    return POINTPOSITION_SPANNING;};  one thing more:My g_polygonlist variable contains mesh polygon linked list.while reading this variable in debug modeg_polygonlist->vertexlist[0] = { x=426.12313  y = 72.23568   z = 0.00002112415}[1] = { x=0.0  y=0.0 z=0.0 }[2] = { x=0.0  y=0.0 z=0.0} why the second & third member of array shows zero values.   