效果图如上:
步骤:首先,设置模型视角往后退,再旋转视角;然后,用默认绘制立方体函数绘制;最后,利用空闲对模型做角度微调。
实现代码如下:
1 #include <GL\glut.h> 2 3 GLfloat xRotated, yRotated, zRotated; 4 5 void Display(void) 6 { 7 glClear(GL_COLOR_BUFFER_BIT); 8 glLoadIdentity(); 9 glTranslatef(0.0,0.0,-4.0); 10 glRotatef(xRotated,1.0,0.0,0.0); 11 glRotatef(yRotated,0.0,1.0,0.0); 12 glRotatef(zRotated,0.0,0.0,1.0); 13 //glScalef(2.0,1.0,1.0); 14 glutWireCube(1.5); 15 glFlush(); //Finish rendering 16 glutSwapBuffers(); 17 } 18 19 void Reshape(int x, int y) 20 { 21 if (y == 0 || x == 0) return; //Nothing is visible then, so return 22 //Set a new projection matrix 23 glMatrixMode(GL_PROJECTION); 24 glLoadIdentity(); 25 //Angle of view:40 degrees 26 //Near clipping plane distance: 0.5 27 //Far clipping plane distance: 20.0 28 gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0); 29 glMatrixMode(GL_MODELVIEW); 30 glViewport(0,0,x,y); //Use the whole window for rendering 31 } 32 static int times = 0; 33 void Idle(void) 34 { 35 times++; 36 if(times >30000) 37 times = 0; 38 39 if(times %30000 == 0) 40 { 41 xRotated += 0.3; 42 yRotated += 0.1; 43 zRotated += -0.4; 44 Display(); 45 } 46 } 47 48 49 int main (int argc, char **argv) 50 { 51 //Initialize GLUT 52 glutInit(&argc, argv); 53 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //For animations you should use double buffering 54 glutInitWindowSize(300,300); 55 //Create a window with rendering context and everything else we need 56 glutCreateWindow("Cube example"); 57 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 58 xRotated = yRotated = zRotated = 0.0; 59 glClearColor(0.0,0.0,0.0,0.0); 60 //Assign the two used Msg-routines 61 glutDisplayFunc(Display); 62 glutReshapeFunc(Reshape); 63 glutIdleFunc(Idle); 64 //Let GLUT get the msgs 65 glutMainLoop(); 66 return 0; 67 }