【发布时间】:2013-12-11 05:03:40
【问题描述】:
我正在尝试使用 LWJGL 创建一个基于体素的小型项目。该项目的一部分是在玩家移动时加载他们周围的小块景观。这个加载部分工作正常,但我遇到了一个问题,当我沿着 +X 轴行走时,沿着 -X 轴移动相同距离的景观块会加载。 Z 轴也会发生这种情况。
我很好奇,所以我尝试在块上反转 X 轴和 Z 轴的渲染方向,这似乎解决了这个问题。但是,我还决定将轴也渲染为线,并验证现在所有内容都正确绘制,我生成了以下图像:
(我显然无法嵌入图片,所以链接:http://i.imgur.com/y5hO1Im.png)
在此图像中,红色、蓝色和绿色线沿负轴绘制,而紫色、黄色和青色线沿正轴绘制。真正奇怪的是,图像显示相机在 +X 和 +Z 范围内,但在内部,相机的位置矢量在 -X 和 -Z 范围内。这对于为什么块在相反的轴上加载是有道理的,就好像相机在 +X 上渲染但内部位于 -X 的位置,然后将加载 -X 块。
所以我不确定这里发生了什么。我确定我缺少一个小的设置或不正确的正/负,但我似乎找不到任何东西。所以我想我的问题是,相机的内部位置是否正确渲染?如果是这样,我是否需要反转我渲染的所有内容?如果没有,相机中是否有明显可见的东西扰乱了渲染?
相关代码的一些sn-ps,尽量不让代码块溢出帖子
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(),这反过来会创建一个巨大的实体立方体,该立方体会在块的原点呈现。
如果需要,可以提供更多代码。
【问题讨论】: