It took a long 8 hour stint on Saturday and most of Monday but I finally managed to crack the cascade shadow mapping trick. Phew! It has been a while since I coded some deep 3D or shaders, and last week it was very much a case of cut and pasting my way to victory.
Alas when things start to go wrong, or shadows render in strange and unpredictable ways, you have nowhere to turn when you base your techniques on someone else's idea. I resolved to sit down and learn every single field, call, transform and math step and really understand what shadow mapping. This approach worked, and mid-day Saturday I began to see how the shadow coordinates where calculated to produce the desired results.
Blue Screen Of Despair
It did not help the situation when close to the end of Saturday, my new machine did a massive BSOD and completely erased one of the header files I was working on. All gone. No back-ups that I could find and despair for me. Rewriting it would have taken another half a day, and then in my darkest hour I remembered that I had set-up a network backup system to grab the contents of the drive and copy it each night at 7PM. Amazingly I had a complete version from Friday night and was able to put it back in and continue working.
As of now, what I have you can see below. The clever stuff is invisible of course, but the shadow you see is actually generated from four depth render targets and seamlessly stitched together using orthographic projections and some clever 3D math.
In order to be able to read the depth buffer for this technique, I discovered a cool hack you can make on DX9 hardware to transfer the depth buffer contents over to a regular texture that can be passed to a shader. Using the same trick, it might be possible to do some really nice effects such as depth-sensitive smoke and particle effects to avoid those sharp polygons cutting into the scene. I will leave those for another day, but now I have access to the depth buffer from a single pass with no loss of performance, it's another tool in my Reloaded toolbox for the future.
The battle is far from over as the current prototype does not use PCF (percentage closer filtering), cascade blending (to make the cascade transitions invisible) and I still need to check out the depth ranges I am using to ensure I get the highest quality depth information from each cascade.
Sorry if you where expecting a video, but I am going to wait for some new artwork from Mark before putting something on YouTube as I think it will show off the shadow technique better with brighter more interesting objects. I have just made a massive backup of my files so far (twice) in a bout of healthy paranoia and I am going to switch to a few emails that need my attention. I was hoping to get the shadow stuff cracked today, and my mission was a success. The lesson learned over the last few days is that as good as cut and paste is, there is no substitute for a piece of paper, a pen and a brain that wants to learn how to do the 'working out'. Well done brain!