Wednesday, 7 September 2011

Multiple Light Sources

So I'm wondering just how one handles multiple light sources? The alpha-blending of the various light-intensity values for a particular map cell is easily understandable, but how does one store (potentially near-infinite) multiple-overlapping light sources and their separate light intensities?


eeeickythump said...

The easiest way is probably with so-called "dijkstra maps".

You can associate a dmap with every light source, the root (origin) of each map being the location of that light source. Dijkstra maps tell you the distance to the origin at every spot on the map -- you just need to turn that distance into light attenuation.

The total amount of light at (x,y) is the sum of dmap(x, y) for all the extant light source dmaps.

Of course you will have to recalc a light's dmap if it moves (or you could store the dmap at the tile).

I believe libtcod contains a dijkstra map implementation.

Pender said...
This comment has been removed by the author.
Pender said...

Well, the way Brogue does it is simply to recalculate all of the lights from scratch every turn, whether or not they move. Here is how I summarized my lighting engine on RGRD:

Assuming your lights are full color, create three separate arrays of
integers to store light data. At the start of each turn, zero them
out. Then cycle through every light on the level. Calculate its field
of view as previously discussed, then for each cell in its field of
view, add the R, G and B components of the color of its light (after
applying a distance drop-off) to the light data arrays. Light
components greater than 100 or 255 or whatever are okay.
Then when you display a cell, to figure out the color to use, treat
the RGB components of the cell's pigment color and of the cell's light
color as fractions (with a pure white color being 1.0, 1.0, 1.0), and
use pairwise multiplication to combine them. Any components with a
value greater than 1.0 should be reduced to 1.0. Display the resulting
color. Note that any cell that has no light content will display as
black. You can either add an ambient light to the entire dungeon
(instead of zeroing out the light grids at the start of the turn, set
them to the corresponding component of your desired ambient light
color) or you can add a "miner's light" that follows the player around
so that the area in his field of view is always illuminated.

Dave said...

Cheers guys. I'm also interested in figuring out how one can relate 1 or more light sources to a particular map cell.