I'm a software engineer who works on game development part time. I teach game development (on gamedev.stackexchange.com and lynda.com). I'm always working on something, and I'll post updates here. Let me know if there's a game development topic you want to know more about, I probably know the answer, or at least where to get one.

Thursday, June 9, 2011

A light post

A light post
Lighting is at a pretty good place right now. It hasn't gone through a bunch of optimization, but it’s pretty fast. It works sort of like flow. Say we have a situation like this with a light inside a little tunnel:
The flow starts at the position of the light (of course).

The light flows through cubes that are light transparent and touches the faces of cubes that are not, and sets their color along the way.

The “flow” distance is limited by the falloff of the light. For each face the “flow” touches, I calculate the distance and the intensity, the dot product of the light vector and the normal of the face. This means that if light isn't “pointed toward” the face, it doesn't get lit. This alone works OK, but doesn't take into account solid objects, like walls.

If shadows are enabled, then before we set a face light, we check to make sure it has line-of-sight with the source. This is a bit more processing intensive. However, what’s cool is we only have to do it once! We only have to compute it again if a cube in range of the light is added or removed. Otherwise the light is “baked” into the texture of the cube. Plus I already had solid picking code, and can use it here too.

This is actually really fast, considering. The lighting is done without noticeable delay. Check out the screenshots of it in game on the gallery page. The shadows produced are a bit chunky, since it’s a per face lighting, not even per vertex or per pixel. But it’s fast and it works for me!
Perhaps later I’ll do something nifty like Minecraft and have smooth lighting.
Also, who doesn’t love SketchUp?


  1. So your "flow" method, how does it handle for surfaces nearby the light but not connected to it?

    It looks like your method requires the walls needed to be lit connected to each other in some way, within the distance of the light.

    Eg. imagine a very wide cave, the walls are too far away to be lit by the torch, but the ceiling and floor are close enough to be lit. The light source is placed on the floor is not touching the ceiling. How will the light cross the open space to the ceiling?

  2. The light "flows" through the empty spaces, not through the surfaces. See the text: "The light flows through cubes that are light transparent...". This easily allows for disconnected surfaces. Each face gets its light value from the adjacent empty space. Thanks for your question.