24,854
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <stdio.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
GLfloat x1 = 100.0f;
GLfloat y10 = 150.0f;
GLsizei rsize = 50;
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
GLfloat windowWidth;
GLfloat windowHeight;
void init()
{
glClearColor(0.0f,1.0f,1.0f,1.0f);
}
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,1.0f,0.0f);
glRectf(x1,y10,x1 + rsize,y10 + rsize);
glutSwapBuffers();
}
void TimerFunction(int value)
{
if(x1 > windowWidth - rsize||x1 < 0)
xstep = -xstep;
if(y10 > windowHeight - rsize||y10 < 0)
ystep = -ystep;
if(x1 > windowWidth - rsize)
x1 = windowWidth - rsize - 1;
if(y10 > windowHeight - rsize)
y10 = windowHeight - rsize - 1;
x1 += xstep;
y10 += ystep;
glutPostRedisplay();
glutTimerFunc(33,TimerFunction,1);
}
void ChangeSize(GLsizei w,GLsizei h)
{
if(h == 0)
h = 1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w <= h)
glOrtho(0.0f,250.0f,0.0f,250.0f * h / w,1.0f,-1.0f);
else
glOrtho(0.0f,250.0f * w / h,0.0f,250.0f,1.0f,-1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,0);//屏幕左上角为原点(0,0)
glutInitWindowSize(600,600);
glutCreateWindow("test");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33,TimerFunction,1);
init();
glutMainLoop();
}