在OpenGL中将2D对象移动到点

lvjbypge  于 2022-11-04  发布在  其他
关注(0)|答案(2)|浏览(152)

如何使用OpenGL在点的方向(不是GL_POINTS,而是坐标)移动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.
}
daupos2t

daupos2t1#

你需要转换modelview矩阵。假设你已经在modelview模式下了:

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

ryhaxcpt

ryhaxcpt2#

  • 代表OP回答:*

Thanks @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.
}

相关问题