【发布时间】:2019-09-17 05:17:11
【问题描述】:
我一直在开发类似 Minecraft 的体素游戏,并决定通过光线投射来实现方块放置和破坏。然而,我意识到,当站在非常靠近一个方块时,方块会围绕自身而不是相机(或观察平面)旋转。眼睛位置偏离了大约。在任何给定轴上为 0.5。我很确定这就是为什么我的光线投射不能精确工作的原因,我认为问题在于投影。
我尝试过使用不同的投影方法,但它们都有相同的结果。在创建视图矩阵时调整相机的位置也不起作用,并导致一些奇怪的拉伸和错误的旋转。
我的投影矩阵代码:
public static Matrix4f createProjectionMatrix (float fov, float aspectRatio, float clipNear, float clipFar) {
float tanHalfFOV = (float)Math.tan(fov/2);
float zm = clipFar + clipNear;
float zp = clipFar - clipNear;
Matrix4f matrix = new Matrix4f();
matrix.m00(1.0f / (tanHalfFOV * aspectRatio));
matrix.m11(1.0f / tanHalfFOV);
matrix.m22(-zm / zp);
matrix.m32(-2.0f * clipNear * clipFar / zp);
matrix.m23(-1.0f);
return matrix;
}
视图矩阵代码:
public static Matrix4f getViewMatrix () {
return new Matrix4f().rotateXYZ(rotation).translate(-position.x, -position.y, -position.z);
}
着色器代码:
#version 400 core
in vec3 worldCoords;
in vec2 textureCoords;
out vec2 passTextureCoords;
uniform mat4 projViewMatrix;
void main () {
gl_Position = projViewMatrix * vec4(worldCoords, 1.0);
passTextureCoords = textureCoords;
}
渲染代码:
public static void render (ChunkMesh cm) {
Renderable model = cm.getModel();
Shaders.CHUNK_MESH_SHADER.activate();
Shaders.CHUNK_MESH_SHADER.loadToUniform("projViewMatrix", Display.getProjectionMatrix().mul(Camera.getViewMatrix(), new Matrix4f()));
GL30.glBindVertexArray(model.vaoID);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, BlockTexture.TEXTURE.textureID);
GL11.glDrawElements(GL11.GL_TRIANGLES, model.vertexCount, GL11.GL_UNSIGNED_INT, 0l);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
Shaders.CHUNK_MESH_SHADER.deactivate();
}
在任何给定的轴上,光线投射都表现得很好,但是当面对两个轴之间时,光线投射看起来就像它的原点与屏幕的中心不同。由于我的问题集中在未对齐而不是光线投射(这似乎有效)上,因此我只会在需要时添加代码。任何帮助表示赞赏!
【问题讨论】:
标签: java opengl projection-matrix