Pushapjit
05-03-2006, 10:53 PM
I am doing collision detection using bSP tree. In this code i am passing linked list of mesh polygons to SelectBestSplitter() function. POLYGON PolyList contains linked list of polygons. this function is further calling ClassifyPOly() function. the debugger shows error in classifypoly () function. it shows poly as undefined value.
//-----------------------------------------------------------------------------
// Name : selectBestSplitter
// Desc : Selects the next best splitting plane from the poly linked list passed
//-----------------------------------------------------------------------------
POLYGON* CMyD3DApplication::selectBestSplitter( POLYGON *PolyList )
{
POLYGON *Splitter = PolyList;
POLYGON *CurrentPoly = PolyList;
POLYGON *SelectedPoly = NULL;
ULONG BestScore = 100000;
// Loop round all potential splitters
while( Splitter != NULL )
{
CurrentPoly = PolyList;
ULONG score = 0;
ULONG splits = 0;
ULONG backfaces = 0;
ULONG frontfaces = 0;
// Loop round all polys testing split counts etc
while( CurrentPoly != NULL )
{
if( CurrentPoly != Splitter )
{
int result = classifyPoly(CurrentPoly, Splitter);
switch( result )
{
case POINTPOSITION_ONPLANE:
break;
case POINTPOSITION_FRONT:
++frontfaces;
break;
case POINTPOSITION_BACK:
++backfaces;
break;
case POINTPOSITION_SPANNING:
++splits;
break;
default:
break;
}
}
CurrentPoly = CurrentPoly->Next;
}
// Calculate Score
score = abs( (int)(frontfaces-backfaces) ) + (splits*4);
// Compare scores
if( score < BestScore )
{
BestScore = score;
SelectedPoly = Splitter;
}
Splitter = Splitter->Next;
}
return SelectedPoly;
}
//-----------------------------------------------------------------------------
// Name : classifyPoly()
// Desc : Classifies a polygon against the plane passed
//-----------------------------------------------------------------------------
int CMyD3DApplication::classifyPoly( POLYGON *Poly, POLYGON *Plane )
{
D3DXVECTOR3 *vec1 = (D3DXVECTOR3*)&Plane->VertexList[0];
int Infront = 0;
int Behind = 0;
int OnPlane = 0;
float result;
// Loop round each of the vertices
for( 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;
}
//-----------------------------------------------------------------------------
// Name : selectBestSplitter
// Desc : Selects the next best splitting plane from the poly linked list passed
//-----------------------------------------------------------------------------
POLYGON* CMyD3DApplication::selectBestSplitter( POLYGON *PolyList )
{
POLYGON *Splitter = PolyList;
POLYGON *CurrentPoly = PolyList;
POLYGON *SelectedPoly = NULL;
ULONG BestScore = 100000;
// Loop round all potential splitters
while( Splitter != NULL )
{
CurrentPoly = PolyList;
ULONG score = 0;
ULONG splits = 0;
ULONG backfaces = 0;
ULONG frontfaces = 0;
// Loop round all polys testing split counts etc
while( CurrentPoly != NULL )
{
if( CurrentPoly != Splitter )
{
int result = classifyPoly(CurrentPoly, Splitter);
switch( result )
{
case POINTPOSITION_ONPLANE:
break;
case POINTPOSITION_FRONT:
++frontfaces;
break;
case POINTPOSITION_BACK:
++backfaces;
break;
case POINTPOSITION_SPANNING:
++splits;
break;
default:
break;
}
}
CurrentPoly = CurrentPoly->Next;
}
// Calculate Score
score = abs( (int)(frontfaces-backfaces) ) + (splits*4);
// Compare scores
if( score < BestScore )
{
BestScore = score;
SelectedPoly = Splitter;
}
Splitter = Splitter->Next;
}
return SelectedPoly;
}
//-----------------------------------------------------------------------------
// Name : classifyPoly()
// Desc : Classifies a polygon against the plane passed
//-----------------------------------------------------------------------------
int CMyD3DApplication::classifyPoly( POLYGON *Poly, POLYGON *Plane )
{
D3DXVECTOR3 *vec1 = (D3DXVECTOR3*)&Plane->VertexList[0];
int Infront = 0;
int Behind = 0;
int OnPlane = 0;
float result;
// Loop round each of the vertices
for( 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;
}