我正在尝试使用Java / LWJGL制作游戏/演示,并遇到第一人称摄影机的麻烦:
我使用WASD移动eye
向量,但是正在发生奇怪的事情:
当我将视线移开或移向我时,正确的响应。视图的左侧部分被我的视图遮挡。
当我将眼睛向右或向左移动时,顶部和顶部分别显示。视图的底部被我的视图遮挡了。
我已经成功实现了鼠标移动(环顾四周),现在相关代码如下:
(注意:Matrix4f类按列优先顺序工作)
Matrix4f viewMatrix = new Matrix4f();
private void checkKeys() {
if (isKeyCurrentlyDown(Keyboard.KEY_W)) {
eye.updateTranslate(1.0f, 0.0f, 0.0f);
updateView();
}
else if (isKeyCurrentlyDown(Keyboard.KEY_S)) {
eye.updateTranslate(-1.0f, 0.0f, 0.0f);
updateView();
}
else if (isKeyCurrentlyDown(Keyboard.KEY_A)) {
eye.updateTranslate(0.0f, -1.0f, 0.0f);
updateView();
}
else if (isKeyCurrentlyDown(Keyboard.KEY_D)) {
eye.updateTranslate(0.0f, 1.0f, 0.0f);
updateView();
}
}
private void updateView() {
System.out.println("eye = " + eye);
viewMatrix.identity().viewFPS(eye, roll, yaw, pitch);
System.out.println("viewMatrix = " + viewMatrix);
Uniforms.setUniformMatrix4(UNIFORM_VIEW_MATRIX, false, viewMatrix);
}
@Override
protected void render(final double msDelta) {
super.render(msDelta);
glClearColor(0.0f, 0.25f, 0.0f, 1.0f);
glClearDepthf(1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.use();
for (int i = 0; i < 24; i++) {
float fVar = i + currentTime / 1000f * 0.3f;
modelviewMatrix.identity()
.translate(0.0f, 0.0f, -8.0f)
.rotate(currentTime / 1000f * 45.0f, 0.0f, 1.0f, 0.0f)
.rotate(currentTime / 1000f * 21.0f, 1.0f, 0.0f, 0.0f)
.translate(
(float)Math.sin(2.1f * fVar) * 2.0f,
(float)Math.cos(1.7f * fVar) * 2.0f,
(float)Math.sin(1.3f * fVar) * (float)Math.cos(1.5f * fVar) * 2.0f
);
Uniforms.setUniformMatrix4(UNIFORM_MODEL_MATRIX, false, modelviewMatrix.writeToFloatBuffer(modelViewMatrixBuffer));
program.drawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
}
}
#version 440 core
layout(location = 0) in vec4 position;
out VS_OUT {
vec4 color;
} vs_out;
layout(location = 0) uniform mat4 model_matrix;
layout(location = 1) uniform mat4 view_matrix;
layout(location = 2) uniform mat4 proj_matrix;
void main() {
gl_Position = proj_matrix * view_matrix * model_matrix * position;
vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);
}
public enum UniformLocation {
UNIFORM_MODEL_MATRIX(0),
UNIFORM_VIEW_MATRIX(1),
UNIFORM_PROJECTION_MATRIX(2)
;
private final int location;
private UniformLocation(final int location) {
this.location = location;
}
public int getLocation() {
return this.location;
}
}
//Column-major order
public Matrix4f viewFPS(final Vector3f eye, final float rollAngle, final float yawAngle, final float pitchAngle) {
//roll = rolling your head, Q&E
//yaw = looking left/right, mouseY
//pitch = looking up/down, mouseX
float sinRoll = (float)Math.sin(Math.toRadians(rollAngle));
float cosRoll = (float)Math.cos(Math.toRadians(rollAngle));
float sinYaw = (float)Math.sin(Math.toRadians(yawAngle));
float cosYaw = (float)Math.cos(Math.toRadians(yawAngle));
float sinPitch = (float)Math.sin(Math.toRadians(pitchAngle));
float cosPitch = (float)Math.cos(Math.toRadians(pitchAngle));
Vector3f xAxis = new Vector3f(
cosYaw * cosPitch,
sinYaw * cosPitch,
-sinPitch
);
Vector3f yAxis = new Vector3f(
cosYaw * sinPitch * sinRoll - sinYaw * cosRoll,
sinYaw * sinPitch * sinRoll + cosYaw * cosRoll,
cosPitch * sinRoll
);
Vector3f zAxis = new Vector3f(
cosYaw * sinPitch * cosRoll + sinYaw * sinRoll,
sinYaw * sinPitch * cosRoll - cosYaw * sinRoll,
cosPitch * cosRoll
);
return multiply(
xAxis.getX(), xAxis.getY(), xAxis.getZ(), -xAxis.dot(eye), //X column
yAxis.getX(), yAxis.getY(), yAxis.getZ(), -yAxis.dot(eye), //Y column
zAxis.getX(), zAxis.getY(), zAxis.getZ(), -zAxis.dot(eye), //Z column
0.0f, 0.0f, 0.0f, 1.0f //W column
);
}
我真的不知道为什么相机表现得如此怪异,甚至意味着什么?
图片的部分怎么突然变得不可见?
更新:可能与
viewFPS()
方法有关,因为那里的翻译看起来有些奇怪,有人可以确认吗? 最佳答案
注意,在viewFPS
中,您将旋转和平移组合在一个矩阵乘法中。
我不熟悉构建矩阵的方式,但是我可以告诉您什么对我有用。
创建3个矩阵,其中1个用于X旋转,Y旋转和Z旋转值。
将这些矩阵按所需顺序相乘。 (我用Z,Y,然后用X)
将结果与您的翻译矩阵相乘。 (即您的眼睛位置)
结果矩阵就是您的视图矩阵。
这可能不如您所使用的方法有效,但是让它开始工作然后从那里进行优化很好吗?拆分旋转轴还具有能够一次调试1个轴的好处。
我使用的旋转矩阵:
其中,(phi)和(psi)是绕X,Y和Z轴的旋转。(http://www.fastgraph.com/makegames/3drotation/)