将2D对象移动到OpenGL中的一个点

时间:2012-07-30 23:47:29

标签: c++ c opengl

如何使用OpenGL在点的方向上移动2D对象(不是GL_POINTS,而是坐标)?


为了更好地理解我的代码:

我已将大部分代码拆分为不同的源代码,但这是实际创建形状和设置场景的代码:

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:很抱歉,如果我听起来像菜鸟,但我只是从Java转向C ++,所以我可以做一些跨平台的游戏。此外,我从未开发过游戏,只有一些Android,iOS和BlackBerry应用程序。


解决方案

感谢@paddy,我试图了解glTranslatef的使用并附带解决方案。这是工作代码,它将创建一个100x100的正方形,并将其移动到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帮助我。的:)

1 个答案:

答案 0 :(得分: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矩阵做这个。