GLSL Shadow Map code sample

Hello guys,

I’ve been tinkering a lot with shadowmap and GLSL lately, here are a few binaries with source code:

Raw shadow mapping

Percentage Closer Filtering shadow mapping

Variance Shadow Mapping

It uses GLUT for portability so it works on Win32, Mac and Linux.
Hope it will help someone ;)!

Cool! :slight_smile:

Oh man, just got my bug fixed with my shadow maps. Looking to implement VSM’s so that tut will be GREAT. thanks a lot!

Great stuff,

I struggled a bit when I first did my shadow maps in OpenGL. someone on here was kind enough to link me to their tutorial on how to do it, and I had to strip his source down to the basics and see where I was making mistakes.

Since then I had always planned on doing a Very Simple Shadows tutorial on GLSL shadow-mapping in GLUT, but it seems you have beaten me to it :slight_smile:

nevermind lol

Thanks for the great tutorial Nicolas! One question though… how can you pull off this same thing without the extra GL_TEXTURE matrix translation? I can’t seem to get it to work with that.

Also, in the code, you’re polling for a xPixelOffset and a yPixelOffset uniform that don’t appear to exist in the shaders.

nicolasbol, thanks for the nice tutorials! Keep up the good work, we appreciate it a lot :slight_smile:

Why is the code with the PCF not working? I see no shadow(s) :stuck_out_tongue:

I found some informations: The PCF Code is only working on nvidia hardware. Any idea why?
I found no extentions or something for that result :stuck_out_tongue:

Check AMD’s docs on enabling PCF shadow comparisons. NVidia may be allowing it for a combination of state that AMD is rejecting.

I found the problem in the fragment shader.
The shadow2DProj function end’s with “.w” - I change this to “.r” and now it work :slight_smile:

Ah! Ok. Pre-GLSL 1.3, you can set how the rgba (xyzw) vec4 return value is set by the shadow texture lookup via GL_DEPTH_TEXTURE_MODE:


  GL_INTENSITY = rrrr
  GL_LUMINANCE = rrr1
  GL_ALPHA = 000r
  GL_RED = r001 

The default is LUMINANCE, so you were probably getting “1” before when you were using .w.

Post-GLSL 1.3, DEPTH_TEXTURE_MODE is deprecated and GLSL behaves as if its always set to LUMINANCE (so you get a rrr1 return, where r is the depth comparison value).

I’m trying to implement the PCF Soft Shadows in my own project. The idea is to adapt the code from the example.

Unfortunately, I’m getting an incorrect lighting, very bright and without shadows. I need some help diagnosing the issue.

I’m using a QGLWidget (from Qt) to render. Before this implementation, I was just doing rendering on the widget; however, I’ve seen that the tutorial uses two FBO objects. I copied that part, but I don’t know if I have to change also my code.

This is the result that I’m getting:

http://minddebuggers.com/fotos/rendering-wrong.jpg

This is a reference of what I get without lighting:

http://minddebuggers.com/fotos/rendering-OK.jpg

Thanks in advance.

I have some trouble with the simple shadow mapping.
I’m using textureProj() with sampler2DShadow.

Maybe i put the wrong TextureMatrix.

Proj-Light --> perspective projection of the light
View-Light --> view matrix of the light

I do the following:

  • for the depth texture:

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
        glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
    
  • during shadow map rendering (with FBO like the tutorial) I use Proj-Light and View-Light*ModelMatrix
    respectively as projection and model-view matrix

  • during the regular rendering pass I use the following TextureMatrix:
    TextureMatrix = Bias * Proj-Light * View-Light * ModelMatrix;

  • vertex shader:


        shadowmapCoord = TextureMatrix * inputVertex:
    
  • fragment shader:

         shadow = textureProj(shadowMap, shadowmapCoord )
    

The result is no shadow at all… :frowning:
Any help is much appreciate!

I think we need to see the entire shader. Many subtle things could be wrong.

OMG you’re right. I accidentally changed some piece of my spaghetti code, and… tahdah… it works! I can see the Moiré pattern!

Thanks anyway.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.