[2012-12-31 16:15] OpenGL学习笔记(6)第一个动画 - Clingingboy - 博客园 - Google Chrome
glutMouseFunc用于捕获鼠标事件
glutKeyboardFunc用于捕获键盘事件
glutMotionFunc用于鼠标按下又移动鼠标的事件(MouseMove)
glutIdleFunc事件,当循环队列处于空闲时则触发该事件
glutTimerFunc单位时间内内触发事件
属性-》链接器-》输入 附加依赖库 glut32.lib OPENGL32.LIB GLU32.LIB GLAUX.LIB
属性-》c/c++-》预处理器 预处理器定义 ;GLUT_BUILDING_LIB
每次都将spin加2,这样的话spin其实是矩形旋转度数的总和,但如何维护矩形初始化(未旋转前)的矩阵呢?
即将为旋转前的当前矩阵压入(glPushMatrix)矩阵堆栈中,然后当旋转结束后又弹出(glPopMatrix)堆栈恢复,当spin增加时,又以原始矩阵来合并,这样就不会出现什么问题了.
还有一种做法则是去掉矩阵堆栈的压入与弹出,使得spin每次都是等于2,但度数就可能会超过360,造成数字过大转换,如
#include <stdio.h> #include <gl/glut.h> static GLfloat spin = 0.0; void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin,0.0,0.0,1.0); glColor3f(1.0,1.0,1.0); glRectf(-25.0,-25.0,25.0,25.0); glPopMatrix(); glutSwapBuffers(); } void spinDisplay(void) { spin = spin + 2.0; if (spin > 360.0) { spin = spin-360.0; } glutPostRedisplay(); } void init(void) { glClearColor(0.0,0.0,0.0,0.0); glShadeModel(GL_FALSE); } void reshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void mouse(int button,int state,int x,int y) { switch(button){ case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) { glutIdleFunc(spinDisplay); } break; case GLUT_MIDDLE_BUTTON: case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) { glutIdleFunc(NULL); } break; default: break; } } int _tmain(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(250,250); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMainLoop(); return 0; }
(http://www.cnblogs.com/Clingingboy/archive/2010/10/17/1853671.html)
[2012-12-31 16:35] OpenGL学习笔记(6)第一个动画 - Clingingboy - 博客园 - Google Chrome
glutTimerFunc回调事件只执行一次,如果想一直执行的话就需要在内部再次调该方法,下面用glutTimerFunc来替代glutIdleFunc实现同样的效果
void spinDisplay(int value) { spin = spin + 2.0; if (spin > 360.0) spin = spin - 360.0; glutPostRedisplay(); glutTimerFunc(10, spinDisplay, 1); }
当然首先要外部先调用一次spinDisplay方法才可以
(http://www.cnblogs.com/Clingingboy/archive/2010/10/17/1853671.html)