【发布时间】:2014-10-18 03:10:32
【问题描述】:
(作业相关问题)
我正在尝试通过this tutorial 实现阴影映射,但是,我遇到了一个奇怪的问题——我场景中的所有对象都应用了相同的阴影,如下所示: (灯光在 -3、5、-3(右下))
我做错了什么?我认为这将与矩阵相关,但我不知道如何解决它......
创建视图矩阵:
viewMatrix = new Matrix4f();
Matrix4f.rotate(MathHelpers.degreesToRadians(verticalRotation), new Vector3f(1f, 0f, 0f), viewMatrix, viewMatrix);
Matrix4f.rotate(MathHelpers.degreesToRadians(horizontalRotation), new Vector3f(0f, 1f, 0f), viewMatrix, viewMatrix);
Matrix4f.translate(position, viewMatrix, viewMatrix);
viewMatrix.store(cameraMatrix);
cameraMatrix.flip();
将数据发送到 GPU:
Mat4 proj2 = Matrices.perspective(90, 1, 3f, 20f);
Mat4 view2 = = MathHelpers.Matrix4fToMat4(lights.get(i).getViewMatrix());
Mat4 model2 = Mat4.MAT4_IDENTITY;
Mat4 mvp = MathHelpers.biasMVP(proj2, view2, model2);
FloatBuffer matBuffer = org.lwjgl.BufferUtils.createFloatBuffer(16);
MathHelpers.Mat4ToMatrix4f(mvp).store(matBuffer);
matBuffer.flip();
深度着色器: http://pastebin.com/SqFaBnSE http://pastebin.com/t5t35XNM
普通着色器: http://pastebin.com/yQhqyAWc http://pastebin.com/2C2sXByz
【问题讨论】:
-
在您的顶点着色器中,您必须根据模型在阴影贴图创建和模型显示之间的转换来调整阴影坐标:示例 tex[7] 用于阴影贴图转换,modelView 是单个模型转换:ShadowCoord = (gl_TextureMatrix[7] * inverse(modelView) * gl_ModelViewMatrix) * gl_Vertex;
-
所以如果我理解你的评论是正确的,我需要将pastebin.com/2C2sXByz的第41行更改为:
vec4 newShadowCoord = lights[0].matrix * (inverse( viewMatrix * modelMatrix ) ) * pass_Position;?这样做最终会产生随相机移动的阴影,但我已经看到其他有同样问题的问题,所以我会检查这些。感谢您的帮助! -
哦,我是个傻瓜,应该是:vec4 newShadowCoord = lights[0].matrix * (inverse( modelMatrix ) ) * pass_Position;,当然。尽管如此,以不同的方式对我来说仍然失败。感谢您的帮助!
-
我回顾了一下我的代码,我让你感到困惑:modelView 统一保存整个场景的模型矩阵,而我用贬值的 'gl_Matrices' 检索当前 modelMatrix 并从它们之间的区别(对不起,如果我让你感到困惑)
标签: java opengl graphics lwjgl shadow