1

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


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

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

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 和黑莓应用程序。


解决方案

谢谢@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 帮助我。:)

4

1 回答 1

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 矩阵也执行此操作。

于 2012-07-30T23:52:27.560 回答