我想在按下左箭头键时使主体(方形)向左移动。不幸的是,它在数据结构中,我不知道要在void SpecialKeys(int key, int x, int y)
部分放置什么。在OpenGL中进行简单的形状移动(形状处于数据结构中)
#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)
{
}
}**
抱歉,这是非常基本的OpenGL您几乎可以从任何书籍或教程网站获得知识。你正在寻找的东西叫做模型 - 视图矩阵。这个想法是(在固定管道的opengl中)你将一个矩阵推到与你的所有顶点相乘的矩阵栈上。然后你可以通过调用例如glTranslate。例如看这个:http://nehe.gamedev.net/tutorial/rotation/14001/(但使用glTranslate而不是glRotate)。现代opengl的教程网站在这里:http://www.opengl-tutorial.org/ –
我上面的评论提到了一种新的和新的方式在opengl中做事。因为你显然刚开始使用opengl,所以我强烈建议直接去“现代”。 –