static bool BoundingBoxInitialized = false; static twTriple BBCenter; // the center point of the bounding box. static twTriple BBMin; // the min corner static twTriple BBMax; // the max corner static GLfloat OuterRadius; // radius of the bounding sphere static GLfloat InnerRadius; // radius of the inner sphere GLfloat min(GLfloat x, GLfloat y) { return x < y ? x : y; } /* computes near and far using bounding box coordinates */ void twBoundingBox(GLfloat xmin, GLfloat xmax, GLfloat ymin, GLfloat ymax, GLfloat zmin, GLfloat zmax) { if( xmin > xmax ) printf("xmax (%f) must be greater than or equal to xmin (%f)\n",xmax,xmin); if( ymin > ymax ) printf("ymax (%f) must be greater than or equal to ymin (%f)\n",ymax,ymin); if( zmin > zmax ) printf("zmax (%f) must be greater than or equal to zmin (%f)\n",zmax,zmin); // store the box twTripleInit(BBMin,xmin,ymin,zmin); twTripleInit(BBMax,xmax,ymax,zmax); twTripleInit(BBCenter,(xmax+xmin)*0.5,(ymax+ymin)*0.5,(zmax+zmin)*0.5); OuterRadius = twPointDistance(BBMin,BBMax)*0.5; InnerRadius = min(min(xmax-xmin,ymax-ymin),zmax-zmin)*0.5; BoundingBoxInitialized = true; if(TW_BOUNDING_BOX & twMessageKinds) { twTriplePrint("BB Center",BBCenter); twTriplePrint("BB Min",BBMin); twTriplePrint("BB Max",BBMax); printf("Outer radius = %f, inner = %f\n",OuterRadius,InnerRadius); } } /* Takes the student's array va with n points and computes the bounding box, then calls twBoundingBox. */ void twVertexArray(twTriple va[], int n) { int i; GLfloat minx = va[0][0]; GLfloat maxx = va[0][0]; GLfloat miny = va[0][1]; GLfloat maxy = va[0][1]; GLfloat minz = va[0][2]; GLfloat maxz = va[0][2]; for(i=1; i maxx) maxx = va[i][0]; if (va[i][1] < miny) miny = va[i][1]; if (va[i][1] > maxy) maxy = va[i][1]; if (va[i][2] < minz) minz = va[i][2]; if (va[i][2] > maxz) maxz = va[i][2]; } twBoundingBox(minx, maxx, miny, maxy, minz, maxz); } void twDrawBoundingBox() { twError(); glPushAttrib(GL_ALL_ATTRIB_BITS); glDisable(GL_LIGHTING); glLineWidth(1); glBegin(GL_LINES); glColor3f(1,0,0); // red for lines parallel to x glVertex3f(BBMin[0],BBMin[1],BBMin[2]); glVertex3f(BBMax[0],BBMin[1],BBMin[2]); glVertex3f(BBMin[0],BBMax[1],BBMin[2]); glVertex3f(BBMax[0],BBMax[1],BBMin[2]); glVertex3f(BBMin[0],BBMin[1],BBMax[2]); glVertex3f(BBMax[0],BBMin[1],BBMax[2]); glVertex3f(BBMin[0],BBMax[1],BBMax[2]); glVertex3f(BBMax[0],BBMax[1],BBMax[2]); glColor3f(0,1,0); // green for lines parallel to y glVertex3f(BBMin[0],BBMin[1],BBMin[2]); glVertex3f(BBMin[0],BBMax[1],BBMin[2]); glVertex3f(BBMax[0],BBMin[1],BBMin[2]); glVertex3f(BBMax[0],BBMax[1],BBMin[2]); glVertex3f(BBMin[0],BBMin[1],BBMax[2]); glVertex3f(BBMin[0],BBMax[1],BBMax[2]); glVertex3f(BBMax[0],BBMin[1],BBMax[2]); glVertex3f(BBMax[0],BBMax[1],BBMax[2]); glColor3f(0,0,1); // blue for lines parallel to z glVertex3f(BBMin[0],BBMin[1],BBMin[2]); glVertex3f(BBMin[0],BBMin[1],BBMax[2]); glVertex3f(BBMax[0],BBMin[1],BBMin[2]); glVertex3f(BBMax[0],BBMin[1],BBMax[2]); glVertex3f(BBMin[0],BBMax[1],BBMin[2]); glVertex3f(BBMin[0],BBMax[1],BBMax[2]); glVertex3f(BBMax[0],BBMax[1],BBMin[2]); glVertex3f(BBMax[0],BBMax[1],BBMax[2]); glEnd(); glPopAttrib(); twError(); }