Java 3D相机类无法正常工作
有人能告诉我为什么我的相机课不能正常工作吗?我将位置向量设置为(0,0,-10),将注视向量设置为(0,0,0),但当我在(0,0,0)上绘制某个对象时,它不在那里。我对向量数学和矩阵之类的东西很陌生,所以我打赌问题出在LookThrough()上Java 3D相机类无法正常工作,java,math,opengl,vector,matrix,Java,Math,Opengl,Vector,Matrix,有人能告诉我为什么我的相机课不能正常工作吗?我将位置向量设置为(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 Cam
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,但您似乎正在使用计算(position-lookAt)来推导前向向量。难道不是相反吗?前向向量指向远离观察者的位置 3) 同样,虽然不熟悉java,但调用pow()执行一次乘法可能有些过分 1和2可能是你痛苦的原因。最后,我建议看一下gluLookAt——假设GLU库在Java中可用。如果不是,请查看gluLookAt的源代码(有一个变体可用)