Search code examples
javaopengllwjgl

glRotatef works on multiple objects


Basicly I want to open a Door and I thought of using glRotatef. My Problem is that it is affecting every object which is drawn after it. Does anyone know how to stop that ?

Door.class

    public static void draw(Texture door) {  

    door.bind();
    if(Door_Test.state == "out" && d != 90){
        glRotatef(i, 0, 1, 0);
        i+=5;
    }
    glBegin(GL_QUADS);  
        glColor3f(1f, 1f, 1f);glTexCoord2f(0,0);glVertex3f(-2,3, -15);
        glColor3f(1f, 1f, 1f);glTexCoord2f(0,1);glVertex3f(-2,-3, -15);
        glColor3f(1f, 1f, 1f);glTexCoord2f(1,1);glVertex3f(2,-3, -15);
        glColor3f(1f, 1f, 1f);glTexCoord2f(1,0);glVertex3f(2,3, -15);
    glEnd();

}

Solution

  • When you rotate, it rotates the entire scene. So to rotate a single object, you rotate the entire scene, draw your object, then rotate your screen back.

    glRotatef(i, 0, 1, 0);
    
    // Draw object.
    
    glRotatef(-i, 0, 1, 0);
    

    As Reto Koradi pointed out, if you continually do this you might have floating point rounding errors that accumulate over time. HolyBlackCat's answer offers a better solution.