UV of a LightMap.

Hi JGO!

I have encountered some problems trying to use lightmaps for me 2d spotlights and conelights.

I’m using similar ones to this.

I’m building the lights’ geometry with meshes using a triangle fan. Since both can cast shadows they don’t always have a circular or a cone shape.
My problem is I can’t figure out how to calculate the texture coordinates for the bound textures in the shader.

Let’s say I have a window size of 1024x768 and the point light is at 5000, 4000 in world space.
How can I calculate what texture coordinates to use for sampling the light map texture in the fragment shader.

Any advice would be greatly appreciated! :slight_smile:

If you are not using indexed rendering, better using that than triangle fan

I’m using GL_TRIANGLE_FAN at the moment but I’ll swap to indexed rendering then.
How would that solve my texture coordinate problem though?

Can’t you just always use a square texture regardless the shadow shape? Or are there some negative consequences?

The lightmap itself is a square power of 2 texture. The light’s geometry is uneven though because some vertices are blocked by shadow casters so I can easily get a light mesh like this:

The geometry is so small there won’t be a performance delta however

Could you generate lightmap with transparent pixels corresponding to casters? In this way you could just have one single square as vbo for all the lightmaps, updating just the matrix, switch the texture and you will be done

Isn’t there a way to calculate it instead of me having to modify everything I have done so far? :smiley:

Sure, but it is a little more tricky, you should take the position in model space, translate it in order to have the origin on the lower or upper left corner, normalize the axes and save the values inside the texture coordinates

You should be able to calculate the texture coordinates for your light mesh like this:


 (pseudocode)
 lightmaptexcoord.xy = normalize((light.xy - vertex.xy) / light.radius) + 1.0 * 0.5

Do this for every vertex of the image you provided above and it should work.

This is what I was looking for, thank you!