OpenGL / C ++中“ 2D”相机的运动

时间:2019-03-26 11:27:48

标签: c++ opengl glut opengl-compat

我已经检查了该站点上的所有其他相关问题,但是没有一个解决方案对我有用。我只是试图跟随矩形,该矩形在OpenGL中通过按键左右移动。这是我非常简单的程序:

/*Begin useful backend functions/vars*/
/*************************************/
//Window size and refresh rate (60 fps)
int width = 500;
int height = 500;
int interval = 1000 / 60;

//Used to draw rectangles
void drawRect(float x, float y, float width, float height) {
    glBegin(GL_QUADS);
    glVertex2f(x, y);
    glVertex2f(x + width, y);
    glVertex2f(x + width, y + height);
    glVertex2f(x, y + height);
    glEnd();
}
/***********************************/
/*End useful backend functions/vars*/


/*Game vars*/
/***********/
//keycodes
#define keyA 0x41
#define keyD 0x44

//player
int playerWidth = 30;
int playerHeight = 50;
int playerSpeed = 3;

//player starting position
float playerX = width / 2;
float playerY = 25.0f;
/***************/
/*End game vars*/


/*Game specific functions*/
/*************************/
void keyboard() {
    //Move player (and camera) on key presses
    if (GetAsyncKeyState(keyA)) {
        playerX -= playerSpeed;
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(-playerSpeed,0,0);
    }
    if (GetAsyncKeyState(keyD)) {
        playerX += playerSpeed;
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(playerSpeed, 0, 0);
    }
}
/********************/
/*End game functions*/


/*Draw and update for window*/
/****************************/
void draw() {
    //Initialliy clear
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    //Draw player
    drawRect(playerX, playerY, playerWidth, playerHeight);

    //Swap buffers to end
    glutSwapBuffers();
}

void update(int value) {
    // input handling
    keyboard();  

    // Call update() again in 'interval' milliseconds
    glutTimerFunc(interval, update, 0);

    // Redisplay frame
    glutPostRedisplay();
}
/*****************/
/*End draw/update*/


/*Main function*/
/***************/
int main(int argc, char** argv) {
    // initialize opengl (via glut)
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(width, height);
    glutCreateWindow("My Game");

    // Register callback functions   
    glutDisplayFunc(draw);
    glutTimerFunc(interval, update, 0);

    // setup scene to be 2d
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //set draw color to white
    glColor3f(1.0f, 1.0f, 1.0f);

    //start the whole thing
    glutMainLoop();
    return 0;
}
/*************/
/*End of main*/

键盘移动效果很好,但是:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-playerSpeed,0,0);

在我的keyboard()函数内部,什么也不做。而且,如果我使用GL_PROJECTION进行尝试,则会使屏幕变黑。

1 个答案:

答案 0 :(得分:3)

首先要注意的是,自几十年来,不赞成使用glBegin/glEnd序列和固定函数矩阵堆栈进行绘制。参见Fixed Function PipelineLegacy OpenGL


简化事情。

添加一个用于键盘上,下键(glutKeyboardFunc / glutKeyboardUpFunc)的键盘事件。此功能仅修改播放器的速度。按下按钮时设置速度,释放按钮时设置速度:

int playerSpeed = 0;
void keyboardDown(unsigned char key, int x, int y)
{
    if (key == 'a')
        playerSpeed -= 3;
    else if (key == 'd')
        playerSpeed = 3;
}

void keyboardUp( unsigned char key, int x, int y )
{
    playerSpeed = 0;
}

timer事件只是修改播放器的位置:

void update(int value)
{
    playerX += playerSpeed;

    glutTimerFunc(interval, update, 0);
    glutPostRedisplay();
}

draw中设置模型矩阵并绘制矩形:

void draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(playerX, playerY, 0);

    //Draw player
    drawRect(0, 0, playerWidth, playerHeight);

    //Swap buffers to end
    glutSwapBuffers();
}

main中设置回调函数:

int main(int argc, char** argv) 
{
    // ...

    glutDisplayFunc(draw);
    glutTimerFunc(interval, update, 0);
    glutKeyboardFunc( keyboardDown );
    glutKeyboardUpFunc( keyboardUp );

    // ...
}