Search code examples
javaapilwjgl

lwjgl, rotating only one object on screen?


So I am trying to only rotate one object, I have read other posts about how to do so but all of them just say something like this: 1. call glLoadIdentity(); 2. draw shape 3. rotate

I have tried what they tell me to do but it doesn't seem to work for me?

if (time != faces.size() - 1 && faces.size() != 1){
            if (faces.get(time+1).needsIdentity){
                GL11.glLoadIdentity();
                System.out.println("The not last identity was set!");
            }
            System.out.println("got identity");
        }else{
            if (faces.get(faces.size() - 1).needsIdentity){
                GL11.glLoadIdentity();
                System.out.println("identity set!");
            }
            System.out.println("got last identity");
        }


        GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(f.clr.red, f.clr.green, f.clr.blue);
        GL11.glVertex3f(f.loc.x - f.x, f.loc.y +  f.y, f.loc.z + f.z);
        GL11.glVertex3f(f.loc.x + f.x, f.loc.y + f.y, f.loc.z + f.z);
        GL11.glVertex3f(f.loc.x + f.x, f.loc.y - f.y, f.loc.z + f.z);
        GL11.glVertex3f(f.loc.x - f.x, f.loc.y - f.y, f.loc.z + f.z);
        GL11.glEnd();
        finished();
    }

public void finished(){
    GL11.glRotatef(rs.rotx, 1F, 0F, 0F);
    GL11.glRotatef(rs.roty, 0F, 1F, 0F);
    GL11.glRotatef(rs.rotz, 0F, 0F, 1F);
    System.out.println("rotated");
}

This is my code. in the array called faces are 4 quads 3 of them have needsIdentity false and one of them have it true, also the one I'm trying to rotate. I have put in the print lines to check if it gets the identity, which it does. Also to times 1 gets added every round.

Can you explane where exactly i have to call the glLoadIdentity()? You might want to know this but it rotates me instead of the object.


Solution

  • If you want to rotate only certain parts of a scene (In your case, one object), you can use glPushMatrix() and glPopMatrix(). This allows you to do transformations (Such as translating, rotating and scaling) and revert after you're done drawing the object you want to transform. Don't bother with glLoadIdentity() - That resets all transformations. The reason it probably rotates you is because you probably draw yourself after calling glRotatef(r, x, y, z) several times.

    //Draw some objects here - not rotated
    
    GL11.glPushMatrix(); //Push the matrix onto the stack
    
    //Rotate the object about to be drawn
    GL11.glRotatef(rs.rotx, 1F, 0F, 0F);
    GL11.glRotatef(rs.roty, 0F, 1F, 0F);
    GL11.glRotatef(rs.rotz, 0F, 0F, 1F);
    
    //Draw the object
    GL11.glBegin(GL11.GL_QUADS);
    GL11.glColor3f(f.clr.red, f.clr.green, f.clr.blue);
    GL11.glVertex3f(f.loc.x - f.x, f.loc.y +  f.y, f.loc.z + f.z);
    GL11.glVertex3f(f.loc.x + f.x, f.loc.y + f.y, f.loc.z + f.z);
    GL11.glVertex3f(f.loc.x + f.x, f.loc.y - f.y, f.loc.z + f.z);
    GL11.glVertex3f(f.loc.x - f.x, f.loc.y - f.y, f.loc.z + f.z);
    GL11.glEnd();
    
    GL11.glPopMatrix(); //Pop the matrix off of the stack
    
    //Draw some more objects here - not rotated