/* Displays an OBJ using the glm library call from Nate Robbins's library. The object's bounding box is computed and used to set up the camera. Scott D. Anderson Fall 2006 */ #include #include #include #include #include char* filename; // the filename for the OBJ file /* This function both initializes and draws the model. It initializes the model if it hasn't already been initialized. This allows us to package everything up into just one function, which is convenient. It means that the first time the function is called it will be quite long, but we'd have to pay that price sometime anyhow. */ void drawModel(char* obj_filename, boolean printBB) { // The following variable must be declared static, so that it doesn't // get reset to NULL every time the function is called, since this is // how we avoid reloading the OBJ file every single time. static GLMmodel* pmodel = NULL; if(NULL == pmodel) { pmodel = glmReadOBJ(obj_filename); if (!pmodel) { fprintf(stderr,"error reading OBJ file %s\n",obj_filename); return; } if(printBB) { GLfloat BoundingBox[6]; // [minx,maxx,miny,maxy,minz,maxz] glmBoundingBox(pmodel,BoundingBox); printf("Bounding Box is (%f,%f,%f,%f,%f,%f)\n", BoundingBox[0], BoundingBox[1], BoundingBox[2], BoundingBox[3], BoundingBox[4], BoundingBox[5]); } glmFacetNormals(pmodel); glmVertexNormals(pmodel, 90.0); } glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL ); } void display(void) { twDisplayInit(); twCamera(); glPushAttrib(GL_ALL_ATTRIB_BITS); GLfloat pos[] = { 0.0, 0.0, 1.0, 0.0 }; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, pos); // define a default material, in case the object doesn't twTriple darkGray = {0.5, 0.5, 0.5}; twColor(darkGray,0.9,20); // draw the object drawModel(filename,true); glPopAttrib(); glFlush(); glutSwapBuffers(); } int main(int argc, char* argv[]) { char buf[256]; if( argc <= 1 ) { printf("What OBJ file to load? "); scanf(buf,sizeof(buf),stdin); filename = buf; } else { filename = argv[1]; } /* Ready to start doing OpenGL */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twBoundingBox(BoundingBox[0],BoundingBox[1], BoundingBox[2],BoundingBox[3], BoundingBox[4],BoundingBox[5]); twMainInit(); glutMainLoop(); return (0); }