Projecting Textures On The Environment Question submitted by (09 July 1999) Return to The Archives
 I am trying to code a 3D engine in my spare time, and i got around to building the lightmapping code for it. The standard lightmapping techniques are too "standard", so i came up with something else. Each lightsource is to have a volume, and 6 textures to represent light intensity destribution over it's bounding box(or 8mm slides, for that matter). The scene is then classified(ie clipped to 6 volumes) according to which b-box face goes on what poly. The next step is to project the appropriate texture onto the envirnoment. But, the question is, how do you project textures onto the envirnoment efficiently? There has to be some sort of trick to this, since it's very useful.

it sounds like you want to project, onto the environment, the six faces of a box that surrounds a point lightsource. if this is the case, then try the following:

simple solution
let P be a point on a box face in world coordinates (WC). L is the location of your light source within this box, also in WC. the projection of P onto a surface is

 ` S = a(L - P) - P `

if S is horizontal, where z = 0, then

 ` a = zp / (zl - zp) `

and the coordinates of S in world coordinates are

 ``` sx = (xp * zl - xl * zp) / (zl - zp) sy = (yp * zl - yl * zp) / (zl - zp) ```

the texel value at S(sx, sy, 0) is the same as the texel value at P.

generic solution
in many instances you want to project a lightmap onto surfaces that are neither horizontal nor axis-aligned on a grid. hacking around with some matrix operations yields the following solution. the projection of P onto S with lightsource at L is

 ` S = P[(L . G)I - GL] `

P and L are row vectors and G, the normal of the target surface S, is a column vector. I is an identity matrix. note that the matrix product GL is a 4x4 matrix, and the dot product (L . G) is a scalar.

if you want a full derivation of the following formulation, send e-mail to me at h2jang@scs.ryerson.ca

take care.

important note
in both solutions, the box faces are *perspectively* projected. as a result, you should not simply project the four corners of a box face and linearly interpolate to yield all texel values for the interior of the box face.

because of this perspective projection, you may need to play around with the location of the lightsource to arrive at the results you want.

Response provided by Hin Jang

 This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.