I'm working on a small 3d engine, and I'm planning on using lightmaps for static lighting. I'm at the point where I have to generate the lightmaps now, but for some reason I can't get the texel-world-coordinates right.
the triangle I'm rendering a map for is (v1,v2,v3) where v1,v2,v3 are vectors
the UVs for the lightmap are (0,0), (1,0) and (1,1)
to start with, I'm using 1 static light at (0,0,0), and I'm using the following code to generate the maps
cVector3 edge1 = v3 - v1;
cVector3 edge2 = v2 - v1;
cVector3 edge3 = v3 - v2;
for(int x = 0; x < LIGHTMAPSIZE; x++)
{
for(int y = 0; y < LIGHTMAPSIZE; y++)
{
float mx = (float)x / (float)LIGHTMAPSIZE;
float my = (float)y / (float)LIGHTMAPSIZE;
cVector3 pos3d = v1 + (edge2*mx)+(edge3*my);
float dist = 255-max(pos3d.Magnitude(), 255);
map[x][y][0] = dist;
map[x][y][1] = dist;
map[x][y][2] = dist;
}
}
the problem is that pos3d doesn't contain the proper world-coordinates, which means the lighting isn't calculated properly as well. When I use (pos3d.x%256) as color, it does appear as a gradient on 1 triangle, but when I have 2 triangles next to eachother the gradients do not match











