关于java:glTranslatef不随z轴移动

glTranslatef not moving with z axis

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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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透视图中看到事物。