【发布时间】:2017-04-30 23:55:16
【问题描述】:
我正在尝试在 OpenGL 中从上到下移动几个点(水平渲染)。代码运行没有任何错误。当我尝试处理诸如-> 开始移动、加速/减速或停止移动之类的事件时,它不起作用。下面是代码。
#include <gl/glut.h>
int pointStatus=0;
float pointX = 0.0f;
float pointY = 0.0f;
float speed, b = 0.5;
char s;
void Point(int x1, int y1)
{
glColor3f(1.0, 1.0, 1.0);
glPointSize(5);
glPushMatrix();
glBegin(GL_POINTS);
glVertex2i(x1, y1);
glPopMatrix();
glEnd();
}
void Draw()
{
int x=0, y=6;
for (x = 0; x <= 6; x += 1)
{
Point(x,y);
}
glFlush();
}
void movePoint(int s, int x, int b)//
{
if (pointStatus == 1)
{
pointY -= b + speed;
}
if (pointY>14)
{
pointY = -6;
}
glPushMatrix();
glTranslatef(pointX, pointY, 0);
Draw();
glPopMatrix();
}
void handleKeypress(unsigned char key, int x, int y)
{
if (key == '1')
{
pointStatus = 1; // start
b = +0.05;
speed = 0.5;
}
else if (key == '=' || key == '+')
{
pointStatus = 1; //speed up
b = +.05;
speed += .2;
}
else if (key == '-' || key == '_')
{
pointStatus = 1; //speed down
b = -.05;
speed -= .2;
}
else if (key == '2')
{
pointStatus = 0; //stop
speed = 0;
}
}
void myDisplay(void)
{
movePoint(s, b, speed);
//glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void Initialize() {
glClearColor(0.435294, 0.258824, 0.258824,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-14.0, 14.0, -14.0, 14.0, -1.0, 10.0); // left, right, bottom, top, near and far
}
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(700,500);
glutInitWindowPosition(400, 200);
glutCreateWindow("tuntaa");
Initialize();
glutDisplayFunc(myDisplay);
glutKeyboardFunc(handleKeypress);
glutMainLoop();
return 0;
}
【问题讨论】: