# Lightmap coordinates, planar mapping

Hello all,

I am writing a program to calculate lightmaps given a triangle soup and a list of light sources. What I’m having trouble is computing the correct world-space position of each lumel. I found a few tutorials about generating lightmap coordinates on the internet, Polygone’s tutorial comes to mind, and I’m currently stuck on planar mapping. Here’s what I currently have:

1. Find the largest component of a tri’s normal. Let’s say Y for example.

2. For every vertex, assign X to the u lightmap coordinate and Z to the v lightmap coordinate

3. Calculate the max and min lightmap V and U and the delta V and U

4. For every vertex, subtract the min U from the lightmap’s U and the min V from the lightmap’s V and divide both by their respective deltas

Now, up to this point everything is roses. Following is the part I’m having trouble with.

1. Calculate the zero vector and the two edge vectors. The zero vector is the vertex with lightmap coordinates 0,0. The edge vectors are the vectors from 0,0 to 1,0 and 0,0 to 0,1 respectively along the xz plane of the triangle. This is how I would calculate all 3 vectors for the above example:

aZeroVect.x = minU;
aZeroVect.z = minV;
aZeroVect.y = - ( norm.x * minU + norm.z * minV + d ) / norm.y;

lVect1.x = maxU;
lVect1.z = minV;
lVect1.y = - ( norm.x * maxU + norm.z * minV + d ) / norm.y;

lVect2.x = minU;
lVect2.z = maxV;
lVect2.y = - ( norm.x * minU + norm.z * maxV + d ) / norm.y;

aEdge1 = lVect1 - aZeroVect;
aEdge2 = lVect2 - aZeroVect;

This makes sense to me, although I’m not 100% sure I’m doing it correctly.

1. For each lumel, calculate its world-space coordinate as follows. My lightmaps right now are all 16 by 16.

x /= 16;
y /= 16;

pos = zeroVect + ( edge1 * x ) + ( edge2 * y );

Where x and y are the lumel coordinates and pos is the final lumel position.

Can anyone see if and what I’m doing incorrectly.

By the way, I apologize in advance for the potential off topic post. I thought about going to the beginner’s forum. I also did a search on ‘planar mapping’ in this forum but didn’t find anything useful.

I thank you all in advance

–Marcos

You’ve left out some re-naming, I think.

Assuming that “x” and “y” are pixel coordinates, not texture coordinates, and edge1 and edge2 are the edge vectors of your triangle, then it should work.

However, the way you calculate your edge vectors in 5) seems quite suspect.

First, I recommend that you, for sanity, use the interval [0,1] for each texture coordinate. Second, the zero vectors and edge vectors are not as complicated as you make them. If you pick one corner of the triangle (C0) and the other corners are C1 and C2, then

ZeroVector = C0
EdgeVector1 = (C1-C0)
EdgeFector2 = (C2-C0)

Then, for any given texture coordinate (u=[0,1],v=[0,1]) you can derive the world position as C0 + u*(C1-C0) + v*(C2-C0).

Note that for a triangle, (u>=0, v>=0, u+v<=1) so you can stuff two triangles into one texture “square”, but be careful with the edge conditions. And remember that with texture repeat or when tiling these tiles, (0,0) is BETWEEN the leftmost pixel and the previous rightmost pixel; (1/32,1/32) is in the center of the leftmost pixel for a 16x16 texture.

>> What I’m having trouble is computing the correct world-space position of each lumel.<<

have a search for ‘barycentric coordinates’

>>By the way, I apologize in advance for the potential off topic post. I thought about going to the beginner’s forum. I also did a search on ‘planar mapping’ in this forum but didn’t find anything useful.<<

people what have we become I’ve become affraid to post something here… MtPOI