PDA

View Full Version : Need some help with generating lightmaps


Borf
09-26-2005, 07:27 AM
Hi,

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

roel
09-26-2005, 07:39 AM
just a quick guess: try:

cVector3 pos3d = v1 + (edge2*mx)+(edge1*my);

or something. some intuitive explanation: on top of the code you create two vectors that create a span (edge1 and edge2), and they are both relative to v1. in the calculation for pos3d you use v1 as base and scale in the directions of the edges, so you should use only the edges relative to v1, and that is not edge3. this explanation sucks, sorry. good luck.

kusma
09-26-2005, 05:32 PM
i'm not meaning to be an ass and ruin your fun here, but is there a really good reason why you don't offload shadowmap-calculations to the content creation tools like 3dsmax? giving your artists more freedom and contro is a GoodThing(tm)...

roel
09-27-2005, 12:51 AM
I made a lightmapper too, its quite interesting and challenging. I guess that's a good reason.

Ed Mack
09-27-2005, 09:46 AM
I enjoyed the learning curve, and real-time lightmapping is interesting. See http://www.frustum.org for gpu-based realtime lightmapping (or so I remember)

roel
09-27-2005, 12:22 PM
I enjoyed the learning curve, and real-time lightmapping is interesting. See http://www.frustum.org for gpu-based realtime lightmapping (or so I remember)

If I'm correct, these two things are totally different actually (I guess that you mean shadow mapping). Confusing names though.