Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Variance Shadow Mapping Shadow Brightness Issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 BlueSpud   Members   -  Reputation: 426

Like
0Likes
Like

Posted 24 May 2014 - 06:51 PM

Hello,

After attempting to implement variance shadow mapping I've run into a problem and absolutely no idea what is causing it. Here is a picture of what is going on:

uCubW.png

 

The shadow seems to get lighter the closer it is to the occluder. Its very strange and the implementations / tutorials that I've looked at don't seem to have the same problem and haven't touched on how to fix it. Of course this image isn't enough, so here is the relevant code:

 

How I create the frame buffer for the shadow map:

frameBuffer bufferToReturn;
        bufferToReturn.x = x;
        bufferToReturn.y = y;

        glGenFramebuffers(1, &bufferToReturn.bufferID);
        glBindFramebuffer(GL_FRAMEBUFFER, bufferToReturn.bufferID);

        glGenTextures(1, &bufferToReturn.texture);
        glBindTexture(GL_TEXTURE_2D, bufferToReturn.texture);
        if (shadow == false)
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA_FLOAT32_APPLE, x, y, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 0);
        else glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, x, y, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

        glBindTexture(GL_TEXTURE_2D, 0);


        glGenTextures(1, &bufferToReturn.depth);
        glBindTexture(GL_TEXTURE_2D, bufferToReturn.depth);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, x, y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
        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_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        if (shadow == true)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);

        glBindTexture(GL_TEXTURE_2D, 0);

        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, bufferToReturn.texture, 0);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, bufferToReturn.depth, 0);
        
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        return bufferToReturn;

And here is the shader code for rendering the depth. I've only rendered out the depth to attempt to be memory conservative and just compute the moments in the lighting shader.

varying vec4 position;

void main()
{

    //get the depth
    float depth = position.z/position.w;

    gl_FragColor = vec4(depth, 0.0, 0.0, 1.0);
}

Lastly the lighting code, at least the parts of it that deal with shadow.

uniform sampler2D positionMap;
uniform sampler2D normalMap;
uniform sampler2D albedoMap;
uniform sampler2D shadowMap;

varying vec2 texcoord;
uniform mat4 shadowMatrix;

float chebyshevUpperBound(vec3 coord)
{
    float depth = texture2D(shadowMap,coord.xy).r;
    vec2 moments = vec2(depth,depth*depth);

    //if we are closer than the depth map, we are lit
    if (coord.z <= moments.x)
        return 1.0;

    float variance = moments.y - (moments.x*moments.x);
    variance = max(variance,0.0005);

    float momentdistance = coord.z - moments.x;
    float p_max = variance / (variance + momentdistance*momentdistance);
	
    return p_max;
}

void main()
{
    //get all the G-buffer information
    vec3 normal = normalize(texture2D(normalMap,texcoord)).rgb;
    vec4 color = (texture2D(albedoMap,texcoord));
    vec3 position = (texture2D(positionMap,texcoord)).rgb;
    vec4 lightPosition = shadowMatrix*vec4(position,1.0);

    lightPosition /= lightPosition.w;
    float shadow = chebyshevUpperBound(lightPosition.xyz);

    gl_FragColor = vec4(((diffuselight+specular)*shadow)+ambient,color.w);

  
}

Any input is greatly appreciated and hopefully may be able to help anybody else in the future with the same problem.



Sponsor:

#2 Angus Hollands   Members   -  Reputation: 717

Like
1Likes
Like

Posted 24 May 2014 - 07:49 PM

Whilst I have little immediate experience on the subject, we had VSM shadows implemented recently, and a similar effect was observed.
The developer who implemented speculated (in passing) that the intensity of the light source was influencing the shadow

http://blenderartists.org/forum/showthread.php?268228-quot-Variance-quot-Shadow-whats-the-point&p=2217175&viewfull=1#post2217175



#3 BlueSpud   Members   -  Reputation: 426

Like
0Likes
Like

Posted 24 May 2014 - 08:01 PM

Whilst I have little immediate experience on the subject, we had VSM shadows implemented recently, and a similar effect was observed.
The developer who implemented speculated (in passing) that the intensity of the light source was influencing the shadow

http://blenderartists.org/forum/showthread.php?268228-quot-Variance-quot-Shadow-whats-the-point&p=2217175&viewfull=1#post2217175

Thanks for the link. While it was useful, I've modified the shader so the diffuse is not used and the same thing occurs, so that can't be it.



#4 RnaodmBiT   Members   -  Reputation: 667

Like
4Likes
Like

Posted 24 May 2014 - 09:36 PM

The problem is the way variance shadow mapping determines shadow/light per pixel. It takes the difference between the (linear depth)^2 and the depth^2, and the greater the distance, the greater the shadowing term, but when they get close together (which is what happens when the shadow approaches the occluder) the shadowing term gets lower. 

 

I also noticed you are getting the shadow map depth data incorrectly. You need to store both linear and squared depth in the depth map in separate channels. Then you can sample them both instead of 'vec2(depth,depth*depth)'. This means that the depth and depth2 values will be linearly sampled and that depth * depth != depth2 for most cases. This is a key point for variance shadow mapping because it allows you to apply a blur to the shadow map and get nice smooth shadows as a result instead of the square edges you have shown. 

 

As it stands in your code, if we simplify it you get 

float variance = moments.y - (moments.x * moments.x);

which is the same as

float variance = moments.y - moments.y; // ( = 0) this is why you sample both moments.x and moments.y from a depthmap
variance = max(variance, 0.0005); // ( = 0.0005)

and finally

float p_max = variance / (variance + momentdistance*momentdistance);


#5 BlueSpud   Members   -  Reputation: 426

Like
0Likes
Like

Posted 24 May 2014 - 10:00 PM

 

The problem is the way variance shadow mapping determines shadow/light per pixel. It takes the difference between the (linear depth)^2 and the depth^2, and the greater the distance, the greater the shadowing term, but when they get close together (which is what happens when the shadow approaches the occluder) the shadowing term gets lower. 

 

I also noticed you are getting the shadow map depth data incorrectly. You need to store both linear and squared depth in the depth map in separate channels. Then you can sample them both instead of 'vec2(depth,depth*depth)'. This means that the depth and depth2 values will be linearly sampled and that depth * depth != depth2 for most cases. This is a key point for variance shadow mapping because it allows you to apply a blur to the shadow map and get nice smooth shadows as a result instead of the square edges you have shown. 

 

As it stands in your code, if we simplify it you get 

float variance = moments.y - (moments.x * moments.x);

which is the same as

float variance = moments.y - moments.y; // ( = 0) this is why you sample both moments.x and moments.y from a depthmap
variance = max(variance, 0.0005); // ( = 0.0005)

and finally

float p_max = variance / (variance + momentdistance*momentdistance);

Alright, So I've changed the code and something happened, but the problem I am trying to solve is the shadowing term issue. It doesn't seem to happen on the demos that I've seen like the original paper's implementation. So the question is, how am I supported to compensate for that so all the shadows are nice and uniform.



#6 Irlan   Members   -  Reputation: 1584

Like
0Likes
Like

Posted 26 May 2014 - 01:55 PM

Maybe this can be useful: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/



#7 BlueSpud   Members   -  Reputation: 426

Like
0Likes
Like

Posted 26 May 2014 - 02:02 PM

Maybe this can be useful: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/


That doesn't have anything on variance shadow mapping :/

#8 Irlan   Members   -  Reputation: 1584

Like
0Likes
Like

Posted 26 May 2014 - 02:05 PM

 

Maybe this can be useful: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/


That doesn't have anything on variance shadow mapping :/

 

I know, I've just posted in case someone don't even know what Shadow Mapping is and is using OpenGL.
 happy.png



#9 RnaodmBiT   Members   -  Reputation: 667

Like
1Likes
Like

Posted 26 May 2014 - 06:04 PM

Okay, for the getting lighter problem, your issue is these lines:

float momentdistance = coord.z - moments.x;
float p_max = variance / (variance + momentdistance*momentdistance);

You can see that as momentdistance approaches 0, p_max will approach 1 / 1, giving you fully light pixels. One simple solution for this would be to scale momentdistance up by some set value, but, to put it simply, there will ALWAYS be this 'getting lighter' problem when using variance shadow mapping, thats part of how it determines shadowing in the first place. In most cases this wont be an issue as the object being shadowed will be thick enough that you wont notice the light section since it will be hidden by the object it self, ie. a crate. 



#10 phil_t   Crossbones+   -  Reputation: 3946

Like
0Likes
Like

Posted 26 May 2014 - 06:35 PM


there will ALWAYS be this 'getting lighter' problem when using variance shadow mapping, thats part of how it determines shadowing in the first place. In most cases this wont be an issue as the object being shadowed will be thick enough that you wont notice the light section since it will be hidden by the object it self, ie. a crate. 

 

Yeah, I think that's just the nature of the algorithm. I used to use it, but I had many thin shadow-casters, and the artifacts ended up being too noticeable to justify its use. It looks good in demos/screenshots, because the objects and such are carefully chosen to hide the artifacts :P.


Edited by phil_t, 26 May 2014 - 06:36 PM.


#11 BlueSpud   Members   -  Reputation: 426

Like
0Likes
Like

Posted 27 May 2014 - 04:55 PM

Okay, for the getting lighter problem, your issue is these lines:

float momentdistance = coord.z - moments.x;
float p_max = variance / (variance + momentdistance*momentdistance);

You can see that as momentdistance approaches 0, p_max will approach 1 / 1, giving you fully light pixels. One simple solution for this would be to scale momentdistance up by some set value, but, to put it simply, there will ALWAYS be this 'getting lighter' problem when using variance shadow mapping, thats part of how it determines shadowing in the first place. In most cases this wont be an issue as the object being shadowed will be thick enough that you wont notice the light section since it will be hidden by the object it self, ie. a crate. 

That worked well, but now the parts of the shadow that would have been lighter are dark. Should I just increase the radius of the blur?



#12 RnaodmBiT   Members   -  Reputation: 667

Like
0Likes
Like

Posted 27 May 2014 - 05:02 PM

Increasing the blur might help you, but really, it'd down to you, the old advice 'if it looks right, it is right' certainly applies here.

 

It might be that in the end, you decide variance shadow mapping might not be the best option if you have lots of thin geometry just as phil_t did. It's really down to you, I've just tried to explain how the algorithm works, and why you are seeing what you are so that you can figure out how you want to go about dealing with it.

 

Good Luck



#13 BlueSpud   Members   -  Reputation: 426

Like
0Likes
Like

Posted 28 May 2014 - 12:48 PM

I've decided to abandon variance shadow mapping because they plainly just don't work right. By multiplying the distance, it is possible to get a softer shadow, but it comes at a cost. The contrast of the blur will no longer work. The higher you multiply it, the lower the contrast. Its a cool idea, and blurring shadowing does work, but the fact that it does this kind of makes it unusable. To anyone who else has this problem and wants to solve it, I would recommend not using variance shadow mapping, as fixing this issue to the fullest extent will pretty much nullify the benefits of using them. Thanks to everyone who helped.



#14 kalle_h   Members   -  Reputation: 1474

Like
0Likes
Like

Posted 28 May 2014 - 02:34 PM

Variance shadow mapping has it uses like for particles or volumetrics. But for general scenes there are usually too strong artifacts.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS