z翻译后为什么LWJGL多边形会消失?

时间:2013-02-12 11:31:06

标签: transform lwjgl

我开始学习lwjgl并遇到问题! 我在做什么:

  1. 加载纹理

  2. 开始渲染周期

  3. 绘制矩形并应用纹理

  4. 检查键盘和鼠标事件以及旋转/移动相机

    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);
    }
    
  5. 当我旋转并移动XY平面中的所有内容时,它可以完美地工作。但是当我尝试沿着Z轴移动时,整个矩形消失了。我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,在查看初始参数后,我自己找到了解决方案。

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

函数定义渲染,并且此框外的所有内容都不会被渲染。因此,在沿z轴移动后,该项目消失。我将渲染框更改为

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

并且有效。