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

时间:2013-12-11 05:03:41

标签: java opengl lwjgl

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

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

(显然我无法嵌入图片,所以链接:http://i.imgur.com/y5hO1Im.png

在此图像中,红色,蓝色和绿色线沿负轴绘制,而紫色,黄色和青色线沿正轴绘制。真正奇怪的是,图像显示摄像机处于+ 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(),这反过来会创建一个巨大的立方体,在块的原点呈现。

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

1 个答案:

答案 0 :(得分:1)

替换

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

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

想一想,你想把世界翻译成相机的倒数,这样0,0,0就是相机的位置。