opengl 看不到会动的矩形 只有蓝色窗口 程序能运行-
#include"stdafx.h"//MoveRect.c#include<windows.h>#include<gl/glut.h>#include<gl/gl.h>...
#include "stdafx.h"
//MoveRect.c
#include <windows.h>
#include <gl/glut.h>
#include<gl/gl.h>
#include<gl/glu.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 RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(x1, y1, x1+rsize, y1+rsize);
//清空命令缓冲区并交换帧缓存
glutSwapBuffers();
}
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
{
windowWidth = 250.0f*w/h;
windowHeight = 250.0f;
}
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
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 > windowHeight-rsize) y1 = windowHeight-rsize-1;
// 根据步长修改正方形的位置
x1 += xstep;
y1 += ystep;
// 用新坐标重新绘图
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
void SetupRC(void)
{
//设置窗口清除色为蓝色
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
int main(void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);
SetupRC();
glutMainLoop();
} 程序没错误啊,其他的程序可以运行得到想要的结果,就这个程序看不到动的矩形,求助 谢谢了, 展开
//MoveRect.c
#include <windows.h>
#include <gl/glut.h>
#include<gl/gl.h>
#include<gl/glu.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 RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glRectf(x1, y1, x1+rsize, y1+rsize);
//清空命令缓冲区并交换帧缓存
glutSwapBuffers();
}
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
{
windowWidth = 250.0f*w/h;
windowHeight = 250.0f;
}
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
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 > windowHeight-rsize) y1 = windowHeight-rsize-1;
// 根据步长修改正方形的位置
x1 += xstep;
y1 += ystep;
// 用新坐标重新绘图
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
void SetupRC(void)
{
//设置窗口清除色为蓝色
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
int main(void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);
SetupRC();
glutMainLoop();
} 程序没错误啊,其他的程序可以运行得到想要的结果,就这个程序看不到动的矩形,求助 谢谢了, 展开
2个回答
展开全部
把主函数里面glutTimerFunc(33, TimerFunction, 1);33改成比较大的,比如glutTimerFunc(330, TimerFunction, 1);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询