Sign in to follow this  
Pushapjit

Mesh Polygon linked list

Recommended Posts

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]


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites
Actually, while running the code in debug mode it breaks in d3dx9math.inl (d3dx math inline function)

D3DXINLINE D3DXVECTOR3
D3DXVECTOR3::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[i];
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;
}


Share this post


Link to post
Share on other sites
one thing more:

My g_polygonlist variable contains mesh polygon linked list.
while reading this variable in debug mode
g_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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this