Я реализую направленное отображение теней в отложенном затенении.
Сначала я рендерил карту глубины из светлого вида (ортогональная проекция).
Результат:![result](https://i.imgur.com/Tdmai8R.png)
Я собираюсь сделать 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, результат будет:
![result](https://i.imgur.com/0lzV6L1.jpg)
По сути, именно так выглядит 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);
}