#include #include #include #include //some methods you need to modify: void renderScene(void); void mouseMove(int x, int y); void readMesh(const char filename[100]); void computeMassCenter(float massCenter[3]); void computeNormal(int faceID, float norm[3]); //some methods you want to look at: void reshapeScene(GLsizei width, GLsizei height); void initGL(); void mouseClick(int button , int state, int x, int y); void keyBoard(unsigned char key, int x, int y); float ObjTranslation[3]={0,0,0}; float EyeTranslation[3]={0,0,-5}; //default eye position int win_width, win_height; int gButton; float whratio; int mouseX, mouseY; // Design whatever data structure you like as the point list std::vector ptlist[3]; std::vector facelist[3]; GLfloat rot_x = 0.f, rot_y = 0.f; ////////////////////////////////// void usage(void) { printf("This program is the first homework from XXX \r\n"); printf("The input is a .m triangle mesh. \r\n"); printf(" f - flat shading\r\n"); printf(" s - smooth shading\r\n"); printf(" w - wireframe\r\n"); printf(" key ESC - exit\r\n"); } int main(int argc, char **argv) { //1. The first thing you must do is call the function glutInit. It initializes GLUT library. glutInit(&argc, argv); //2. Define the display mode using the function glutInitDisplayMode(unsigned int mode) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); //3. Define the window //3.1. Establish the window's position, i.e. its top left corner. glutInitWindowPosition(100,100); //3.2. Choose the window size, using the function glutInitWindowSize(int width, int height). int width = 640; int height = 480; whratio = (float)width /(float)height; glutInitWindowSize(width, height); //3.3 After these settings, we can create the window with a title glutCreateWindow("XXX's Homework 1"); //4. Assign a function responsible for the rendering (register a callback) glutDisplayFunc(renderScene); //5. Assign a reshape function for the "resize" event received by the window. glutReshapeFunc(reshapeScene); //6. Assign functions for mouse and keyboard events respectively glutMouseFunc(mouseClick); glutMotionFunc(mouseMove); glutKeyboardFunc(keyBoard); //7. Some GL initialization initGL(); //8. Load the Mesh, //read the mesh , and store them into the vertex and face lists, argv[1] is the filename readMesh("torus.m"); //9. Finally, get in the application event processing loop glutMainLoop(); return 1; } /* Called when a "resize" event is received by the window. */ void reshapeScene(int width, int height) { win_width=width; win_height=height; GLsizei mwidth = (widthsize();++i) { int vertID[3]; float norm[3]; computeNormal(i,norm); glNormal3f(norm[0], norm[1], norm[2]); for (int j=0;j<3;++j) { vertID[j] = facelist[j][i]; float x = ptlist[0][vertID[j]]; float y = ptlist[1][vertID[j]]; float z = ptlist[2][vertID[j]]; glVertex3f(x,y,z); } } glEnd(); glutSwapBuffers(); } void readMesh(const char filename[100]) { ////Please modify the following //ptlist[0].push_back(1); //ptlist[1].push_back(1); //ptlist[2].push_back(0); //ptlist[0].push_back(0); //ptlist[1].push_back(-1); //ptlist[2].push_back(0); //ptlist[0].push_back(2); //ptlist[1].push_back(1); //ptlist[2].push_back(0); //facelist[0].push_back(0); //facelist[1].push_back(1); //facelist[2].push_back(2); ////Step 2: Read in a mesh FILE * fp = fopen( filename, "r" ); if( !fp ) { printf("Can't open file %s\n", filename ); return; } char line[1024]; int id; while( !feof(fp) ) { fgets( line, 1024, fp ); if( !strlen( line ) ) continue; char * str = strtok( line, " \r\n"); if (!str) continue; if( !strcmp(str, "Vertex" ) ) { str = strtok(NULL," \r\n{"); id = atoi( str ); for( int i = 0 ; i < 3; i ++ ) { str = strtok(NULL," \r\n{"); ptlist[i].push_back(atof( str )); } str = strtok( NULL, "\r\n"); if(!str) continue; std::string s(str); int sp = s.find("{"); int ep = s.find("}"); continue; } if( !strcmp(str,"Face") ) { str = strtok(NULL, " \r\n"); if (!str) continue; id = atoi( str ); int vid[3]; for( int i = 0; i < 3; i ++ ) { str = strtok(NULL," \r\n{"); vid[i] = atoi(str); facelist[i].push_back(vid[i]-1); } if (!str) continue; str = strtok( NULL, "\r\n"); if( !str || strlen( str ) == 0 ) continue; std::string s(str); int sp = s.find("{"); int ep = s.find("}"); continue; } } fclose(fp); //Step 4 float mass[3]; computeMassCenter(mass); //ObjTranslation[0]=-mass[0];ObjTranslation[1]=-mass[1];ObjTranslation[2]=-mass[2]; } void computeMassCenter(float massCenter[3]) { massCenter[0] = massCenter[1]=massCenter[2] =0; for (int i=0;i