我试图在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;
}
答案 0 :(得分:0)
glutPostRedisplay()
设置空闲回调,请不要从显示回调中调用glutIdleFunc()
。glClear()
。GLUT_DOUBLE
,请使用glutSwapBuffers()
。glutPostRedisplay()
(并重新启动定时器)。你也可以在那里进行物理更新。glutGet(GLUT_ELAPSED_TIME)
&amp; Fix your timestep! 所有在一起:
#include <GL/glut.h>
#include <map>
std::map< int, bool > keys;
void special( int key, int x, int y )
{
keys[ key ] = true;
}
void specialUp( int key, int x, int y )
{
keys[ key ] = false;
}
float xpos = 0;
float ypos = 0;
void update()
{
const float speed = 1.0;
if( keys[ GLUT_KEY_LEFT ] )
{
xpos -= speed;
}
if( keys[ GLUT_KEY_RIGHT ] )
{
xpos += speed;
}
if( keys[ GLUT_KEY_UP ] )
{
ypos += speed;
}
if( keys[ GLUT_KEY_DOWN ] )
{
ypos -= speed;
}
}
void display()
{
glClearColor( 0.435294, 0.258824, 0.258824, 0.0 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -100, 100, -100, 100, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( xpos, ypos, 0 );
glScalef( 10, 10, 1 );
glBegin( GL_QUADS );
glVertex2i( -1, -1 );
glVertex2i( 1, -1 );
glVertex2i( 1, 1 );
glVertex2i( -1, 1 );
glEnd();
glutSwapBuffers();
}
void timer( int value )
{
update();
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 640 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutSpecialFunc( special );
glutSpecialUpFunc( specialUp );
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}