OpenGL视点跟踪物体运动


视点跟踪一个节点运动的原理是:把物体矩阵取反。

  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3. #include <gl/glut.h>   
  4. #include <math.h>   
  5. #include <time.h>   
  6.   
  7. GLfloat angle = 0.0;  
  8. GLfloat theta = 0.0, vp = 6;//vp视点的位置   
  9.   
  10. GLfloat cenx, ceny;  
  11.   
  12. const int PI = 3.1415926;  
  13. const double N = 200.0;  
  14.   
  15. int myWin;  
  16. const int MAX_MAP = 500;  
  17. int myMap[MAX_MAP][MAX_MAP];  
  18.   
  19. inline double aToR(double x)  
  20. {  
  21.     return x/180.0 * 3.1415926;  
  22. }  
  23. void openLight()  
  24. {  
  25.       
  26.     float light_position[4] = {30, 30, 30, 0};  
  27.     float light_ambient[4] = {1.0, 1.0, 1.0, 1.0};  
  28.     float light_diffuse[4] = { 1.0, 1.0, 1.0, 1.0};  
  29.   
  30.     glLightfv(GL_LIGHT0,GL_POSITION,light_position);   
  31.     glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);  
  32.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);  
  33.       
  34.     glEnable( GL_LIGHTING );  
  35.     glEnable( GL_LIGHT0 );  
  36. }  
  37.   
  38. void cube()  
  39. {  
  40.     typedef float point3[3];  
  41.     typedef int edge[2];  
  42.     typedef int face[4];  
  43.   
  44.     point3 vertices[8] = {  
  45.         {-1.0, -1.0, -1.0},  
  46.         {-1.0, -1.0, 1.0},  
  47.         {-1.0, 1.0, -1.0},  
  48.         {-1.0, 1.0, 1.0},  
  49.         {1.0, -1.0, -1.0},  
  50.         {1.0, -1.0, 1.0},  
  51.         {1.0, 1.0, -1.0},  
  52.         {1.0, 1.0, 1.0}  
  53.     };  
  54.   
  55.     point3 normals[6] = {  
  56.         {-1.0, 0.0, 0.0},  
  57.         {0.0, 0.0, 1.0},  
  58.         {0.0, 1.0, 0.0},  
  59.         {0.0, 0.0, -1.0},  
  60.         {0.0, -1.0, 0.0},  
  61.         {1.0, 0.0, 0.0}  
  62.     };  
  63.     edge edges[24] = {  
  64.         {0, 1}, {1, 3}, {3, 2}, {2, 0},  
  65.         {0, 4}, {1, 5}, {3, 7}, {2, 6},  
  66.         {4, 5}, {5, 7}, {7, 6}, {6, 4},  
  67.         {1, 0}, {3, 1}, {2, 3}, {0, 2},  
  68.         {4, 0}, {5, 1}, {7, 3}, {6, 2},  
  69.         {5, 4}, {7, 5}, {6, 7}, {4, 6}  
  70.     };  
  71.   
  72.         face cube[6] =   
  73.         {  
  74.             {0, 1, 2, 3}, {5, 9, 18, 13},  
  75.             {14, 6, 10, 19}, {7, 11, 16, 15},  
  76.             {4, 8, 17, 12}, {22, 21, 20, 23}  
  77.         };  
  78.   
  79.         //GLdouble myClipPlane[] = {1.0, 1.0, 0.0, -1.0};   
  80.         //glClipPlane(GL_CLIP_PLANE0, myClipPlane);   
  81.         ///glEnable(GL_DEPTH_TEST);   
  82.         //glEnable(GL_CLIP_PLANE0);   
  83.         glBegin(GL_QUADS);  
  84.             for(int face = 0; face < 6; face ++)  
  85.             {  
  86.                 glNormal3fv(normals[face]);  
  87.                 for(int edge = 0; edge < 4; edge ++)  
  88.                 {  
  89.                     glVertex3fv(vertices[edges[cube[face][edge]][0]]);  
  90.                 }  
  91.             }  
  92.         glEnd();  
  93.         //glDisable(GL_CLIP_PLANE0);   
  94. }  
  95.   
  96. void qumian()  
  97. {  
  98.     glLineWidth(10);  
  99.     glBegin(GL_LINES);  
  100.           
  101.           
  102.         glVertex3f(40, 0, 0);  
  103.         glVertex3f(0, 20, 0);  
  104.           
  105.     glEnd();  
  106. }  
  107.   
  108. void display(void)  
  109. {  
  110.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  111.     glEnable(GL_DEPTH_TEST);  
  112.     glClearColor(0, 0, 0.5, 1);  
  113.     openLight();  
  114.     glMatrixMode(GL_MODELVIEW);  
  115.     glLoadIdentity();  
  116.     gluLookAt(0, 0, 0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);  
  117.       
  118.     glTranslatef(0, 0, -12);  
  119.     //glTranslatef(0, -5, 0);   
  120.     glRotatef(-angle, 0.0, 0.0, 1.0);  
  121.       
  122.     //glScalef(10, 10, 10);   
  123.     glPushMatrix();  
  124.         glRotatef(angle, 0.0, 0.0, 1.0);  
  125.         //glTranslatef(0, 5, 0);   
  126.         cube();  
  127.     glPopMatrix();  
  128.   
  129.     glPushMatrix();  
  130.         glTranslatef(2, 2, -9);  
  131.         cube();  
  132.     glPopMatrix();  
  133.   
  134.     glPushMatrix();  
  135.         glTranslatef(2, 3, -2);  
  136.         cube();  
  137.     glPopMatrix();  
  138.   
  139.     glPushMatrix();  
  140.         glTranslatef(-1, 3, 2);  
  141.         cube();  
  142.     glPopMatrix();  
  143.   
  144.     glutSwapBuffers();  
  145.           
  146. }  
  147.   
  148. void reshape(int w, int h)  
  149. {  
  150.     int min = w < h?w:h;  
  151.     int cenx = w *  0.5;  
  152.     int ceny = h *  0.5;  
  153.     glViewport(cenx - min*0.5,  ceny - min*0.5,  (GLsizei)min, (GLsizei)min);  
  154.     glMatrixMode(GL_PROJECTION);  
  155.     glLoadIdentity();  
  156.   
  157.     gluPerspective(60, 1, 0.1, 300.0);  
  158.     glutPostRedisplay();  
  159. }  
  160.   
  161. void iterationStep(void)  
  162. {  
  163.     angle += 1.0;  
  164.     cenx = cos(angle / 180 * 3.14159) * vp + vp;  
  165.     ceny = sin(angle / 180 * 3.14159) * vp;  
  166. }  
  167.   
  168. void animate(void)  
  169. {  
  170.     iterationStep();  
  171.     glutPostRedisplay();  
  172. }  
  173.   
  174. int main ( int argc, char ** argv )  
  175. {  
  176.     glutInit(&argc, argv);  
  177.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);   //这里必须使用双缓冲区,双缓冲区通过在后一个缓冲区里绘画,并不停交换前后缓冲区(可见缓冲区),来产生平滑的动画,使用双缓冲区可以有效的预防闪烁。   
  178.     glutInitWindowSize(500, 500);  
  179.     glutInitWindowPosition(50, 50);  
  180.     myWin = glutCreateWindow("画球");  
  181.   
  182.     glutDisplayFunc(display);  
  183.     glutReshapeFunc(reshape);  
  184.     glutIdleFunc(animate);  
  185.     glutMainLoop();  
  186. }  

最终效果图如下所示:

相关内容