#include #include #include #include float whratio; int win_height, win_width; GLUquadricObj *obj; /* Some variables to measure mouse movement */ int mousePositionX0 = 0, mousePositionY0 = 0; int mouseButton = 0; /* Some variables to describe the object dimension and camera placement */ float cameraPosition[3] = { 0, 0, 2 }; //default camera position float objCenter[3] = { 0, 0, 0 }; //default object center float boxMin[3] = { 0, 0, 0 }; float boxMax[3] = { 0, 0, 0 }; float axislen = 1.414; /* Some variables to control interactive transformations */ /* Some variables to store the triangular mesh*/ void MyInit(void); bool ReadOBJFile(const char filename[]) { std::cout << "Reading OBJ File " << filename << " ..."; std::ifstream input(filename); if (!input.good()){ std::cerr << "Can't open file " << filename << "!" << std::endl; return false; } while (input.good()){ std::string line; std::getline(input, line); if (line.empty()) continue; std::stringstream ss(line); std::string keyword; ss >> keyword; if (keyword == "v"){ //parsing a line of vertex element double x, y, z; ss >> x >> y >> z; //do something here } if (keyword == "f"){ //parsing a line of face element int v1id, v2id, v3id; ss >> v1 >> v2 >> v3; //do something here; NOTE that the index here starts with 1!! } } return false; // after you finish this, change the return value to true } void ComputeBoundingBox(); //rendering void SetCamera(void); void Render_BoxAndAxes(void); void Render_Mesh(void); //glut functions void mouseMove(int x, int y); void mouseClick(int button, int state, int x, int y); void reshape(int w, int h); void display(void) { SetCamera(); Render_BoxAndAxes(); Render_Mesh(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); win_height = h; win_width = w; glMatrixMode(GL_PROJECTION); glLoadIdentity(); whratio = (double)w / (double)h; //A Commonly Suggested Setting: set ratio in gluPerspective to the aspect ratio of the associated viewport gluPerspective(60, whratio, axislen*0.01, axislen * 5); glMatrixMode(GL_MODELVIEW); //change back to modelview glutPostRedisplay(); } void main(int argc, char** argv) { if (argc != 2) { std::cout << "HW1: Read a .obj file and render it in its boundingbox.\n"; std::cout << "Usage: " << argv[0] << " input_mesh.obj\n"; return; } //Read the obj mesh bool flag = ReadOBJFile(argv[1]); if (!flag) { std::cerr << "Fail to read " << argv[1] << "\n"; return; } ComputeBoundingBox(); //OpenGL Routines glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(700, 700); glutInitWindowPosition(100, 100); glutCreateWindow("Homework 1 submitted by XXX"); MyInit(); //Register Callback Functions glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouseClick); glutMotionFunc(mouseMove); glutMainLoop(); } void mouseMove(int x, int y) { double movingScale = axislen / win_height; // just a scaling factor to make the mouse moving not too sensitive /* rotation*/ if (mouseButton == GLUT_LEFT_BUTTON) { ////////////do something//////////////// } /*xy translation */ if (mouseButton == GLUT_MIDDLE_BUTTON) { ////////////do something //////////////// } /* zoom in and out */ if (mouseButton == GLUT_RIGHT_BUTTON) { // suppose we want to make moving up as zooming out } mousePositionX0 = x; mousePositionY0 = y; glutPostRedisplay(); } void mouseClick(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) mouseButton = GLUT_LEFT_BUTTON; else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) mouseButton = GLUT_MIDDLE_BUTTON; else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) mouseButton = GLUT_RIGHT_BUTTON; mousePositionX0 = x; mousePositionY0 = y; return; } //Rendering void Render_BoxAndAxes() { float axiswidth = axislen / 100; glMatrixMode(GL_MODELVIEW); glColor3f(1, 1, 1); glPushMatrix(); //bounding box glTranslatef(objCenter[0], objCenter[1], objCenter[2]); glutWireCube(axislen); glTranslatef(-axislen / 2, -axislen / 2, -axislen / 2); glutSolidSphere(axiswidth*1.5, 10, 10); //x-axis glColor3f(1, 0, 0); glPushMatrix(); glRotatef(90, 0, 1, 0); gluCylinder(obj, axiswidth, axiswidth, axislen, 5, 5); glPopMatrix(); glPushMatrix(); glTranslatef(axislen, 0, 0); glRotatef(90, 0, 1, 0); glutWireCone(axiswidth*1.5, axiswidth * 3, 10, 10); glPopMatrix(); //y-axis glColor3f(0, 1, 0); glPushMatrix(); glTranslatef(0, axislen, 0); glRotatef(-90, 1, 0, 0); glutWireCone(axiswidth*1.5, axiswidth * 3, 10, 10); glPopMatrix(); glPushMatrix(); glRotatef(-90, 1, 0, 0); gluCylinder(obj, axiswidth, axiswidth, axislen, 5, 5); glPopMatrix(); //z-axis glColor3f(0, 0, 1); glPushMatrix(); glTranslatef(0, 0, axislen); glRotatef(-90, 0, 0, 1); glutWireCone(axiswidth*1.5, axiswidth * 3, 10, 10); glPopMatrix(); glPushMatrix(); gluCylinder(obj, axiswidth, axiswidth, axislen, 5, 5); glPopMatrix(); glPopMatrix(); } void Render_Mesh(){ } bool ReadOBJFile(const char filename[]){ return true; } void ComputeBoundingBox() { } void SetCamera(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(cameraPosition[0], cameraPosition[1], cameraPosition[2], objCenter[0], objCenter[1], objCenter[2], 0, 1, 0); //Do something here to incorporate interactive transformations specified by the user } void MyInit() { glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glEnable(GL_DEPTH_TEST); glClearColor(0, 0, 0, 0); glShadeModel(GL_SMOOTH); glPolygonMode(GL_FRONT, GL_FILL); obj = gluNewQuadric(); //only for drawing spheres and cones glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHT0); // Create light components GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f }; GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat position[] = { cameraPosition[0], cameraPosition[1], cameraPosition[2], 1.0f }; // the light is on the camera position // Assign created components to GL_LIGHT0 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); glLightfv(GL_LIGHT0, GL_POSITION, position); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); }