OpenGL阴影贴图在所有对象上完全相同

时间:2014-10-18 03:10:32

标签: java opengl graphics lwjgl shadow

(作业相关问题)

我正在尝试按照this tutorial实现阴影贴图,但是,我遇到了一个奇怪的问题 - 我场景中的所有对象都会应用相同的阴影,如下所示:{ {0}}(灯光在-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

2 个答案:

答案 0 :(得分:0)

在对阴影贴图进行采样时,首先应将片段的世界位置(在应用相机变换和透视矩阵之前)转换为

在顶点着色器中,这将是:

attribute vec3 pos;

uniform mat4 shadowTransform;//the MVP you used to create the shadowmap
uniform mat4 MVP;

varying vec4 shadowPos;//you can use the xy/w to sample the shadow map and compare against z/w

main(){
   gl_position = MVP*vec4(pos,1);
   shadowPos = shadowTransform*vec4(pos, 1);
}

答案 1 :(得分:0)

所以,我最终想通了。这是一个简单的逻辑错误,因为没有使用正确的位置。

public static void setLightUniforms(int program, Matrix4f mat) { Mat4 proj2 = Matrices.perspective(90, 1, 3f, 20f); Mat4 view2 = = MathHelpers.Matrix4fToMat4(lights.get(i).getViewMatrix()); Mat4 model2 = Mat4.MAT4_IDENTITY;

我总是使用身份矩阵作为位置,而不是我计算的模型的位置。

现在代码(注意参数的用法):

public static void setLightUniforms(int program, Matrix4f mat) { Matrix4f proj = MathHelpers.Mat4ToMatrix4f(Matrices.perspective(90, 1, 3f, 20f)); Matrix4f view = lights.get(i).getViewMatrix(); Matrix4f bias = MathHelpers.Mat4ToMatrix4f(MathHelpers.biasMatrix); Matrix4f MVP = Matrix4f.mul(proj, Matrix4f.mul(view, mat, null), null); Matrix4f.mul(bias, MVP, MVP);