Lighting with projected textures?


I have a problem with using projected textures for lighting:
What I want to is to light my existing scene with a projective texture. For that I calculate the projected position in my vertex shader with:
projPos = gl_ModelViewProjectionMatrix * gl_Vertex;

I managed to write a fragment shader, in which I used a LightMap to light the street. Therefore I used 2 sampler2D an the Position. I did it in this way:

varying vec4 projPos;
uniform sampler2D LightMap;
uniform sampler2D Street;

void main(void)
vec4 streetColor = texture2D(Street,gl_TexCoord[0].st);
vec4 lightColor = texture2D(LightMap,;
if (lightColor.x > 0.0)
streetColor += lightColor;
gl_FragColor = streetColor;

I know this isn’t yet very nice, but it shows what I want to do. The LightMap is only black and white and therefore I only check one value.

But what I don’t know is, how I can use this to light a scene with many textures, do I really have to access every texture for it’s own, or is there a way to acces all textures per fragment an add a light-value to them?

I hope anyone can help me out of this


The if statement is slow and for better style you should write a the gl_FragColor outside the if-statement.
If the lightmap is unsigned, you don’t need to have an if-statement at all. Just add it.
A different way to do this is to do the “full brightness” calculations and darken it by modulation with the “lightmap” like this:
gl_FragColor = texture2D(Street,gl_TexCoord[0].st) * texture2D(LightMap,;

For multiple textures you need to combine the textures and modulate the result.
Hmm, if you want that multiple lights generate overbright areas, you need to add per light.

PS: Lighting, not Lightning. :wink:

thank you for your english lesson, I updated my post :slight_smile:

back to my problem:

I think my problem is, that I don’t really understand the texture mapping in GLSL. Imagine a scene with a street and a wall and I want to project a light on both of them.

How can I know which texture of both I do have to access to multiply this with my LightMap? Is there any method, to get the color value of the texture, which is used for the current fragment?

How would I for example write a fragment shader, that does the texture mapping for a scene with multiple textures? I think that is the thing that I don’t understand :frowning:

Your talking about fragment programs here. They don’t care about if you draw a wall or a street, they just need to find the correct texture unit number set in the samplers.

To get different textures per object (not multitexturing) the routine which draws needs to
glBindTexture(GL_TEXTURE_2D, wallTexObjID);
glBindTexture(GL_TEXTURE_2D, streetTexObjID);

In this case lets assume the glActiveTexture is GL_TEXTURE0 at all times for the rendering and GL_TEXTURE1 while you downloaded the lightmap once long ago. Alas using multiple textures per primitive, aka. multitexturing.

You just need to setup your samplers once. Rename your “Street” sampler to decalMap and it gets clearer.

glUniform1iARB(decalMapLocation, 0);
glUniform1iARB(lightMapLocation, 1);

From now on the decal color (street or wall texture color) is always taken from the texture unit 0 and the lightmap values from texture unit 1 while that fragment shader runs.

BTW, to eliminate texture switching overhead you can put all the different textures of a model into one big texture, this is called a texture atlas and used for game figure skins and lightmaps very often. But beware of the mipmapping problems you earn.

thank you very much, this helps me a lot…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.