gpt4 book ai didi

c++ - '2D' 相机在 OpenGL/C++ 中的移动

转载 作者:行者123 更新时间:2023-11-30 02:16:08 25 4
gpt4 key购买 nike

我已经检查了该站点上的所有其他相关问题,但没有一个解决方案对我有用。我只是想跟随我的矩形,它在 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,它会使我的屏幕变黑。

最佳答案

首先请注意,glBegin/glEnd 绘制的那幅画sequences 和 fixed function matrix stack 几十年来一直被弃用。参见 Fixed Function PipelineLegacy OpenGL .


简化事情。

添加按键向上和向下的键盘事件(glutKeyboardFunc/glutKeyboardUpFunc)。此功能仅修改播放器的速度。当按下按钮并设置为 0 时设置速度,当释放按钮时:

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;
}

定时器事件只是修改玩家的位置:

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 );

// ...
}

关于c++ - '2D' 相机在 OpenGL/C++ 中的移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55356028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com