第八章的习题有动画的要求,之前并没有讲解动画如何制作,网上搜到一篇文章SCARA——OpenGL入门学习五六(三维变换、动画),按照里面的方法,使用双缓存和空闲回调函数实现了一个简单的帧动画。
1 #include <GLUT/GLUT.h> 2 3 GLfloat count = 0.0; 4 5 void init (void) 6 { 7 glClearColor(0.0, 0.0, 0.0, 1.0); 8 glMatrixMode(GL_PROJECTION); 9 gluOrtho2D(-20, 20, -20, 20); 10 } 11 12 void displayFcn (void) 13 { 14 glClear(GL_COLOR_BUFFER_BIT); 15 16 glColor3f(1.0, 1.0, 1.0); 17 glBegin(GL_LINE_LOOP); 18 glVertex2f(-20.0 + count, -20.0 + count); 19 glVertex2f(-20.0 + count, -10.0 + count); 20 glVertex2f(-10.0 + count, -10.0 + count); 21 glVertex2f(-10.0 + count, -20.0 + count); 22 glEnd(); 23 24 glFlush(); 25 glutSwapBuffers(); 26 } 27 28 void idleFcn (void) 29 { 30 count ++; 31 if(count > 30.0) 32 count = 0.0; 33 displayFcn(); 34 } 35 36 int main(int argc, char * argv[]) 37 { 38 glutInit(&argc, argv); 39 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 40 glutInitWindowPosition(-50, 50); 41 glutInitWindowSize(600, 300); 42 glutCreateWindow("Animation"); 43 44 init(); 45 46 glutDisplayFunc(displayFcn); 47 glutIdleFunc(idleFcn); 48 glutMainLoop(); 49 50 return 0; 51 }
b0b15dccfd0464a826f8050e20c44c87e7dfee49 project: Chapter8Example4