The One Man MMO Project
The story of a lone developer's quest to build an online world :: MMO programming, design, and industry commentary
Ignorance Can be Bliss
By Robert Basler on 2012-08-17 23:50:49
Homepage: www.onemanmmo.com email:one at onemanmmo dot com

One of the things I've always liked about computer programming is that if something works, you really don't need to know how it works. Libraries work like this, quite often code samples do as well. I've been working on getting soft shadows to work, and when I started, armed with a great code example of soft shadows using Variance Shadow Mapping I was totally optimistic I wouldn't need to really understand how shadow mapping works.

Well that's out the window. I understand.

The best description I found of how shadow mapping works came from Wikipedia. I also needed this tutorial to understand how to do shadows from the sun (for spotlights you render the shadow map with a perspective projection, the sun requires an orthographic projection.) The tutorial was also helpful in pointing out that you want your shadow map frustum to be as tight on your visible scene as you can arrange (otherwise you're wasting shadow map resolution on stuff you won't see.) When I was checking things, I went back to the original VSM paper and example code from GDC 2006.

A lot of games today use cascaded shadow maps. CSM's increase the shadow map resolution near the camera so you don't get blocky shadows but are also more complicated to render. I don't need more resolution near the camera however because my scene is mostly a uniform distance from the camera.

I did find a couple bugs in the VSM example. When a depth buffer is initialized in OpenGL it initializes it to 1.0. In the VSM example it writes depth values to a texture, but clears the texture to 0,0,0. This doesn't cause a problem in the example because it renders the entire scene when doing the shadow map generation pass. My first attempt, in order to improve performance, I tried only rendering shadowcasters to the shadow map. With the example's value in the shadow map I would get shadows in areas where there shouldn't be ones. Changing the value to 1.0, I found that the blur step would cause me to lose the edges of the shadows because it would blur towards infinite distance and very quickly pass into the range where pixels were fully lit. Now I'm rendering the whole scene into the shadow buffer so that the blurring doesn't blur to infinity.

Also, the gaussian blur has a -3 where there should be just 3. It's hard to blame him for the blur shader bug though, it is all over the internet.

`	color += texture2D( textureSource, gl_TexCoord[0].st + vec2(  3.0 * scale.x,  3.0 * scale.y ) )   * 0.015625;`

I can't say enough nice things about GDebugger. Running my app (and the example) under GDebugger I could easily view the generated textures and FBO's to figure out where things were going wrong. Trying to debug shadows by rendering the various textures to the screen would have been really painful.

Here's a screenshot of my test scene, a floating cube and a column on a plane.

I still have a bit of work to do. I have to get the shadows rendering in the full game. But that's next week.

 By Robert Basler on 2012-08-27 23:08:45 Homepage: www.onemanmmo.com email:one at onemanmmo dot com (You need to turn on JavaScript.) Additional enhancements at Ignorance Can be Bliss - Part II