Z axis trouble with glTranslatef(...) - LWJGL

Posted by Zarkopafilis on Game Development See other posts from Game Development or by Zarkopafilis
Published on 2013-11-05T14:05:56Z Indexed on 2013/11/05 16:14 UTC
Read the original article Hit count: 179

Filed under:
|
|

Here is the code:

private static boolean up = true , down = false , left = false , right = false, reset = false, in = false , out = false;

 public void start() {
        try {
        Display.setDisplayMode(new DisplayMode(800,600));
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(0);
    }

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 800, 0, 600, 0.00001f, 1000);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);



    Keyboard.enableRepeatEvents(true);


    while (!Display.isCloseRequested()) {


      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);   

      input();

      if(up){
          GL11.glTranslatef(0,0.1f,0);
      }
      if(down){
          GL11.glTranslatef(0,-0.1f,0);
      }
      if(left){
          GL11.glTranslatef(-0.1f,0,0);
      }
      if(right){
          GL11.glTranslatef(0.1f,0,0);
      }
      if(in){
          GL11.glTranslatef(0, 0, 1f);
      }
      if(out){
          GL11.glTranslatef(0, 0, -1f);
      }
      if(reset){
          GL11.glLoadIdentity();
      }

      GL11.glBegin(GL11.GL_QUADS);

      GL11.glColor3f(255, 255, 255);
      GL11.glVertex3f(800/2, 600/2, 0);
      GL11.glVertex3f(800/2 + 200, 600/2, 0);
      GL11.glVertex3f(800/2 + 200, 600/2 + 200, 0);
      GL11.glVertex3f(800/2, 600/2 + 200, 0);

      GL11.glColor3f(0, 255, 0);
      GL11.glVertex3f(800/2, 600/2, 1);
      GL11.glVertex3f(800/2 + 200, 600/2, 1);
      GL11.glVertex3f(800/2 + 200, 600/2 + 200, 1);
      GL11.glVertex3f(800/2, 600/2 + 200, 1);

      GL11.glEnd();


        Display.update();
    }

    Display.destroy();
    }


public static void main(String[] argv){
     new main().start();
 }


 public void input(){
     up = false;
     down = false;
     left = false;
     right = false;
     reset = false;
     in = false;
     out = false;

     if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
         reset = true;
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_W)){
        up = true;
     } 
     if(Keyboard.isKeyDown(Keyboard.KEY_S)){
        down = true;
     }
     if(Keyboard.isKeyDown(Keyboard.KEY_A)){
        left = true;
     }
     if(Keyboard.isKeyDown(Keyboard.KEY_D)){
        right = true;
     }
     if(Keyboard.isKeyDown(Keyboard.KEY_Q)){
         in = true;
     }
     if(Keyboard.isKeyDown(Keyboard.KEY_E)){
         out = true;
     }
 }

As you can see I am creating 2 quads , a white one at z 0 and a green one at z 1. WASD keys function correctly. Also when I hit SPACEBAR the white quad is being shown. If I then press E , I can see the green quad. But if I press Q afterwards , I dont see the white one again!(Space Works everytime). Also if I render the green one at Z = -1 everything works perfectly BUT you may need up to 3 key presses Q/E to see the other quad. Why is that happening?

© Game Development or respective owner

Related posts about opengl

Related posts about java