PDA

View Full Version : shadows to go with deferrred lightning for 2d. Ideas?



jaketehsnake
08-22-2015, 01:37 AM
So, I've been twisting and turning for many nights now, trying to figure out how to implement shadows into my rendering process.

So for now I'm using deferred lightning to visualize a 2d world. It's rendered top down and I render back to front to get the proper result. My rendering looks like this:

Bind my G-buffer FBO, containing two textures. One for diffuse and one for normals.
Bind G-buffer shader.
upload my sprite-VBO
Bind G-buffer as sources.
Bind my light-shader.
Bind the default FB.
upload my light-VBO.

My light-shader takes a quad with a 3d center position. All fragments within the quad compare their distance and direction to the center-position and output light accordingly. In other words I'm not iterating through all lights for each fragment like I've seen traditional deferred lightning do.

Now the problem is to implement shadows, preferably a bit soft. So far I've got two ideas of how to do this, but none is satisfactory.

method 1:
enable stencil test. For each light:
render the shadows that's within the light-quad.
render the light, using the stencil test.
Cons:
for each light I'd have to switch between two shaders, I'd also have to change the stencilFunc. I'd have no soft shadows to speak of.

method 2:
Add another 1-bit texture to the G-buffer. In this each sprite will write a 1 if it's a shadowcaster. Then, in the lightpass, for each fragment I do a loop where I step back towards the center and the 1-bit texture is sampled. If it's a 1, then the fragment is in shadow and no light will be applied. If it reaches the center by only encountering 0:s, the fragment is lit.
Cons: long loop in fragment-shader. Ineffective!

I've looked long and hard on the internet and all I've found is shadow-maps and I can't seem to understand them, since I haven't worked with 3d or depth tests. Besides, I should be able to find a simpler solution since my world is so simplified.
Any ideas?

mogumbo
08-24-2015, 02:54 PM
Shadow volumes were only in favor for a short time when commodity graphics cards didn't have enough RAM to do a good job at shadow maps. Those days are long gone, though, and now shadow maps are everywhere. However, I doubt any of that applies to a 2D game. Computers are so darn fast with so much RAM now that you could do either one easily in 2D.

Just to take a wag at your particular problem: shadow maps for a 3D scene are 2D. So for your 2D game you could use 1D shadow maps. That means for each light quad you render you would bind a different 1D shadow map and light position. That's enough info to calculate whether each pixel is in or out of shadow. A 1D shadow map is small enough you could probably render it on the GPU or CPU, whichever is easiest.

Anyway, that's just a guess from someone who has never done 2D shadows, only 3D. There should be much more detailed ideas out there that apply to your situation. There are a ton of articles out there about doing shadows in 2D games and I think a lot of them are based on shadow maps. If you study harder on shadow maps you'll probably have a lot more good ideas.