lightmapping trouble !!!

i use lightmaps to lit area around fireballs and similar stuff. color of world is multiplied by color of lightmap (i thing this assumptation is correct and it looks pretty good) but there’s a trouble when multiple lightmaps are on same spot.

for example i’ve got red and blue lights. red lightmap removes (almost) everyting except red. when blue lightmap is applied resulting color is black (or something very dark).

t thing lightmaps should be first added together and then multiplied with world texture. is it possible to do this in some way ?

You’re on the right track. Consider adding your lightmaps and modulating with the texture as you propose

Color = (LightMap0 + LightMap1) * Texture

This is the same as

Color = LightMap0Texture + lightMap1Texture

So you could achieve this effect in 2 passes using glBlendFunc( GL_ONE, GL_ZERO ) for the first pass, and glBlendFunc( GL_ONE, GL_ONE ) in the second.

Be sure to set your texture environment mode to modulate (the default)

glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

then put your texture in unit 0, and your lightmap in unit 1.

This technique works, will support any number of lights, and will work on 2 texture unit hardware.

yeah, this should work, thank you :slight_smile:

i read your post again and i actually don’t understand

i think correct equation should be

Color = clamp((LightMap0 + LightMap1)) * Texture

in this case

Color != LightMap0Texture + lightMap1Texture

or maybe, i don’t know how such lightmap should look like. currently my lightmaps have white edges and fade to color of light (red, blue, whatever). they have no alpha channel. is that corrent ?

i simply fear when i repeat your code 10 times (on extremly lit patch:)) resulting color will be (almost) white

can you explain this a little ?

Miko,

you’re right. In current unextended hardware we’re limited to 8 bits a channel, which can max out quickly if adding a bunch of high intensity lights. This is a limitation of the hardware, not the technique (if you can make that distinction :slight_smile: )

Try scaling your lights down a little. If you know the maximum number of lights that can affect a given object, then scale them such that they sum to approximately 1.

Also, you could try using float buffers, if precision is that important to you. You can read about the nVidia extension here
http://www.nvidia.com/dev_content/nvopenglspecs/GL_NV_float_buffer.txt

thanks for your reply but i still don’t understand. i was probably unclear last time.

assume i’ve got plain white lightmap (which should not affect texture in any way). when i apply this lightmap twice

Color = 1Texture + 1Texture = 2*Color

world will be twice bright as normal…

To answer your question, you need to understand that the gl clamps colors to the range [0,1].

To get the 2x lighing you mentioned, you would need to do the sum on the cpu (slow), or in a one pass combiner (fast), or float buffer, but the result will still be clamped to [0,1] before set in the frame buffer.

If you haven’t already, I strongly recommend reading the spec
http://www.opengl.org/documentation/spec.html

i know. i don’t want to produce 2x lighting but i fear i will do it.

let’s look at your first post

glBlendFunc( GL_ONE, GL_ZERO )
this will diable blending so i will render texture lit only by first lightmap overwriting anything that was in framebuffer before

glBlendFunc( GL_ONE, GL_ONE )
this will render texture lit by second lightmap and adds result to first (and clamps result to 0-1 range)

am i right ?

i thing plain white lightmap should not do anything

is this true ?

so assume i pass my plain white lightmap to both passes above and i’ll get results twice bright as normal (because i add my texture to itself = 2 x texture. and result is then clamped to 0-1 range but it’s still 2 x texture). and this is a problem. i twice used lightmap which was supposed to do nothing and my world is 2 x brighter. and this is a problem i’m talking about.

please can you tell me where i made a mistake ? (yeah, i know it’s difficult to make me understand but you will gain my neverending love if you help:))

glBlendFunc( GL_ONE, GL_ZERO )
this will diable blending so i will render texture lit only by first lightmap overwriting anything that was in framebuffer before

That’s right, if you add the first pass, anything in frame buffer will be added to your object, making it look transparent.

glBlendFunc( GL_ONE, GL_ONE )
this will render texture lit by second lightmap and adds result to first (and clamps result to 0-1 range)

am i right ?

This will add the clamped value to the contents of that frame buffer, then that result will again be clamped to [0,1].

i thing plain white lightmap should not do anything
is this true ?

I’m not sure what you mean here. :confused:

it seems you don’t undertand me:)) maybe it’s my poor english, maybe it’s my knowledge of gl, maybe it’s god’s will i don’t care:)

try to explain how should lightmap look like. maybe it helps me to understand. currently my lightmap is a coloured circle on white background. edge is white (1,1,1), the point in the middle is for exapmle red (1,0,0), rest is something between these two extremes. no alpha channel is present. is this corrent?

i thing plain white lightmap should not do anything

it’s a texture filled with (1,1,1). when multiplied with world texture it does not change anything. of course i do not plan to use such lightmap, it was just example to illustrate a problem.

and the rest from my previous post…

so assume i pass my plain white lightmap to both passes above and i’ll get results twice bright as normal (because i add my texture to itself = 2 x texture. and result is then clamped to 0-1 range but it’s still 2 x texture). and this is a problem. i twice used lightmap which was supposed to do nothing and my world is 2 x brighter.

yeah, situation described above is really extreme case, but if i understand you curretly it blows the technique you’re talking about to hell

Aha! The background of your lightmap should be black, not white. There should only be color where there is light (white is a color here, it’s as bright as it gets). With a modulation, we only want to add the area of the texture that’s affected by the light map, i.e., where there’s light.

aha! :slight_smile: )) i love you :slight_smile: