GameDev.netRazorblade's Isometric Dynamic Lighting Algorithm

Razorblade's Isometric Dynamic Lighting Algorithm
by Travis "Razorblade" BemannIsometric engines use 2D tiles masquerading as if they were in 3D, minus perspective. A problem with most isometric engines today is that they cannot have dynamic lighting with point light sources. This article does not explain isometric engines, but explains how to implement and use dynamic lighting in isometric engines. In most isometric engines, the light source is directional and is permanently fixed when the tiles are created. At the time of writing, there is only one isometric game that I know of that has point light sources, which is Command & Conquer 2: Tiberian Sun, but it has not come out yet as of writing this. The approach shown here uses specially made tiles in which each pixel has two values. The first value is a value which is the RGB color of the pixel, and the second value is a 8-bit value which designates the direction the pixel faces. The 8-bit value is actually a two 4 bit values, one which is the rotation of the normal of the pixel around the y axis in a right handed system, and the other being the elevation of the normal of the pixel. The rotation is from 0-15 with 0 designating 0 degrees, and 15 designating 360 degrees. The elevation is from 0-15 with 0 designation straight down and 15 straight up. Before each tile is drawn while rendering, a table of lighting values (monochromatic or RGB) is calculated using the locations and distances of each light source. The rotation/elevation combination for each direction acts as an index in this table. Here is pseudocode for calculating this table in with simple monochromatic dynamic lighting as well as ambient lighting: calculate tile lighting table zero out table from i = 0 to i = 15 from j = 0 to j = 15 table[i][j] = 0 calculate point lighting from i = 0 to i = number of light sources distance = sqrt( ( tile.x - light[i].x )^2 + ( tile.y - light[i].y )^2 + ( tile.z - light[i].z )^2 ) if distance^2 < light[i].intensity received_light = light[i].intensity - distance^2 if abs( light[i].x - tile.x ) = 0 if light[i].x - tile.x < 0 rotation_angle = 0 else rotation_angle = pi else rotation_angle = atan( light[i].z - tile.z / light[i].x - tile.x ) if abs( light[i].z -tile.z ) = 0 if light[i].z - tile.z < 0 elevation_angle = 0 else elevation_angle = pi else elevation_angle = atan( light[i].y - tile.y / light[i].z - tile.z ) if elevation_angle > pi elevation_angle = elevation_angle - pi from r = 0 to r = 15 row_light = cos( rotation_angle - ( 2pi / 16 ) * r ) * received_light from e = 0 to e = 15 table[r][e] = table[r][e] + cos( elevation_angle - ( pi / 16 ) * e ) * row_light calculate ambient lighting from r = 0 to r = 15 from e = 0 to e = 15 table[r][e] = table[r][e] + ambient.intensity In the pseudocode, table[i][j] is the table of monochromatic lighting values, with the first index being the rotation of the normal around the y axis and the second index being the elevation of the normal. tile.x, tile.y, and tile.z indicate the location of the isometric tile the table is being generated for in 3D space. light[i].x, light[i].y, and light[i].z indicate the coordinates of each point light. light[i].intensity is the power of each point light. ambient.intensity is the power of the ambient light. The direction value with each pixel is used as an index in the lighting table as if the lighting table had only one index, not two indexes. The lighting value is then extracted from the selected location in the table and is used in draw the selected pixel in the tile, but I will not discuss methods of doing this here because they are very dependent on whether the isometric uses indexed or RGB colors, monochromatic or RGB lighting, and other factors, such as quality vs. speed. All tiles must be assigned a location in 3D space in this engine, even if they are assumed to be on the ground do to the details of this engine. Unfortunately, tiles do not cast shadows with this engine.
© 1999-2011 Gamedev.net. All rights reserved. |