Newbie Coordinates/Rendering trouble

Started by Zarkopafilis, November 06, 2013, 14:09:38

Previous topic - Next topic

Zarkopafilis

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? It seems that the quads are drawn somekindof top of eachother . I am alterning the Z at the coords. Why is this happening?

And my second problem is with gluPerspective. Everytime I am trying to draw a triangle or other shapes , I end up drawing them at top right of the window! (I am using the same booleans with the other code)
while (!Display.isCloseRequested()) {
			
	
		  GL11.glClearColor(0f, 0f, 0f, 0f);
		  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, 0.1f);
		  }
		  if(out){
			  GL11.glTranslatef(0, 0, -0.1f);
		  }
		  if(reset){
			  GL11.glLoadIdentity();
		  }
		  
		  GL11.glBegin(GL11.GL_TRIANGLES);
		  
		  GL11.glColor3f(0, 255, 0);
		  GL11.glVertex3f(50, 100, 10);
		  GL11.glVertex3f(10, 10, 10);
		  GL11.glVertex3f(1, 5, 10);
		  
		  GL11.glEnd();

		  Display.update();
		}

Please , can you help me?
Also an offtopic? Question, I am trying to learn OpenGL , I already know more of the basics of Java. I am confused . Shall I keep learning 1.1 or go straight to 4.X?Also , It seems stuff get removed while version number keeps increasing!
Thanks in advance

quew8

Your problem is your projection matrix. To begin with, Z values between 0.00001 and 1000. So the green is out but the white isn't. If you translate by 1, then it's between 1.00001 and 1001. So both quads are out of the question.

As for your gluPerspective() problem, you didn't actually how the code where you set up the projection matrix so there isn't much I can do. As for your other questions, look here: http://arcsynthesis.org/gltut/. It's an excellent tutorial that takes you through the basics whilst teaching modern OpenGL. @ra4king has even ported the source code to LWJGL if you have trouble reading C code or would like fully runnable examples. https://bitbucket.org/ra4king/lwjgl-shader-tutorials.