Ffelagund

02-24-2004, 04:41 AM

Hello, I'm programming the bounding spheres of some meshes, but i'm having troubles. This is my method: First I find te bounding box of the mesh (i need it for later tasks), then, with the max and minimun corner of the BBox, i find the center( center.x = (minCorner.x+maxCorner.x)/2;

center.y = (minCorner.y+maxCorner.y)/2;

center.z = (minCorner.z+maxCorner.z)/2 http://www.opengl.org/discussion_boards/ubb/wink.gif

and i go thought all vertices of the mesh, checking the distace form the center to the current vertex, at the end I have the radius and the center of the sphere, but...

the mesh cut the sphere: www.telecable.es/personales/ffelagund/bbox.jpg (http://www.telecable.es/personales/ffelagund/bbox.jpg) www.telecable.es/personales/ffelagund/bbox3.jpg (http://www.telecable.es/personales/ffelagund/bbox3.jpg) www.telecable.es/personales/ffelagund/bbox2.jpg (http://www.telecable.es/personales/ffelagund/bbox2.jpg)

with the cilinder all work ok but with the other mesh doesn't.

Here the code:

//Find max and minus corner of the BBOX:

maxCorner.Assign(vertices[0],vertices[1],vertices[2]);

minCorner.Assign(vertices[0],vertices[1],vertices[2]);

for(j=0;i<nVertices;i++)

{

if(vertices[j]>maxCorner.x) maxCorner.x=vertices[j];

if(vertices[j]<minCorner.x) minCorner.x=vertices[j];

j++;

if(vertices[j]>maxCorner.y) maxCorner.y=vertices[j];

if(vertices[j]<minCorner.y) minCorner.y=vertices[j];

j++;

if(vertices[j]>maxCorner.z) maxCorner.z=vertices[j];

if(vertices[j]<minCorner.z) minCorner.z=vertices[j];

j++;

}

//find center of the sphere

center.x = (minCorner.x+maxCorner.x)/2;

center.y = (minCorner.y+maxCorner.y)/2;

center.z = (minCorner.z+maxCorner.z)/2;

//sphere render

void BSphere::Render()

{

glPushMatrix();

glEnable(GL_BLEND);

glBlendFunc(GL_ONE,GL_ONE);

GLUquadricObj *p = gluNewQuadric();

glTranslatef(center.x,center.y,center.z);

gluSphere(p,radius,30,30);

gluDeleteQuadric(p);

glDisable(GL_BLEND);

glPopMatrix();

}

If i comment the translatef of the sphere, the sphere of the spiral/capsule mesh appears centrated and in the right place (the origin) but the cilinder sphere appears in the same place, the origin, because the two spheres are drawed in the same place, but if I use the Translatef line, the cilinder sphere are OK, but the other sphere cuts the other mesh.

Can anyone tell me why?

thank you

center.y = (minCorner.y+maxCorner.y)/2;

center.z = (minCorner.z+maxCorner.z)/2 http://www.opengl.org/discussion_boards/ubb/wink.gif

and i go thought all vertices of the mesh, checking the distace form the center to the current vertex, at the end I have the radius and the center of the sphere, but...

the mesh cut the sphere: www.telecable.es/personales/ffelagund/bbox.jpg (http://www.telecable.es/personales/ffelagund/bbox.jpg) www.telecable.es/personales/ffelagund/bbox3.jpg (http://www.telecable.es/personales/ffelagund/bbox3.jpg) www.telecable.es/personales/ffelagund/bbox2.jpg (http://www.telecable.es/personales/ffelagund/bbox2.jpg)

with the cilinder all work ok but with the other mesh doesn't.

Here the code:

//Find max and minus corner of the BBOX:

maxCorner.Assign(vertices[0],vertices[1],vertices[2]);

minCorner.Assign(vertices[0],vertices[1],vertices[2]);

for(j=0;i<nVertices;i++)

{

if(vertices[j]>maxCorner.x) maxCorner.x=vertices[j];

if(vertices[j]<minCorner.x) minCorner.x=vertices[j];

j++;

if(vertices[j]>maxCorner.y) maxCorner.y=vertices[j];

if(vertices[j]<minCorner.y) minCorner.y=vertices[j];

j++;

if(vertices[j]>maxCorner.z) maxCorner.z=vertices[j];

if(vertices[j]<minCorner.z) minCorner.z=vertices[j];

j++;

}

//find center of the sphere

center.x = (minCorner.x+maxCorner.x)/2;

center.y = (minCorner.y+maxCorner.y)/2;

center.z = (minCorner.z+maxCorner.z)/2;

//sphere render

void BSphere::Render()

{

glPushMatrix();

glEnable(GL_BLEND);

glBlendFunc(GL_ONE,GL_ONE);

GLUquadricObj *p = gluNewQuadric();

glTranslatef(center.x,center.y,center.z);

gluSphere(p,radius,30,30);

gluDeleteQuadric(p);

glDisable(GL_BLEND);

glPopMatrix();

}

If i comment the translatef of the sphere, the sphere of the spiral/capsule mesh appears centrated and in the right place (the origin) but the cilinder sphere appears in the same place, the origin, because the two spheres are drawed in the same place, but if I use the Translatef line, the cilinder sphere are OK, but the other sphere cuts the other mesh.

Can anyone tell me why?

thank you