Keith Judge's blog on Stencil Buffer Optimizations for Deferred Lights
For point lights, I use a sphere and for spot lights I use a cone. I wanted to use an approximated mesh for rendering the light.
The problem of using an approximated mesh without any adjustments is that the edges get cut off.
To illustrate the issue, here's what happens:
- 's' = size of the cone
- 'b' = angle of the cone
- 'r' = base radius of cone = s * cos(b)
- 'h' = height of cone = s * sin(b)
We can adjust the mesh slightly to encapsulate the spot light. We need to figure out what modifications should be made. One option is to increase the radius of the light, and increase the angle of the cone mesh by some amount. If we use a constant amount, it would be a large value to cover for all the cases. So we need to calculate a new angle and radius for each mesh, and use that for the cone mesh. Here is the math to figure out those values. Let's assume that we use a mesh that has 'n' sides on the cone. Our aim is to get the base of the cone mesh to encapsulate the base of the spotlight circle. For that we should do the following:
From the diagram, I've marked the following:
- r = original base radius of cone
- nr = new radius which is the line between the center to the vertex of the polygon
- a = angle between r and nr
We can use the following formula to get nr:
nr = r / cos(a)
Next, we calculate the new angle and new radius of the cone mesh.
- b = original angle of cone
- h = height of cone = s * cos(b)
- c = new angle of cone = atan(nr / h)
- t = new size of cone = nr / cos(c)
No comments:
Post a Comment