Search code examples
transformlwjgl

why LWJGL polygon disappears after z translation?


I've started to learn lwjgl and got a problem! What I am doing:

  1. load a texture

  2. start rendering cycle

  3. draw rectangle and apply texture

  4. check for keyboard and mouse events and rotate/move camera

    public static void main(String[] args) {
    try {
        Display.setDisplayMode(new DisplayMode(320, 200));
        Display.create();
    } catch (Exception e) {
        System.out.println(e);
    }
    
    Texture texture = null;
    try {
        texture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("basic.jpg"), true);
    } catch (Exception e) {
        System.out.println(e);
        return;
    }
    
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 320, 0, 200, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    
    while (!Display.isCloseRequested()) {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        Color.white.bind();
        texture.bind();
        GL11.glBegin(GL11.GL_QUAD_STRIP);
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex3f(100, 100, 0);
            GL11.glTexCoord2f(0, 1);
            GL11.glVertex3f(100, 140, 0);
            GL11.glTexCoord2f(1, 1);
            GL11.glVertex3f(140, 140, 0);
            GL11.glTexCoord2f(1, 0);
            GL11.glVertex3f(140, 100, 0);
        GL11.glEnd();
    
        Display.update();
        processInput();
        try {
            //Thread.sleep(20);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    
    Display.destroy();
    
    }
    
    public static void processInput() {
    long delta = getDelta();
    long divider = 10000000;
    float camx = 0, camy = 0, camz = 0;
    float roll = 0;
    if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
        camz += 1.0f * delta / divider;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
        camz -= 1.0f * delta / divider;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
        camx -= 1.0f * delta / divider;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
        camx += 1.0f * delta / divider;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
        camy -= 1.0f * delta / divider;
    }
    if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
        camy += 1.0f * delta / divider;
    }
    if (Mouse.isButtonDown(0)) {
        roll += 1.0f * delta / divider;
    }
    if (Mouse.isButtonDown(1)) {
        roll -= 1.0f * delta / divider;
    }
    GL11.glTranslatef(camx, camy, camz);
    GL11.glTranslatef(160, 100, 0);
    GL11.glRotatef(roll, 0, 0, 1);
    GL11.glTranslatef(-160, -100, 0);
    }
    

When I rotate and move everything in the XY plane it works perfectly. But when I try moving along Z axis the whole rectangle disappears. What I am doing wrong?


Solution

  • Ok, I got a solution by myself after looking at initial parameters. The

    GL11.glOrtho(0, 320, 0, 200, 1, -1);
    

    function defines rendering are and everything out of this box won't be rendered. Thus, after moving along z axis the item disappears. I changed render box to

    GL11.glOrtho(0, 320, 0, 200, 100, -100);
    

    and that works.