Thursday, July 24, 2014

Opengl,C++ : 3D Rotation Cube


1.    Define the above cube using two separate lists: a vertex list and a face list.
2.    Rotate the given cube using the arrow keys.
 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>   
 typedef struct{float x;float y;}Point2D;  
 float dx = 200, dy = 200, dz = 200;  
 /* Vertex indices for the 6 faces of a cube. */   
 GLint faces[6][4] = {  
      {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},  
      {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}   
 };  
 GLfloat v[8][3];  
 void init(void)  
 {  
      v[0][0] = v[1][0] = v[2][0] = v[3][0] = 100;  
      v[4][0] = v[5][0] = v[6][0] = v[7][0] = 300;  
      v[0][1] = v[1][1] = v[4][1] = v[5][1] = 100;  
      v[2][1] = v[3][1] = v[6][1] = v[7][1] = 300;  
      v[0][2] = v[3][2] = v[4][2] = v[7][2] = 300;  
      v[1][2] = v[2][2] = v[5][2] = v[6][2] = 100;  
 }  
 Point2D Project_ortho(float x, float y, float z){  
      Point2D p0;  
      p0.x = x ;  
      p0.y = y ;  
      return p0;  
 }  
 void drawBox(void)  
 {  
      Point2D p0,p1,p2,p3;  
      for (int f = 0; f < 6; f++) {  
           p0 = Project_ortho(v[ faces[f][0] ][0],v[ faces[f][0] ][1],v[ faces[f][0] ][2]);  
           p1 = Project_ortho(v[ faces[f][1] ][0],v[ faces[f][1] ][1],v[ faces[f][1] ][2]);  
           p2 = Project_ortho(v[ faces[f][2] ][0],v[ faces[f][2] ][1],v[ faces[f][2] ][2]);  
           p3 = Project_ortho(v[ faces[f][3] ][0],v[ faces[f][3] ][1],v[ faces[f][3] ][2]);  
           glBegin(GL_LINE_LOOP);  
                glVertex2f(p0.x,p0.y);  
                glVertex2f(p1.x,p1.y);  
                glVertex2f(p2.x,p2.y);  
                glVertex2f(p3.x,p3.y);  
           glEnd();  
           glFlush();       
      }  
 }  
 void translate(float* x, float* y, float* z, float dx, float dy, float dz){  
      float tx = *x, ty=*y, tz=*z;  
      *x = tx + dx;  
      *y = ty + dy;  
      *z = tz + dz;  
 }   
 void rotatex(float* x, float* y, float* z, float ang){  
      ang = ang * 3.14 / 180.0;                         //angle in radians   
      float tx = *x, ty=*y, tz=*z;  
      *y = ty * cos(ang) - tz * sin(ang);  
      *z = ty * sin(ang) + tz * cos(ang);  
      *x = tx;   
 }   
 void rotatey(float* x, float* y, float* z, float ang){  
      ang = ang * 3.14 / 180.0;                         //angle in radians   
      float tx = *x, ty=*y, tz=*z;  
      *x = tx * cos(ang) + tz * sin(ang);  
      *z = -tx * sin(ang) + tz * cos(ang);  
      *y = ty;  
 }   
 void rotatebox_x(float deg_x){  
      for (int i = 0; i < 8; i++) {  
           translate(&v[i][0],&v[i][1],&v[i][2], -dx,-dy,-dz);  
           rotatex(&v[i][0],&v[i][1],&v[i][2], deg_x);  
           translate(&v[i][0],&v[i][1],&v[i][2], dx,dy,dz);  
      }  
      glutPostRedisplay();  
 }  
 void rotatebox_y(float deg_y){  
      for (int i = 0; i < 8; i++) {  
           translate(&v[i][0],&v[i][1],&v[i][2], -dx,-dy,-dz);  
           rotatey(&v[i][0],&v[i][1],&v[i][2], deg_y);  
           translate(&v[i][0],&v[i][1],&v[i][2], dx,dy,dz);  
      }  
      glutPostRedisplay();  
 }  
 void keyboard(unsigned char key, int x, int y)  
 {  
      if (key == 'q' || key == 'Q')  
           exit(EXIT_SUCCESS);  
 }  
 void special(int key, int, int) {  
      switch (key) {  
      case GLUT_KEY_LEFT: rotatebox_y(-1); break;  
      case GLUT_KEY_RIGHT: rotatebox_y(1);break;  
      case GLUT_KEY_UP: rotatebox_x(-1);break;  
      case GLUT_KEY_DOWN: rotatebox_x(1);break;  
      default: return;  
      }  
 }  
 void myDisplay()  
 {  
      glClearColor(1.0f, 1.0f, 1.0f, 0.0f);   
      glClear(GL_COLOR_BUFFER_BIT);  
      glColor3f(0.0f, 0.0f, 0.0f);  
      drawBox();  
 }  
 int main( int argc, char ** argv ) {  
      glutInit( &argc, argv );  
      glutInitWindowPosition( 0, 0 );  
      glutInitWindowSize( 800, 600 );  
      glutCreateWindow( "3D Rotation Cube" );  
      glMatrixMode( GL_PROJECTION );   
      glLoadIdentity();  
      gluOrtho2D( 0, 800, 0, 600 );  
      glViewport(0, 0, 800, 600);  
      init();  
      glutKeyboardFunc(keyboard);   
      glutSpecialFunc(special);  
      glutDisplayFunc( myDisplay );  
      glutMainLoop();  
      return( 0 );  
 }  

No comments:

Post a Comment