我正在玩LWJGL創建一個基於體素的小項目。該項目的一部分是在玩家移動時在其周圍加載小塊的景觀。這個工程的加載部分沒問題,但是我遇到了一個問題,當我沿着+ X軸行走時,沿着-X軸移動相同距離的風景塊將加載。這也會發生在Z軸上。OpenGL相機位置與渲染圖像不匹配
我很好奇,所以我試着在塊上反轉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()只是遍歷每個加載的塊並調用.rende r(),這反過來又創建了一個巨大的實體立方體,它在塊的原點處呈現。
如果需要,可以提供更多的代碼。
是的,這是對的。回顧OpenGL管道,翻譯是爲了推動世界,而不是相機本身,所以它必須是負面的。愚蠢的疏忽,謝謝! – user3089564