[英]3D Camera class not working like it should
有人可以告訴我為什么我的相機課不能正常工作嗎? 我將位置向量設置為(0,0,-10),將向量視圖設置為(0,0,0),但是當我在(0,0,0)上繪制內容時,它就不存在了。 我對向量數學和矩陣知識非常陌生,所以我敢打賭問題出在它的LookThrough()上。
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Matrix3f;
import org.lwjgl.util.vector.Vector3f;
public class Camera {
//3d vector to store the camera's position in
Vector3f position = null;
Vector3f lookAt = null;
//the rotation around the Y axis of the camera
float yaw = 0;
//the rotation around the X axis of the camera
float pitch = 0;
//the rotation around the Z axis of the camera
float roll = 0;
public Camera(float x, float y, float z)
{
//instantiate position Vector3f to the x y z params.
position = new Vector3f(x, y, z);
lookAt = new Vector3f();
}
public void lookThrough()
{
Matrix3f m = new Matrix3f();
Vector3f out = new Vector3f();
Vector3f.sub(position, lookAt, out);
out.normalise();
//set forward vector
m.m00 = out.x;
m.m01 = out.y;
m.m02 = out.z;
//set right vector
m.m10 = 1;
m.m11 = 0;
m.m12 = 0;
//set up vector
m.m20 = 0;
m.m21 = 1;
m.m22 = 0;
yaw = (float) -(Math.tan(m.m10/m.m00));
pitch = (float) -(Math.tan((-m.m20)/(Math.sqrt(Math.pow(m.m21, 2) + Math.pow(m.m22, 2)))));
roll = (float) -(Math.tan(m.m21/m.m22));
//roatate the pitch around the X axis
GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
//roatate the yaw around the Y axis
GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
//roatate the yaw around the Y axis
GL11.glRotatef(roll, 0.0f, 0.0f, 1.0f);
//translate to the position vector's location
GL11.glTranslatef(position.x, position.y, position.z);
}
}
關於您的課程,我需要強調以下幾點:
1)您正在固定“向右”和“向上”向量,僅保留一個旋轉自由度。 當相機在3D空間中重新定向時,將需要更改這些方向。 就目前而言,您對俯仰的計算始終為0,而對滾動的計算始終為tan(1/0)。
2)盡管我對Java不熟悉,但是您似乎正在使用計算(位置-lookAt)來導出正向向量。 應該不是相反嗎? 前向矢量指向遠離觀看者位置的位置。
3)再次-不熟悉Java-但調用pow()進行單次乘法可能會過大。
1&2可能是您造成麻煩的原因。 最終,我建議您看一下gluLookAt-假設GLU庫在Java中可用。 如果不是,請查看gluLookAt的源代碼( 此處提供一個變體)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.