# Shadow casting in 2D OpenGL

I´ve read several articles, tutorials about shadow casting and I think that I understand the way how it works. However I miss some of the steps. Here is included my order list:

1. Fill framebuffer with ambient light

2. Render light textures to the framebuffer(adding proper blending)

3. Lights have vector of shadow casters(convex polygons) and check if they touch them(by checking the distance) and if yes, I do proper math calculation to project the polygon to the right "transformation". Then I draw them as black polygons to the framebuffer

4.At the end I use this framebuffers texture and blend it with actual game textures to "do lighting (with shadows)".

As you can see I bolded the step 3 because thats where I´m lost. The first thing, how do I project the polygon? (for these polygons I have a vector of points, also a vector of edges). The second thing, is it efficient to do it this way? I mean to iterate through the possible shadow casters for each light.

If you wanna render lights the algorithm it's more difficult, basically you take the light center and the polygons' vertices to draw the light triangles, but you have to calculate which is the nearest wall (the one to be enlightened). That's has a complexity of O(nk²) but I think you can optimize it to something like O(nlog(k)). This way reduces the calculations done by the GPU (you only render the light pixels once and you don't even render the shadowed areas, before you render both and the shadows even multiple times), remember that the casters in a scene would be at most an hundred or similar while the pixel of a monitor are millions, so k² (the worst case) in this scenario is better than k if you will draw a lot less pixels.

For any question ask in comments. I've not been so specific because there are already a lot of tutorials online. (I can link them if you need).

• What do you mean by drawing the light, not the shadow? According to this tutorial (fisherevans.com/blog/post/2d-lighting-and-shadows) I thought that I´m rendering the shadows as well. Btw nice video!
– Pins
Commented Jul 23, 2017 at 8:18
• @Pins like this: redblobgames.com/articles/visibility Commented Jul 23, 2017 at 8:23
• So basically, when we talk about OpenGL, i pass a vector of shadow casters to a light, it checks its vertices if they are front face or not, then lengthen them and render the light texture as triangle fan using all vertices? (I have to also calculate the UV for the vertices used for triangle fan and also care about the order)
– Pins
Commented Jul 23, 2017 at 21:37
• And also at the start I have 4 vertices, and check all vertices I will have with a center of the light.
– Pins
Commented Jul 23, 2017 at 21:47
• The code it's quite a mess, basically I create a square mesh for each light and remove like with a "swiping ray" every object inside it, projecting its shadow to the border of the square. If I remember correctly It's optimized to work only with AABB casters, but I think you can easily change that (removing some assumption from the code about the aligment of the segments). It DOES NOT WORK with overlapped geometry (you can still overlap the boredrs tough). Hope I've said everything about the code, I dont remember it much :) Ask for everything. Commented Jan 14, 2018 at 16:14