View Full Version : multitexture as common lightmap fall all drawing?

10-18-2011, 02:32 PM

So I've got this big 2d image for map and to implement lighting I got the lightmap image for it and a shader. Lightmap image is a simple black-white image. I know how to use multitexturing in a "normal" way whay, when you bind the texture and set glMultiTexCoords() but what when you want to use that texture as lightmap for all your objects you are drawing? This lightmap must be used when drawing other objects over the map, for example when drawing a player over the map on (x,y) position, I must use lightmap (x,y) position. The problem is I have no idea how to resolve object coordinates to correct texture coordinates in shader.

For example: Both map image and lightmap image are 800x600. I draw the map and use lightmap normally with multiTexCoords(). Now I want to draw player that is located at (100,100), but this time I can't set the lightmap texture with glMultiTexCoords(). How to calculate proper lightmap coords in shader?

this is my fragment shader now:

uniform sampler2D texture;
uniform sampler2D lightMap;
varying vec2 VaryingTexCoord0;

out vec4 fragColor;

void main (void)
// original texture
fragColor = texture2D(texture, VaryingTexCoord0.st);

// common lightmap affects lighting the pixel
fragColor *= texture2D(lightMap, NEED_THIS.st);

Any ideas on how to resolve texture coordinates I must use in second step?
Thank you all for your time.

Alfonse Reinheart
10-18-2011, 04:21 PM
It seems to me that you want to compute the texture coordinates based on the world-space position of the object you are rendering.

10-18-2011, 06:52 PM
yeah that sounds right.. I have world space coordinates of lightmap and the player, and I need the proper normalized device coordinates for lightmap texture if I understood correctly.
Any ideas how to do that? Multyply with inverse MVP matrix? It would be best (easiest for me) if I could extract it from gl_FragCoord though.

10-19-2011, 02:43 AM
Take the vertex position (.xz) in world coordinates and scale by 1 / world size. That should give you coordinates in 0..1. If your world coordinates are not 0,0 to width, height then you need to apply an offset as well.

Edit:*Do this computation in vertex shader and pass to fragment shader through varying.