如何使用OpenGL在點的方向上移動2D對象(不是GL_POINTS
,但座標)?將2D對象移動到OpenGL中的一個點上
爲了更好地理解我的代碼:
我splited我的大部分代碼轉換成不同的源代碼,但是這是實際創造的形狀和設置的場景之一:
void setupScene(int clearColor[]) {
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
//glClearColor(250, 250, 250, 1.0); // Set the cleared screen colour to black.
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window.
// Set up the orthographic projection so that coordinates (0, 0) are in the top left.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10);
// Back to the modelview so we can draw stuff.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer.
}
void drawScene() {
setupScene((int[]){250, 250, 250, 1});
triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT);
glBegin(GL_QUADS);
glColor3f(RGB(80), RGB(80), RGB(80));
glPushMatrix();
glTranslatef(400, 400, 0);
glVertex2d(200, 100);
glVertex2d(100, 100);
glVertex2d(100, 200);
glVertex2d(200, 200);
glPopMatrix();
glEnd();
glutSwapBuffers(); // Send the scene to the screen.
}
void update(int value) {
glutPostRedisplay(); // Tell GLUT that the display has changed.
glutTimerFunc(25, update, 0); // Tell GLUT to call update again in 25 milliseconds.
}
PS:對不起,如果我聽起來像一個noob,但我只是從Java到C++,所以我可以做一些跨平臺的遊戲。此外,我從來沒有開發過遊戲,只是一些Android,iOS和BlackBerry應用程序。
解決方案
感謝@paddy,我試圖瞭解使用glTranslatef
並與解決方案來。這裏是工作的代碼,它會在100×100創建一個正方形,並將其移動到400x200:
void setupScene(int clearColor[]) {
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
//glClearColor(250, 250, 250, 1.0); // Set the cleared screen colour to black.
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); // This sets up the viewport so that the coordinates (0, 0) are at the top left of the window.
// Set up the orthographic projection so that coordinates (0, 0) are in the top left.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, -10, 10);
// Back to the modelview so we can draw stuff.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and depth buffer.
}
int a = 100;
int b = 200;
int x = 100;
int y = 100;
void drawScene() {
setupScene((int[]){250, 250, 250, 1});
triangle(210, WINDOW_WIDTH, WINDOW_HEIGHT);
glPushMatrix();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glColor3f(RGB(80), RGB(80), RGB(80));
glVertex2d(b, a);
glVertex2d(a, a);
glVertex2d(a, b);
glVertex2d(b, b);
glEnd();
glPopMatrix();
glutSwapBuffers(); // Send the scene to the screen.
}
void update(int value) {
if (x != 400 && y != 200) {
x += 4;
y += 2;
}
glutPostRedisplay(); // Tell GLUT that the display has changed.
glutTimerFunc(25, update, 0); // Tell GLUT to call update again in 25 milliseconds.
}
OpenGL是很多比我想象中的簡單。因爲我總是對C/C++感到恐懼,因爲我習慣Java和JavaScript,但我真的很喜歡它的真棒,簡單和優雅。謝謝@paddy幫助我。 :)
[你有什麼嘗試?](http://www.whathaveyoutried.com) – 2012-07-30 23:50:30
@代碼大師:有一點幫助,我得到了它的工作。謝謝。 – 2012-07-31 01:06:14
請注意,您並不需要推送和彈出矩陣調用。他們在那裏隔離一個操作或者從世界空間建立相關轉換。如果你總是在世界空間工作,只需改寫矩陣即可。 – paddy 2012-07-31 01:54:06