TheKaiser

01-11-2014, 04:53 AM

I'm trying to implement shadowmapping for spotlights in an OpenGL 4.3 deferred renderer. The problem is described below.

Here is the scene from the cameras POV, with shadow calculations commented-out:

http://i.stack.imgur.com/LnxFB.png

Here is the depth texture generated during the shadow pass.

http://i.stack.imgur.com/DIb2g.png

The two images describe the problem: in the first image, you can see small spots of light along the edges of the ground plane, and second image shows this up-close.

http://i.stack.imgur.com/wYJUS.png

http://i.stack.imgur.com/9bvX7.png

Here is the fragment shader for the spotlight lighting pass:

#version 430

layout(std140) uniform;

uniform UnifDirLight

{

mat4 mWVPMatrix; // camera world-view-projection matrix

mat4 mVPMatrix; // the spotlights view-projection matrix

vec4 mLightColor;

vec4 mLightDir;

vec4 mGamma;

vec2 mScreenSize;

} UnifDirLightPass;

layout (binding = 2) uniform sampler2D unifPositionTexture;

layout (binding = 3) uniform sampler2D unifNormalTexture;

layout (binding = 4) uniform sampler2D unifDiffuseTexture;

layout (binding = 5) uniform sampler2D unifShadowTexture;

out vec4 fragColor;

void main()

{

vec2 texcoord = gl_FragCoord.xy / UnifDirLightPass.mScreenSize;

vec3 worldPos = texture(unifPositionTexture, texcoord).xyz;

vec3 normal = normalize(texture(unifNormalTexture, texcoord).xyz);

vec3 diffuse = texture(unifDiffuseTexture, texcoord).xyz;

vec4 lightClipPos = UnifDirLightPass.mVPMatrix * vec4(worldPos, 1.0);

vec3 projCoords = lightClipPos.xyz / lightClipPos.w;

projCoords.x = 0.5 * projCoords.x + 0.5;

projCoords.y = 0.5 * projCoords.y + 0.5;

projCoords.z = 0.5 * projCoords.z + 0.5;

float depthValue = texture(unifShadowTexture, projCoords.xy).x;

float shadowFactor = 0.0;

if (depthValue >= (projCoords.z + 0.0001))

shadowFactor = 1.0;

float angleNormal = clamp(dot(normal, UnifDirLightPass.mLightDir.xyz), 0, 1);

fragColor = vec4(diffuse, 1.0) * shadowFactor * angleNormal *

UnifDirLightPass.mLightColor;

}

I don't see where the problem is, or what might be causing the strange white spots on the edges of the ground plane. Any pointers?

Here is the scene from the cameras POV, with shadow calculations commented-out:

http://i.stack.imgur.com/LnxFB.png

Here is the depth texture generated during the shadow pass.

http://i.stack.imgur.com/DIb2g.png

The two images describe the problem: in the first image, you can see small spots of light along the edges of the ground plane, and second image shows this up-close.

http://i.stack.imgur.com/wYJUS.png

http://i.stack.imgur.com/9bvX7.png

Here is the fragment shader for the spotlight lighting pass:

#version 430

layout(std140) uniform;

uniform UnifDirLight

{

mat4 mWVPMatrix; // camera world-view-projection matrix

mat4 mVPMatrix; // the spotlights view-projection matrix

vec4 mLightColor;

vec4 mLightDir;

vec4 mGamma;

vec2 mScreenSize;

} UnifDirLightPass;

layout (binding = 2) uniform sampler2D unifPositionTexture;

layout (binding = 3) uniform sampler2D unifNormalTexture;

layout (binding = 4) uniform sampler2D unifDiffuseTexture;

layout (binding = 5) uniform sampler2D unifShadowTexture;

out vec4 fragColor;

void main()

{

vec2 texcoord = gl_FragCoord.xy / UnifDirLightPass.mScreenSize;

vec3 worldPos = texture(unifPositionTexture, texcoord).xyz;

vec3 normal = normalize(texture(unifNormalTexture, texcoord).xyz);

vec3 diffuse = texture(unifDiffuseTexture, texcoord).xyz;

vec4 lightClipPos = UnifDirLightPass.mVPMatrix * vec4(worldPos, 1.0);

vec3 projCoords = lightClipPos.xyz / lightClipPos.w;

projCoords.x = 0.5 * projCoords.x + 0.5;

projCoords.y = 0.5 * projCoords.y + 0.5;

projCoords.z = 0.5 * projCoords.z + 0.5;

float depthValue = texture(unifShadowTexture, projCoords.xy).x;

float shadowFactor = 0.0;

if (depthValue >= (projCoords.z + 0.0001))

shadowFactor = 1.0;

float angleNormal = clamp(dot(normal, UnifDirLightPass.mLightDir.xyz), 0, 1);

fragColor = vec4(diffuse, 1.0) * shadowFactor * angleNormal *

UnifDirLightPass.mLightColor;

}

I don't see where the problem is, or what might be causing the strange white spots on the edges of the ground plane. Any pointers?