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.
enable stencil test. For each light:
render the shadows that’s within the light-quad.
render the light, using the stencil test.
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.
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.