我目前正在实现延迟渲染管线,并卡在了阴影映射上。
我已经成功地将其实现到正向渲染管线中。
我的步骤如下:
- 获取在光照视图中的位置
- 转换为光照视图剪裁空间
- 使用* 0.5 + 0.5获取阴影纹理坐标
- 检查深度
编辑:使用新的结果图片更新了代码:
float checkShadow(vec3 position) {
// get position in light view
mat4 invView = inverse(cameraView);
vec4 pEyeDir = sunBias * sunProjection * sunView * invView * vec4(position, 1.0);
// light view clip space
pEyeDir = pEyeDir / pEyeDir.w;
// get uv coordinates
vec2 sTexCoords = pEyeDir.xy * 0.5 + 0.5;
float bias = 0.0001;
float depth = texture(sunDepthTex, sTexCoords).r - bias;
float shadow = 1.0f;
if(pEyeDir.z * 0.5 + 0.5 > depth)
{
shadow = 0.3f;
}
return shadow;
以下是代码中重要的一些变量:
vec3 position = texture(positionTex, uv).rgb;
在某些相机位置上,当我将阴影值乘以最终颜色时,会出现深色背景(网格保持不变)。如下图所示:
![enter image description here](https://istack.dev59.com/4v5VW.webp)
![position](https://istack.dev59.com/f68YL.webp)
![shadow depth](https://istack.dev59.com/KW0Id.webp)
translate(0.5,0.5) * scale(0.5,0.5) * lightProj * lightView * vertexPos - (modelTransformations * vertexPos)
进行计算。 - j-pvec4 ModWorldPos = inverse(gl_ModelViewMatrix) * gl_Vertex - inverse(modelView) * gl_Vertex; ShadowCoord = gl_TextureMatrix[7] * (gl_Vertex - ModWorldPos);
这里是我的代码,纹理矩阵为translate(0.5,0.5) * scale(0.5,0.5) * lightProj * lightView
。 - j-p