33,311
社区成员
发帖
与我相关
我的任务
分享
#include<Windows.h>
#include<gl/glut.h>
GLfloat x1=100.0f;
GLfloat y1=150.0f;
GLsizei rsize=50;
GLfloat xstep=1.0f;
GLfloat ystep=1.0f;
GLfloat windowWidth;
GLfloat windowHeight;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rsize,y1+rsize);
glutSwapBuffers();
}
void TimerFunction(int value)
{
if(x1>windowWidth-rsize||x1<0)
xstep=-xstep;
if(y1>windowHeight-rsize||y1<0)
ystep=-ystep;
if(x1>windowWidth-rsize)
x1=windowWidth-rsize-1;
if(y1>windowWidth-rsize)
y1=windowWidth-rsize-1;
x1+=xstep;
y1+=ystep;
glutPostRedisplay();
glutTimerFunc(33,TimerFunction,1);
}
void init()
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
void reshape(GLsizei w,GLsizei h)
{
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h)
{
windowHeight=250.0f*h/w;
windowWidth=250.0f;
}
else
{
windowWidth=250.0f*w/h;
windowHeight=250.0f;
}
glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void main()
{
glutInitDisplayMode(GL_DOUBLE|GL_RGB);
glutCreateWindow("bounce");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(33,TimerFunction,1);
init();
glutMainLoop();
}