当我按下向左箭头键时,我想让 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/