我想在按下左箭頭鍵時使主體(方形)向左移動。不幸的是,它在數據結構中,我不知道要在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,所以我強烈建議直接去「現代」。 –