我在延迟着色中实现了方向阴影贴图。
首先,我从灯光视图(正交投影)渲染深度图。
我打算做VSM,所以上面的缓冲区是R32G32存储深度和深度*深度。
然后,对于阴影的全屏阴影传递(在光照传递之后),我编写以下像素着色器:
#version 330
in vec2 texCoord; // screen coordinate
out vec3 fragColor; // output color on the screen
uniform mat4 lightViewProjMat; // lightView * lightProjection (ortho)
uniform sampler2D sceneTexture; // lit scene with one directional light
uniform sampler2D shadowMapTexture;
uniform sampler2D scenePosTexture; // store fragment's 3D position
void main() {
vec3 fragPos = texture(scenePosTexture, texCoord).xyz; // get 3D position of pixel
vec4 fragPosLightSpace = lightViewProjMat * vec4(fragPos, 1.0); // project it to light-space view: lightView * lightProjection
// projective texture mapping
vec3 coord = fragPosLightSpace.xyz / fragPosLightSpace.w;
coord = coord * 0.5 + 0.5;
float lightViewDepth; // depth value in the depth buffer - the maximum depth that light can see
float currentDepth; // depth of screen pixel, maybe not visible to the light, that's how shadow mapping works
vec2 moments; // depth and depth * depth for later variance shadow mapping
moments = texture(shadowMapTexture, coord.xy).xy;
lightViewDepth = moments.x;
currentDepth = fragPosLightSpace.z;
float lit_factor = 0;
if (currentDepth <= lightViewDepth)
lit_factor = 1; // pixel is visible to the light
else
lit_factor = 0; // the light doesn't see this pixel
// I don't do VSM yet, just want to see black or full-color pixels
fragColor = texture(sceneTexture, texCoord).rgb * lit_factor;
}
渲染结果是黑屏,但如果我将lit_factor硬编码为1,则结果为:
基本上就是sceneTexture的样子。
所以我认为我的深度值是错误的,这是不可能的,或者我的投影(上面着色器/投影纹理映射中的光空间投影)是错误的。你能帮我验证一下吗?
我的阴影贴图生成代码是:
// vertex shader
#version 330 compatibility
uniform mat4 lightViewMat; // lightView
uniform mat4 lightViewProjMat; // lightView * lightProj
in vec3 in_vertex;
out float depth;
void main() {
vec4 vert = vec4(in_vertex, 1.0);
depth = (lightViewMat * vert).z / (500 * 0.2); // 500 is far value, this line tunes the depth precision
gl_Position = lightViewProjMat * vert;
}
// pixel shader
#version 330
in float depth;
out vec2 out_depth;
void main() {
out_depth = vec2(depth, depth * depth);
}
答案 0 :(得分:1)
在变量gl_FragCoord
中构建的片段着色器的z
组件包含范围[0.0,1.0]中的深度值。这是您应该存储到深度图的值:
out_depth = vec2(gl_FragCoord.z, depth * depth);
计算后
vec3 fragPos = texture(scenePosTexture, texCoord).xyz; // get 3D position of pixel
vec4 fragPosLightSpace = lightViewProjMat * vec4(fragPos, 1.0); // project it to light-space view: lightView * lightProjection
vec3 ndc_coord = fragPosLightSpace.xyz / fragPosLightSpace.w;
变量ndc_coord
包含一个规范化的设备坐标,其中所有组件都在[-1.0,1.0]范围内。
归一化设备坐标的z
分量可以被传送到深度值(如果depth range是[0.0,1.0]),
float currentDepth = ndc_coord.z * 0.5 + 0.5;
此值可以与深度图中的值进行比较,因为currentDepth
和lightViewDepth
由相同的视图矩阵和投影矩阵计算:
moments = texture(shadowMapTexture, coord.xy).xy;
lightViewDepth = moments.x;
if (currentDepth <= lightViewDepth)
lit_factor = 1; // pixel is visible to the light
else
lit_factor = 0; // the light doesn't see this pixel
答案 1 :(得分:0)
这是您在阴影贴图中存储的深度:
depth = (lightViewMat * vert).z / (500 * 0.2);
这是您将回读值与:
进行比较的深度vec4 fragPosLightSpace = lightViewProjMat * vec4(fragPos, 1.0);
currentDepth = fragPosLightSpace.z;
如果fragPos
位于世界空间中,那么我假设为lightViewMat * vert == fragPos
。您正在通过除以500 * 0.2
来压缩深度,但这不等于fragPosLightSpace.z
。
提示:在一个通道中写出currentDepth的值,在另一个通道中写出阴影贴图的值,然后可以直观地或在RenderDoc或类似的位置比较它们。