Click to See Complete Forum and Search --> : Bounding Boxes


mytakamineg240
December 8th, 2006, 01:33 PM
I am trying to build a simple game engine for a class. For this we need to create a bounding volume hierarchy and do some collision detection. Is there anywhere where I can get code for a simple hierarchy to use? Someone else in the project with me created a bounding box and tree class, but they do not function like they should. I'm looking for something a bit more plug and play.

Here is the code to draw the objects. Is there a way to modify this to build the bounding boxes and the hierarchy?


void CNextModel::drawModel( CNextRender* pRender )
{
VERTEX* vertex1;
MTL* mtl1;

//Create 3 arrays for vertex coordinates, texture and normals
float* vArr[3];
float* tArr[3];
float* nArr[3];

int m=0;
for( m=0; m<3;m++){

vArr[m]=new float[ARRAYSIZE];
tArr[m]=new float[ARRAYSIZE];
nArr[m]=new float[ARRAYSIZE];
}

for (int idx=0; idx < ARRAYSIZE; idx++){

vertex1 = (VERTEX*)pRender->pVertex[idx];
if( vertex1==0 ) break;

vArr[0][idx] = vertex1->x;
vArr[1][idx] = vertex1->y;
vArr[2][idx] = vertex1->z;
tArr[0][idx] = vertex1->u;
tArr[1][idx] = vertex1->v;
tArr[2][idx] = vertex1->w;
nArr[0][idx] = vertex1->nx;
nArr[1][idx] = vertex1->ny;
nArr[2][idx] = vertex1->nz;
}//for

//faces
int a,b,c;
int d,e,f;
int g,h,i;
int j,k,l;

char id[100];//Material ID

for (int idx=0; idx < ARRAYSIZE; idx++){

vertex1 = (VERTEX*)pRender->pVertex[idx];
if( vertex1 == 0 ) break;

strcpy( id, vertex1->id );

if( strcmp( id,"") != 0 ){
for( int idx1=0;idx1<MTLARRAYSIZE; idx1++ ){
mtl1 = (MTL*)pRender->pMtl[idx1];
if( mtl1 == 0 ) break;

if( strcmp( id , mtl1->id )==0){

//Use that RGB value for lighting
pRender->setAmbient( mtl1->a_x,mtl1->a_y,mtl1->a_z,0 );
pRender->setDiffuse( mtl1->d_x,mtl1->d_y,mtl1->d_z,0 );
pRender->setSpecular( mtl1->s_x,mtl1->s_y,mtl1->s_z,0 );

glLightfv(GL_LIGHT1, GL_AMBIENT, pRender->LightAmbient); // Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, pRender->LightDiffuse); // Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_SPECULAR, pRender->LightSpecular); // Setup The Specular Light

glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light
glEnable( GL_LIGHT1 );

//Bind the Texture
if( strcmp( mtl1->filename,"")!= 0 ){
glBindTexture( GL_TEXTURE_2D, pRender->texture[idx1]);
}

break;
}//if
}//for
}//if

a = vertex1->a;
b = vertex1->b;
c = vertex1->c;
d = vertex1->d;
e = vertex1->e;
f = vertex1->f;
g = vertex1->g;
h = vertex1->h;
i = vertex1->i;

j = vertex1->j;
k = vertex1->k;
l = vertex1->l;

//if face value is 0, break
if( a-1 < 0 ) break;

if( j-1 >= 0 ) glBegin( GL_QUADS );
else glBegin(GL_TRIANGLES);

if( a-1 >= 0 ){

if( c-1>=0)glNormal3d(nArr[0][c-1], nArr[1][c-1], nArr[2][c-1]);
if( b-1>=0)glTexCoord3d(tArr[0][b-1], tArr[1][b-1], tArr[2][b-1]);
glVertex3d(vArr[0][a-1], vArr[1][a-1], vArr[2][a-1]);
}

if( d-1 >= 0 ) {

if( f-1>=0)glNormal3d(nArr[0][f-1], nArr[1][f-1], nArr[2][f-1]);
if( e-1>=0)glTexCoord3d(tArr[0][e-1], tArr[1][e-1], tArr[2][e-1]);
glVertex3d(vArr[0][d-1], vArr[1][d-1], vArr[2][d-1]);
}
if( g-1 >= 0 ) {
if( i-1>=0)glNormal3d(nArr[0][i-1], nArr[1][i-1], nArr[2][i-1]);
if( h-1>=0)glTexCoord3d(tArr[0][h-1], tArr[1][h-1], tArr[2][h-1]);
glVertex3d(vArr[0][g-1], vArr[1][g-1], vArr[2][g-1]);
}

//#ifdef QUADS
if( j-1 >= 0 ) {
if( l-1>=0)glNormal3d(nArr[0][l-1], nArr[1][l-1], nArr[2][l-1]);
if( k-1>=0)glTexCoord3d(tArr[0][k-1], tArr[1][k-1], tArr[2][k-1]);
glVertex3d(vArr[0][j-1], vArr[1][j-1], vArr[2][j-1]);
}
//#endif
glEnd();

}//for

glEnable(GL_BLEND);

//Clean up

for( m=0; m<3;m++) { delete [] vArr[m]; vArr[m] = NULL; }
for( m=0; m<3;m++) { delete [] tArr[m]; vArr[m] = NULL; }
for( m=0; m<3;m++) { delete [] nArr[m]; vArr[m] = NULL; }

}


Thanks in advance.
Stephen