有了简单的画图基础外,可以通过openGL的glutTimerFunc函数实现简单的动画
砖块在窗口四周不断的弹来弹去
代码
主要是glutTimerFunc函数的使用, 见代码
#include <gl\glut.h>
#include <gl\GLU.h>
#include <gl\GL.h>
#include <math.h>
const GLfloat PI = 3.141595654f;
const int windowWidth = 400;
const int windowHeight = 400;
GLfloat posX = 100.0f;
GLfloat posY = 200.0f;
GLsizei rsize = 50;
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
void myDisplay(void)
{
// 用当前清除色清除颜色缓冲区,即设定窗口的背景色
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0, 0);
glRectf(posX, posY, posX + rsize, posY + rsize);
//glFlush();
glutSwapBuffers(); // 双缓冲下使用该函数交换两个缓冲区的内容
}
void timerFunc(int value)
{
if (posX > windowWidth - rsize || posX < 0)
xstep = -xstep;
if (posY > windowHeight - rsize || posY < 0)
ystep = -ystep;
if (posX > windowWidth - rsize)
posX = windowWidth - rsize - 1;
if (posY > windowHeight - rsize)
posY = windowHeight - rsize - 1;
posX += xstep;
posY += ystep;
/*
glutPostRedisplay 标记当前窗口需要重新绘制。
通过glutMainLoop下一次循环时,窗口显示将被回调以重新显示窗口的正常面板。
*/
glutPostRedisplay();
/*
(1)如果用定时器的话,初始的时候注册一个定时器的回调函数,原型是
glutTimerFunc(unsigned int millis, void (*func)(int value), int value);
参数对应关系为:glutTimerFunc(毫秒数, 回调函数指针, 区别值);
(2)写自己的回调函数 void OnTimer(int value);
用value区分是哪个定时器
(3)在函数里改变和位置有关的变量,然后调用glutPostRedisplay();用来重绘
(4)最后再次调用glutTimerFunc,因为glut的定时器是调用一次才产生一次定时,所以如果要持续产生定时的话,
在定时函数末尾再次调用glutTimerFunc
*/
glutTimerFunc(33, timerFunc, 1);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
//glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // 使用双缓冲
glutInitWindowPosition(100, 100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("第一个OpenGL程序");
gluOrtho2D(0.0, windowWidth, 0.0, windowHeight);
glutDisplayFunc(&myDisplay);
glutTimerFunc(33, timerFunc, 1);
glutMainLoop();
return 0;
}
截图,砖块是不断运动的
同上,小球碰撞运动
代码
#include <gl\glut.h>
#include <gl\GLU.h>
#include <gl\GL.h>
#include <math.h>
const GLfloat Pi = 3.141595654f;
const int windowWidth = 400;
const int windowHeight = 400;
float cx = windowWidth * 1.0 / 5.0;
float cy = windowHeight * 4.0 / 5.0;
GLfloat R = 20;
float maxHeight = cy;
GLfloat xstep = 0.0f;
GLfloat ystep = -2.0f;
void myDisplay(void)
{
// 用当前清除色清除颜色缓冲区,即设定窗口的背景色
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0, 0);
// 绘制多边形,n足够大就会变成圆形
int n = 20;
int i;
glBegin(GL_POLYGON);
for (i = 0; i<n; ++i)
glVertex2f(cx + R*cos(2 * Pi / n*i), cy + R*sin(2 * Pi / n*i));
glEnd();
//glFlush();
glutSwapBuffers(); // 双缓冲下使用该函数交换两个缓冲区的内容
}
void timerFunc(int value)
{
cx += 0.5;
cy += ystep;
if (cy <= R)
{
ystep = -ystep;
maxHeight /= 2;
}
if (cy > maxHeight)
{
ystep = -ystep;
}
glutPostRedisplay();
if (maxHeight >= 2 * R)
glutTimerFunc(33, timerFunc, 1);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
//glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // 使用双缓冲
glutInitWindowPosition(100, 100);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("第一个OpenGL程序");
gluOrtho2D(0.0, windowWidth, 0.0, windowHeight);
glutDisplayFunc(&myDisplay);
glutTimerFunc(33, timerFunc, 1);
glutMainLoop();
return 0;
}