GLSL shadow mapping problem

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Brainsaw
Posts: 1183
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

GLSL shadow mapping problem

Post by Brainsaw »

Hi,

I'm trying to implement a shadow mapping shader (based on XEffects) but I don't get it running (XEffects works fine of course, but I also want something less hardware hungry ;) ).

I want to only run 2 render passes (one for shadow map rendering, one for rendering the scene with shadows). I think I have broken the problem down to the 2nd pass pixel shader:

Code: Select all

 
uniform sampler2D textureSampler;
uniform sampler2D ShadowMapSampler;
uniform vec4 LightColour;
varying float lightVal;
 
float testShadow(vec2 smTexCoord, vec2 offset, float realDistance)
{
    vec4 texDepth = texture2D(ShadowMapSampler, vec2( smTexCoord + offset));
    float extractedDistance = texDepth.r;
    
    return (extractedDistance <= realDistance) ? (1.0  / 8.0) : 0.0; 
}
 
vec2 offsetArray[8];
 
void main() 
{
  vec4 TPos = gl_TexCoord[0];
    vec4 SMPos = gl_TexCoord[1];
    vec4 MVar = gl_TexCoord[2];
 
    offsetArray[0] = vec2(0.0, 0.0);
    offsetArray[1] = vec2(0.0, 1.0);
    offsetArray[2] = vec2(1.0, 1.0);
    offsetArray[3] = vec2(-1.0, -1.0);
    offsetArray[4] = vec2(-2.0, -1.0);
    offsetArray[5] = vec2( 2.0, -1.0);
    offsetArray[6] = vec2(-2.0,  1.0);
    offsetArray[7] = vec2(-2.0,  1.0);
 
  SMPos.xy  = SMPos.xy / SMPos.w / 2.0 + vec2(0.5, 0.5);
 
  vec4 finalCol = vec4(0.0, 0.0, 0.0, 0.0);
 
    // If this point is within the light's frustum.
    vec2 clampedSMPos = clamp(SMPos.xy, vec2(0.0, 0.0), vec2(1.0, 1.0));
    if(clampedSMPos.x == SMPos.x && clampedSMPos.y == SMPos.y && SMPos.z > 0.0 && SMPos.z < MVar.z)
    {
        float lightFactor = 1.0;
        float realDist = MVar.x / MVar.z - 0.002;
    
      for(int i = 0;i < 8; i++)
            lightFactor -= 2.5 * testShadow(SMPos.xy, offsetArray[i] * MVar.w, realDist);
 
  if (lightFactor < 0.05) lightFactor = 0.05;
        // Multiply with diffuse.
     if (MVar.y > 0.25)
            finalCol = lightFactor * MVar.y;
     else
       finalCol = MVar.y;
    }
  else finalCol = 0.25;
 
    gl_FragColor = texture2D(textureSampler, TPos.xy) * finalCol;
}
 
Somehow it seems that I can't access the second texture. If I set the shadow map texture as first texture I get this:
Image

On the other hand with the "normal" first texture and the shadow map as second texture I get this:
Image

I have put together a package with the project ready for download at http://dustbin-online.de/download/shader.zip

Does anyone have an idea what I have to change to get this running? Thanks
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: GLSL shadow mapping problem

Post by Mel »

As for what i see, the problem lies in the shader callback you use to render: you're not setting the texture samplers uniforms "uniform sampler2D textureSampler" and "uniform sampler2D ShadowMapSampler" GL won't set the texture sampler identifiers for you, unlike DX, which are set sequentially, after the first one. You've also to set them. They are integers. Somewhere in your callback you have to set those uniforms to the sampler slot you want them to use, 0 for the texture 0, 1 for the texture 1, and so on.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Brainsaw
Posts: 1183
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Re: GLSL shadow mapping problem

Post by Brainsaw »

Thanks a lot, now it works :D

Image
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: GLSL shadow mapping problem

Post by Mel »

Image
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply