2012-07-30 54 views
1

如何使用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帮助我。 :)

+0

[你有什么尝试?](http://www.whathaveyoutried.com) – 2012-07-30 23:50:30

+0

@代码大师:有一点帮助,我得到了它的工作。谢谢。 – 2012-07-31 01:06:14

+0

请注意,您并不需要推送和弹出矩阵调用。他们在那里隔离一个操作或者从世界空间建立相关转换。如果你总是在世界空间工作,只需改写矩阵即可。 – paddy 2012-07-31 01:54:06

回答

3

您需要翻译模型视图矩阵。假设你在模型视图模式已经:

glPushMatrix(); 
glTranslatef(x, y, z); 
// Draw your shape 
glPopMatrix(); 

[编辑]

@paddy:像这样的事情?我试过这个,但广场不动。 pastebin.com/2PCsy5kC

尝试明确选择模型视图矩阵。你的例子并没有告诉我们,这是该模式目前在:

glSetMatrixMode(GL_MODELVIEW); 
glPushMatrix(); 
glTranslatef(x, y, z); 
// Draw your shape 
glPopMatrix(); 

通常在开始渲染您重置一切......所以,你进入GL_PROJECTION模式,呼叫glLoadIdentity()将其复位,并设置你的相机,那么对GL_MODELVIEW矩阵也要这样做。

+0

这是假设@Nathan正在使用即时模式,不是吗? – 2012-07-30 23:53:50

+0

@TimCooper:不,这可以在任何你称之为非即时模式的情况下工作。 – 2012-07-30 23:55:52

+0

@paddy:是这样的吗?我试过这个,但广场不动。 http://pastebin.com/2PCsy5kC – 2012-07-31 00:00:58

相关问题