【问题标题】:Points won't move by keyboard input in OpenGl点不会通过 OpenGL 中的键盘输入移动
【发布时间】: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;
}

【问题讨论】:

    标签: c++ opengl graphics glut


    【解决方案1】:
    • 不要从显示回调中调用 glutPostRedisplay(),如果您希望该行为通过 glutIdleFunc() 设置空闲回调。
    • 不要从绘图函数更新状态,在单独的通道中执行。
    • 您缺少glClear()
    • 重置您的投影并对每一帧进行建模,有助于防止难以诊断的错误。
    • 如果您要使用glutSwapBuffers(),请使用GLUT_DOUBLE
    • 如果你想要一个均匀的帧速率(在没有可靠的垂直同步的情况下)设置一个计时器回调并调用glutPostRedisplay()(并重新启动计时器)。您也可以在那里进行物理更新。
    • 奖励积分:glutGet(GLUT_ELAPSED_TIME) & 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多