19,468
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <gl/glut.h>
GLfloat x1=100.0f;
GLfloat y1=150.0f;
GLfloat rsize=25;
GLfloat xstep=1.0f;
GLfloat ystep=1.0f;
GLfloat windowWidth;
GLfloat windowHeight;
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rsize,y1-rsize);
glutSwapBuffers(); //刷新绘图命令,并进行交换
}
void SetupRC(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
void TimerFunction(int value)
{
if(x1>windowWidth-rsize||x1<-windowWidth)
xstep=-xstep;
if(y1>windowHeight||y1<-windowHeight+rsize)
ystep=-ystep;
x1+=xstep;
y1+=ystep;
if(x1>(windowWidth-rsize+xstep))
x1=windowWidth-rsize-1;
else if(x1<-(windowWidth+xstep))
x1=-windowWidth-1;
if(y1>(windowHeight+ystep))
y1=windowHeight-1;
else if(y1<-(windowHeight-rsize+ystep))
y1=-windowHeight+rsize-1;
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)
{
windowHeight=250.0f*h/w;
windowWidth=250.0f;
}
else
{
windowHeight=250.0f;
windowWidth=250.0f*w/h;
}
glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(800,600);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33,TimerFunction,1);
SetupRC();
glutMainLoop();
return 0;
}
#include <windows.h>
//#include <gl/gl.h>
//#include <gl/glu.h>
#include <gl/glut.h>
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f); //设置绘图颜色
glRectf(-25.0f,25.0f,25.0f,-25.0f); //绘制填充矩形
glFlush();
}
void SetupRC(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
void ChangeSize(GLsizei w,GLsizei h)
{ GLfloat aspectRatio;
if(h==0) h=1;
glViewport(0,0,w,h);
//重新设置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio=(GLfloat)w/(GLfloat)h;
if(w<=h)
glOrtho(-100.0,100.0,-100/aspectRatio,100.0/aspectRatio,1.0,-1.0);
else
glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100,100.0,1.0,-1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//void main(void)
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutCreateWindow("GLRect");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize); //当窗口大小改变时,重新设置坐标系统
SetupRC();
glutMainLoop();
return 0;
}