c++ - 在 OpenGL 中制作一个简单的形状移动(形状在数据结构中)

标签 c++ variables opengl data-structures

当我按下向左箭头键时,我想让 body (正方形)向左移动。不幸的是,它在数据结构中,我不知道要在 void SpecialKeys(int key, int x, int y) 部分中放入什么才能使其移动。

#include <vector>
#include <time.h>

using namespace std;

#include "Glut_Setup.h"



**struct Vertex
{
float x,y,z;
};
Vertex Body []=
{
(-0.5, -2, 0),
(0.5, -2, 0),
(0.5, -3, 0),
(-0.5, -3, 0)
};**




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





glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-0.5, -2, 0);
glVertex3f(0.5, -2, 0);
glVertex3f(0.5, -3, 0); 
glVertex3f(-0.5, -3, 0);
glEnd();







glutSwapBuffers();
}

void Keys(unsigned char key, int x, int y)
{
switch(key)
{

}
}

**void SpecialKeys(int key, int x, int y)
{
switch(key)
{
}
}**

最佳答案

您只需调用 glTranslatef。

glClear(GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(delta_x, delta_y, -100.f);
//draw here
glPopMatrix();

关于c++ - 在 OpenGL 中制作一个简单的形状移动(形状在数据结构中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21660914/

相关文章:

javascript - JQuery 选择器中的变量不起作用

java - 是否可以将任何类型的输入传递给方法并确定给出的输入类型?

opengl - 更新 VBO 中的顶点数据(glBufferSubData vs glMapBuffer)

opengl - *BaseVertex 和 gl_VertexID

javascript - 软件 3d 引擎中的投影数学

c++ - 无法在 Visual Studio 代码中启动 C++ 调试器

c++ - 我应该使用哪个 C++ 单元测试框架来运行涉及 libcurl 的测试?

c++ - 我可以将空指针传递给 memcmp 吗?

c++ - out() 函数不起作用

javascript - javascript中自调用函数内变量的生命周期是多少