Java OpenGL相机位置与渲染图像不匹配

Java OpenGL相机位置与渲染图像不匹配,java,opengl,lwjgl,Java,Opengl,Lwjgl,我正在用LWJGL创建一个基于体素的小项目。该项目的一部分是在玩家移动时加载玩家周围的小块景观。加载部分工作正常,但我遇到了一个问题,当我沿着+X轴行走时,沿-X轴移动相同距离的大块景观将加载。Z轴也会发生这种情况 我很好奇,所以我尝试在块上反转X轴和Z轴的渲染方向,这似乎解决了问题。但是,我还决定将轴渲染为直线,并验证所有内容现在都正确绘制,由此生成了以下图像: (我显然无法嵌入图像,所以链接:) 在此图像中,红色、蓝色和绿色线沿负轴绘制,而紫色、黄色和青色线沿正轴绘制。真正奇怪的是,图像显示

我正在用LWJGL创建一个基于体素的小项目。该项目的一部分是在玩家移动时加载玩家周围的小块景观。加载部分工作正常,但我遇到了一个问题,当我沿着+X轴行走时,沿-X轴移动相同距离的大块景观将加载。Z轴也会发生这种情况

我很好奇,所以我尝试在块上反转X轴和Z轴的渲染方向,这似乎解决了问题。但是,我还决定将轴渲染为直线,并验证所有内容现在都正确绘制,由此生成了以下图像:

(我显然无法嵌入图像,所以链接:)

在此图像中,红色、蓝色和绿色线沿负轴绘制,而紫色、黄色和青色线沿正轴绘制。真正奇怪的是,图像显示相机在+X和+Z范围内,但在内部,相机的位置向量在-X和-Z范围内。这就解释了为什么块在相反的轴上加载,就好像摄影机在+X上渲染,但在内部处于-X的位置,那么将加载-X块

所以我不确定这里发生了什么。我肯定有一个小的设置或不正确的积极/消极,我错过了,但我似乎找不到任何东西。所以我想我的问题是,相机的内部位置正确吗?如果是这样,我是否需要反转渲染的所有内容?如果没有,相机中是否有清晰可见的东西扰乱了渲染

一些相关的代码片段,试图不让文章中的代码块溢出

Camera.java

public class Camera {

    // Camera position
    private Vector3f position = new Vector3f(x, y, z);

    // Camera view properties
    private float pitch = 1f, yaw = 0.0f, roll = 0.0f;

    // Mouse sensitivity
    private float mouseSensitivity = 0.25f;

    // Used to change the yaw of the camera
    public void yaw(float amount) {
        this.yaw += (amount * this.mouseSensitivity);
    }

    // Used to change the pitch of the camera
    public void pitch(float amount) {
        this.pitch += (amount * this.mouseSensitivity);
    }

    // Used to change the roll of the camera
    public void roll(float amount) {
        this.roll += amount;
    }

    // Moves the camera forward relative to its current rotation (yaw)
    public void walkForward(float distance) {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw));
    }

    // Moves the camera backward relative to its current rotation (yaw)
    public void walkBackwards(float distance) {
        position.x += distance * (float)Math.sin(Math.toRadians(yaw));
        position.z -= distance * (float)Math.cos(Math.toRadians(yaw));
    }

    // Strafes the camera left relative to its current rotation (yaw)
    public void strafeLeft(float distance) {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));
        position.z += distance* (float)Math.cos(Math.toRadians(yaw-90));
    }

    // Strafes the camera right relative to its current rotation (yaw)
    public void strafeRight(float distance) {
        position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));
        position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));
    }

    // Translates and rotates the matrix so that it looks through the camera
    public void lookThrough() {
        GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
        GL11.glTranslatef(position.x, position.y, position.z);
    }
}
Main.java呈现代码

private void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();

        // Set the view matrix to the player's view
        this.player.lookThrough();

        // Render the visible chunks
        this.chunkManager.render();

        // Draw axis
        GL11.glBegin(GL11.GL_LINES);

            // X Axis
            GL11.glColor3f(1, 0, 0);
            GL11.glVertex3f(-100, 0, 0);
            GL11.glVertex3f(0, 0, 0);

            GL11.glColor3f(1, 1, 0);
            GL11.glVertex3f(0, 0, 0);
            GL11.glVertex3f(100, 0, 0);

            // Y Axis
            GL11.glColor3f(0, 1, 0);
            GL11.glVertex3f(0, -100, 0);
            GL11.glVertex3f(0, 0, 0);

            GL11.glColor3f(0, 1, 1);
            GL11.glVertex3f(0, 0, 0);
            GL11.glVertex3f(0, 100, 0);

            // Z Axis
            GL11.glColor3f(0, 0, 1);
            GL11.glVertex3f(0, 0, -100);
            GL11.glVertex3f(0, 0, 0);

            GL11.glColor3f(1, 0, 1);
            GL11.glVertex3f(0, 0, 0);
            GL11.glVertex3f(0, 0, 100);
        GL11.glEnd();

        // Render the origin
        this.origin.render();
    }
chunkManager.render()只需遍历每个加载的块并对其调用.render(),这反过来会创建一个巨大的实心立方体,并在块的原始位置进行渲染

如果需要,可以提供更多代码。

替换

GL11.glTranslatef(position.x, position.y, position.z);


想想看,你想把世界转换成摄像机所在位置的倒数,这样0,0,0就是摄像机所在位置。

是的,这是对的。回顾OpenGL管道,平移是为了移动世界,而不是相机本身,所以它必须是负位置。我的疏忽太愚蠢了,谢谢!
GL11.glTranslatef(-position.x, -position.y, -position.z);