glTranslatef 不随 z 轴移动

glTranslatef not moving with z axis

我编写的应用程序在屏幕上绘制一个矩形。我想在 z 轴上移动这个矩形,但它不起作用。它消失了。我不知道如何修复它。

代码:

package game;


import org.lwjgl.opengl.GL;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;

public class Main {

    public static void main(String[] args) {
        glfwInit();
        System.out.println("Hello World!");
        long  window = glfwCreateWindow(1280   ,720,"Title",0,0);

        glfwMakeContextCurrent(window);
        glfwSwapInterval(1);
        GL.createCapabilities();

        ///////////////////////////////
        glTranslatef(0,0,5);
        ///////////////////////////////


        while(!glfwWindowShouldClose(window)){
            glfwPollEvents();
            glBegin(GL_QUADS);
                glVertex2f(-0.5f,0.5f);
                glVertex2f(-0.5f,-0.5f);
                glVertex2f(0.5f,-0.5f);
                glVertex2f(0.5f,0.5f);
            glEnd();
            System.out.println(z);
            glfwSwapBuffers(window);
        }
        System.exit(0 );
    }
}

默认情况下,仅显示立方体内 x、y、z 坐标为 -1 到 1 的内容。由于代码开头的 glTranslatef ,您实际上在坐标

处渲染了一个正方形

(-0.5f,0.5f,5), (-0.5f,-0.5f,5), (0.5f,-0.5f,5), (0.5f,0.5f,5)

它位于立方体之外,因此未显示。

要更改渲染对象的坐标,我建议查看 glOrthoglFrustum。使用 glFrustum,您还可以看到 3D 透视图。