Sean Niemann
Sean Niemann

Reputation: 55

OpenGL issue with coordinates when using glRotatef()

I am a beginner when it comes to OpenGL (using LWJGL), and am trying to apply a rotation of 90 degrees to a model. The problem is that when the rotation is applied to the model, it also appears to alter its coordinates, resulting in the model being placed out of bounds. I have tried translating the coordinates, but am unsure where to use this method, nor do I know which parameters to use for glTranslate(). Here is the following code snippet that I am using:

public void renderModel(int index) {
    Model model = Editor.models.get(index);
    glMatrixMode(GL_MODELVIEW);

    // apply rotation
    float rotation = 90f;
    glRotate(rotation, 0, 1, 0);

    for (int triangle = 0; triangle < model.face_cnt; triangle++) {
        if (model.face_verts.get(triangle).length == 3) {
            glBegin(GL_TRIANGLES);
        } else {
            glBegin(GL_QUADS);
        }
        for (int i = 0; i < model.face_verts.get(triangle).length; i++) {
                int point_a = model.face_verts.get(triangle)[i];

                float modelX = (float)((model.vert_x.get(point_a)) + x);
                float modelZ = (float)((model.vert_y.get(point_a)) - z4);
                float modelY = (float)((model.vert_z.get(point_a)) + y);

                glVertex3f(modelX, -modelZ, -modelY); // draw
        }
        glEnd();
    }
}

Upvotes: 1

Views: 67

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

Do not add the translation to the vertex coordinates (remove + x, - z4 and + y).

Rotate the model and then translate it. glTranslate has to be de done before glRotate, because the legacy OpenGL matrix operations specify a matrix and multiply the current matrix by the new matrix:

public void renderModel(int index) {
    Model model = Editor.models.get(index);
    glMatrixMode(GL_MODELVIEW);

    // apply rotation
    float rotation = 90f;

    glTranslate(x, -z4, y);
    glRotate(rotation, 0, 1, 0);

    for (int triangle = 0; triangle < model.face_cnt; triangle++) {
        if (model.face_verts.get(triangle).length == 3) {
            glBegin(GL_TRIANGLES);
        } else {
            glBegin(GL_QUADS);
        }
        for (int i = 0; i < model.face_verts.get(triangle).length; i++) {
                int point_a = model.face_verts.get(triangle)[i];

                float modelX = (float)((model.vert_x.get(point_a)));
                float modelZ = (float)((model.vert_y.get(point_a)));
                float modelY = (float)((model.vert_z.get(point_a)));

                glVertex3f(modelX, -modelZ, -modelY); // draw
        }
        glEnd();
    }
}

Upvotes: 1

Related Questions