BspNode * buildBspTree ( Arrays polys ) {
if ( polys.isEmpty () ) return NULL;
int index = findOptimalSplitter ( polys );
Polygon3D * splitter = (Polygon3D *) polys.at ( index );
BspNode * root = new BspNode ( splitter ) ,-
Polygon3D * frontPoly = NULL;
Polygon3D * backPoly = NULL;
Array front ( "Front Polys" );
Array back ( "Back Polys" );
BoundingBoxbox;
for ( int i = 0; i < polys.getNumltems (); i++ ) box.merge ( ( (Polygon3D *) polys.at ( i ) ) -> getBoundingBox () );
root -> setBoundingBox ( box );
splitter -> retain (); // since it'll be release'd
//in next stmt polys.removeAtlndex ( index ) ;
while ( polys.getCount () > 0 ) {
! Polygon3D * poly = (Polygon3D *) polys.at ( 0 );
' poly -> retain ();
polys . removeAtlndex ( 0 ) ,-
switch ( poly -> classify ( *splitter -> getPlane () ) ) {
case IN_PLANE: case IN_FRONT:
front.insert ( poly ) ;
break;
case IN_BACK:
back.insert ( poly ); break;
case IN_BOTH:
frontPoly = new Polygon3D ( poly -> getName (), MAX_VERTICES );
backPoly = new Polygon3D ( poly -> getName (), MAX_VERTICES );
poly -> split ( *splitter -> getPlane (), *frontPoly, *backPoly );
front.insert ( frontPoly ); back.insert ( backPoly );
frontPoly -> release (); backPoly -> release (); break;
}
poly -> release ();
}
root -> front = (front.getCount () > 0 ?
buildBspTree (front) : NULL ); root -> back = (back.getCount () > 0 ?
buildBspTree (back) : NULL );
return root;
}

Функция findOptimalSplitter выбирает грань, которая минимизирует число разбиений при использовании ее для проведения разбиений.

Пишем портальный рендерер (часть II)

int findOptimalSplitter ( const Arrays polys ) {
int bestSplitter = 0;
int bestNumSplits = polys.getCount () ;
for ( int i = 0; i < polys.getCount (); i++ ) {
Polygon3D * splitter = (Polygon3D *) polys.at ( i ) ; const Plane * plane = splitter -> getPlane ();

⇐ Предыдущая| |Следующая ⇒