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

C++ OpenGL/C中“2D”摄像机的运动++,c++,opengl,glut,opengl-compat,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 = 100

我已经检查了这个网站上所有其他相关的问题,但没有一个解决方案对我有效。我只是试着跟随我的矩形,它在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);

在我的键盘功能中,什么都不做。如果我尝试使用GL_投影,它会使我的屏幕变黑。

首先要注意的是,按顺序绘制和固定函数矩阵堆栈几十年来一直被弃用。见和

简化事情

为上下键/glutKeyboardUpFunc添加键盘事件。此功能仅修改玩家的速度。按下按钮时设置速度,松开按钮时设置速度为0:

int playerSpeed=0; void keyboarddown无符号字符键,int x,int y { 如果键=='a' 运动员速度-=3; 如果key='d' 运动员速度=3; } void keyboardUp无符号字符键,int x,int y { playerSpeed=0; } 计时器事件只是修改播放器的位置:

无效更新值 { playerX+=玩家速度; GLUTTIMERFUNCENTERVAL,更新,0; 再发现; } 在“绘制”中,设置模型矩阵并绘制矩形:

抽真空 { glClearGL_颜色_缓冲_位| GL_深度_缓冲_位; glMatrixModeGL_模型视图; glLoadIdentity; glTranslatefplayerX,playerY,0; //平局球员 drawRect0,0,playerWidth,playerHught; //将缓冲区交换到终端 狼吞虎咽; } 在main中设置回调函数:

int main argc,字符**argv { // ... GLUT绘制; GLUTTIMERFUNCENTERVAL,更新,0; glutKeyboardFunc键盘向下; glutKeyboardUpFunc键盘Up; // ... }
首先要注意的是,几十年来,按序列绘制和固定函数矩阵堆栈一直被弃用。见和

简化事情

为上下键/glutKeyboardUpFunc添加键盘事件。此功能仅修改玩家的速度。按下按钮时设置速度,松开按钮时设置速度为0:

int playerSpeed=0; void keyboarddown无符号字符键,int x,int y { 如果键=='a' 运动员速度-=3; 如果key='d' 运动员速度=3; } void keyboardUp无符号字符键,int x,int y { playerSpeed=0; } 计时器事件只是修改播放器的位置:

无效更新值 { playerX+=玩家速度; GLUTTIMERFUNCENTERVAL,更新,0; 再发现; } 在“绘制”中,设置模型矩阵并绘制矩形:

抽真空 { glClearGL_颜色_缓冲_位| GL_深度_缓冲_位; glMatrixModeGL_模型视图; glLoadIdentity; glTranslatefplayerX,playerY,0; //平局球员 drawRect0,0,playerWidth,playerHught; //将缓冲区交换到终端 狼吞虎咽; } 在main中设置回调函数:

int main argc,字符**argv { // ... GLUT绘制; GLUTTIMERFUNCENTERVAL,更新,0; glutKeyboardFunc键盘向下; glutKeyboardUpFunc键盘Up; // ... }
只是出于好奇,你为什么要学习20多年的API?@Quimby你的问题让我有点不安。OpenGL可能很旧,但它过时了吗?当代的替代方案是什么?到目前为止,我还不是专家,但我认为opengl和directx是这一类别中唯一的两个竞争对手…@Olli opengl绝对不是过时的,但OP使用的是1992年推出的版本1.0,现在已经被弃用。这个API并不真正适合现代GPU,缺少许多功能,因此应该使用现代版本——通常是3.3+左右。目前最新版本为2017年的4.6版。OpenGL可能会逐渐被Vulkan取代,但那是几年后的事了。只是出于好奇,你为什么要学习超过20年的API?@Quimby你的问题让我有点不安。OpenGL可能很旧,但它过时了吗?当代的替代方案是什么?到目前为止,我还不是专家,但我认为opengl和directx是这一类别中唯一的两个竞争对手…@Olli opengl绝对不是过时的,但OP使用的是1992年推出的版本1.0,现在已经被弃用。这个API并不真正适合现代GPU,缺少许多功能,因此应该使用现代版本——通常是3.3+左右。目前最新版本为2017年的4.6版。OpenGL可能会逐渐被Vulkan取代,但那是几年后的事了。谢谢你,Rabbid。移动身份;更新循环之外修复了。谢谢你,拉比。移动身份;在更新循环之外修复了它。