Handmade Hero»Forums»Code
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Best way to create and use lightmaps for 2d lighting
I currently use gimp to make lighmaps. And render it lightmap buffer and blend it to the frame buffer. As shown inlighting 2d.Is there a better way of doing this without shaders.
And why do people always try to do blending using shaders only.
Mārtiņš Možeiko
2562 posts / 2 projects
Best way to create and use lightmaps for 2d lighting
Edited by Mārtiņš Možeiko on
Because all modern GPU's do rendering in shaders. GPU's don't have fixed-function-pipeline anymore. Only few bits and pieces are left (like texture sampling or blending with framebuffer). Its just more flexible to do shaders directly, there's really no reason to rely on FFP emulation and limit yourself only on what old FFP can do.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Best way to create and use lightmaps for 2d lighting
Thanks. Is there a way to programmatically create lightmaps.
Bryan Taylor
55 posts
Best way to create and use lightmaps for 2d lighting
msmshazan
Thanks. Is there a way to programmatically create lightmaps.

Yes.

There is no one "right" way to do it, but I think the simplest option would be to raytrace them.

For each pixel of your lightmap:
For each light:
Trace a ray from the pixel to the light
If you didn't hit anything:
Add the light color (attenuated by distance) to the pixel.

The result is your lighting buffer, which you can then blend in.

Trace against the bounding boxes of sprites, and then against their pixels (treat each pixel as a box) only if you hit the box.

This is not completely accurate, but is usually good enough. (And let's face it: *nothing* in 2D rendering is accurate.) You can (and probably should) blur the result slightly to smooth out the edges.

That gets you direct lighting. Indirect / bounce lighting is also possible: basically, in addition to tracing to the light, trace a bunch of rays in all directions. When those rays hit something, check whether *that* pixel is lit, and if so add a percentage of that lighting to the original. (This is all much easier in 3D where you know the normal vectors on everything.)

Indirect lighting is going to be slow, so better to precompute rather than calculating at runtime.
Shazan Shums
159 posts
Some day I will make quality software. Programming FTW.
Best way to create and use lightmaps for 2d lighting
How to use normal maps with light maps ?
I currently have 2 buffers one lightmap buffer and other unlit buffer and i blend multiply both and draw to the screen.
Now where should I put the normal map when drawing.
Is it in the lightmap buffer or some other place.
Mārtiņš Možeiko
2562 posts / 2 projects
Best way to create and use lightmaps for 2d lighting
Normal maps are for dynamic lighting. They change normal of surface to fake a geometry.
You use them same as regular vertex normals, just do that per pixel not per vertex. Plug them in your lighting equation and use the resulting value as pixel light value.