• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BlueSpud

Variance Shadow Mapping Shadow Brightness Issue

13 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

 

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.

0

Share this post


Link to post
Share on other sites

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. 

1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0