Sign in to follow this  
fantasy_world

How to generate lightmap ?

Recommended Posts

fantasy_world    100
Hi all, I have learnt how to use multitexturing and people say it's for lightmap. I know that multitexturing is for lightmap but I don't know how to generate lightmap. Then if I have lightmap and I want to render a scene with it, may I have to recreate the FVF to include the UV for lightmap or do something else ? Help me Thanks

Share this post


Link to post
Share on other sites
Illco    928
You can use an existing software package to render lightmaps, like 3D Studio Max, or write your own (search these forums for some hints). If your lightmap textures are mapped different from your diffuse textures, which is usually the case, you indeed need an extra set of texture coordinates in per vertex. This should be reflected by the FVF by setting it to D3DFVF_TEXCOORD2 instead of ~1.

By the way, multi-texturing is useful for a lot more things than lightmapping! Greetz,

Illco

Share this post


Link to post
Share on other sites
fantasy_world    100
hi,

I want to write my own program to create lightmap. And I have found something to read. I'll come back to ask if I find something to ask.
Thanks

... Oh, how to create lightmap by using 3dsmax ?

Share this post


Link to post
Share on other sites
stroma    218
Quote:
Original post by fantasy_world
hi,

I want to write my own program to create lightmap. And I have found something to read. I'll come back to ask if I find something to ask.
Thanks

... Oh, how to create lightmap by using 3dsmax ?
to use 3dsmax, search "baking" and "rendering to texture". i found that : http://67.15.36.49/team/Tutorials/Lightmapping/lightmapping.asp


humus has a code for lightmapping with bsp trees like this( i havent tried it )

/***** Lightmap generation code *****/

unsigned char *lMap = new unsigned char[tw * th];
memset(lMap, 0, tw * th);

vec3 samples[SAMPLE_COUNT];
for (unsigned int x = 0; x < SAMPLE_COUNT; x++){
do {
samples[x] = vec3(float(rand()), float(rand()), float(rand())) * (2.0f / RAND_MAX) - 1.0f;
} while (dot(samples[x], samples[x]) > 1);
}

index = 0;
for (i = 0; i < model->getBatchCount(); i++){
Batch *batch = model->getBatch(i);
Vertex *src = (Vertex *) batch->getVertices();
unsigned short *inds = (unsigned short *) batch->getIndices();

for (j = 0; j < batch->getIndexCount(); j += 6){
TextureRectangle *rect = texPacker.getRectangle(index);

vec3 dirS = (src[inds[j + 1]].pos - src[inds[j]].pos);
vec3 dirT = (src[inds[j + 5]].pos - src[inds[j]].pos);

vec3 pos = src[inds[j]].pos;
vec3 normal = src[inds[j-1]].normal;


for (int t = 0; t < int(rect->height); t++){
for (int s = 0; s < int(rect->width); s++){
vec3 samplePos = pos + saturate((float(s) / (rect->width - 1))) * dirS + saturate((float(t) / (rect->height - 1))) * dirT;

float lightSum = 0.2f;
for (unsigned int light = 0; light < LIGHT_COUNT; light++){
vec3 lightVec = lightPos[light] - samplePos;
float diffuse = dot(normalize(lightVec), normal);

float sampleSum = 0.0f;
if (diffuse > 0){
coll.pushSphere(samplePos, 5);

for (unsigned int k = 0; k < SAMPLE_COUNT; k++){
if (!coll.intersect(samplePos, lightPos[light] + samples[k] * lightSize[light])) sampleSum += 1.0f / SAMPLE_COUNT;
}

sampleSum *= saturate(diffuse) / (1.0f + 0.000001f * dot(lightVec, lightVec));
}
lightSum += sampleSum;
}
lMap[((rect->y + t) * tw + (rect->x + s))] = (unsigned char) (255.0f * min(lightSum, 1.0f));
}
}

index++;
}
}

Image image;
image.loadFromMemory(lMap, tw, th, FORMAT_I8, true, false);
image.saveImage("LightMap.dds");


you can find his framework at www.humus.ca

hope helps..

Share this post


Link to post
Share on other sites
fantasy_world    100
Quote:

to use 3dsmax, search "baking" and "rendering to texture". i found that : http://67.15.36.49/team/Tutorials/Lightmapping/lightmapping.asp


sorry for replying so late.
thanks for your help.

I don't really understand the code from humus, but I have found a good tutorial. It guides me step by step, it's easy to understand.

But it doesn't mention how to pack these lightmaps into a larger texture.
If a texture has 1024x1024 dimension, does it make a problem ?

or do you have any way to render lightmap efficiently ?

Share this post


Link to post
Share on other sites
stroma    218
"If a texture has 1024x1024 dimension, does it make a problem ?"

. no not really, just it will use graphics memory much and you should take care of that. making one big lightmap texture may be efficient if you use multipass (no state shange for light pass) but in most cases lightmap cannot fits to one texture. so in your code, there should be functions to switch between these.
lots of the games uses small lightmaps. but you can light a whole game level with 1024x1024 size texture. just depends on your needs.

"I have found a good tutorial. It guides me step by step, it's easy to understand"

. can you share?? ;)

Share this post


Link to post
Share on other sites
fantasy_world    100
Quote:

"I have found a good tutorial. It guides me step by step, it's easy to understand"

. can you share?? ;)


Ofcourse I'm willing to share.
link: http://members.net-tech.com.au/alaneb/lightmapping_tutorial.html

I still have some troubles. If two triangles are next to each other but they are belong to two different subsets, that means they have their own textures but they share the same two vertices. So what about the texture coordinates of those two vertices ? Their UV coordinates are the UV coordinates of the triangle 1 or triangle 2 ?

And while I generate lightmaps, I compute the UV coordinate for one vertex and then in the next triangle I have to recompute the UV cooords for this vertex again. SO the previous computing UV will be replaced by the new one. Does it make any mistake ?

thanks.

Share this post


Link to post
Share on other sites
stroma    218
Quote:
Original post by fantasy_world

Ofcourse I'm willing to share.
link: http://members.net-tech.com.au/alaneb/lightmapping_tutorial.html
yeah, i know that. it is not a good demo. no shadows, no poly sort..etc just easy to implement but really bad. you can make same effect even with dynamic lights with todays cards. but lightmapping most common usage is for "static shadows". i mean your calculation is not adapted for this. and generates lots of LM with little geometry. that will dicrease performance.
so i suggest you to use 3dsmax, maya, softimage..etc. you can also calculate GI or bumpmapped lightmaps with these. and examine some q3bsp loader for usage of LM s. its clear. ;)

Share this post


Link to post
Share on other sites
fantasy_world    100
so is there any good tutorial about lightmapping ?
I just want to learn how people do. I have tried to create lightmap by using Max6 (...it works not well, maybe I made something wrong), but I want to generate it myself.

any solution for my previous question ? how to create a right uv coord ?

thanks

Share this post


Link to post
Share on other sites
stroma    218
there were some old but good tutorials about lightmapping at www.flipcode.com
calculating right uv s is not a easy job.
and if you know delphi, search delphi3D. there is a good open-source lightmmapper there.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this