gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:42 25 4
gpt4 key购买 nike

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

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

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

在此图像中,红色、蓝色和绿色线条沿负轴绘制,而紫色、黄色和青色线条沿正轴绘制。真正奇怪的是图像显示相机在 +X 和 +Z 范围内,但在内部,相机的位置 vector 在 -X 和 -Z 范围内。这对于为什么 block 加载在相反的轴上是有道理的,就好像相机在 +X 上渲染但在内部位于 -X 的位置,那么 -X block 将被加载。

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

相关代码的一些片段,尽量不要让代码块溢出帖子

相机.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() 只是遍历每个加载的 block 并在它们上调用 .render(),这反过来创建一个巨大的固体立方体,在 block 的原点处呈现。

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

最佳答案

替换

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

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

想一想,您想将世界转换为相机所在位置的倒数,以便 0,0,0 就是相机所在的位置。

关于java - OpenGL 相机位置与渲染图像不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20510924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com