Handmade Hero » Forums » Code » Best way to create and use lightmaps for 2d lighting
msmshazan
Shazan Shums
162 posts

Some day I will make quality software.
Programming FTW.

#11289 Best way to create and use lightmaps for 2d lighting
6 months, 2 weeks ago

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.

Just being curious on programming....
mmozeiko
Mārtiņš Možeiko
1477 posts
1 project
#11290 Best way to create and use lightmaps for 2d lighting
6 months, 2 weeks ago Edited by Mārtiņš Možeiko on March 15, 2017, 3:20 a.m.

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.
msmshazan
Shazan Shums
162 posts

Some day I will make quality software.
Programming FTW.

#11291 Best way to create and use lightmaps for 2d lighting
6 months, 2 weeks ago

Thanks. Is there a way to programmatically create lightmaps.

Just being curious on programming....
stevetranby
Steve Tranby
12 posts

Code Monkey

#11292 Best way to create and use lightmaps for 2d lighting
6 months, 2 weeks ago

Not sure if this is what you're asking about, but there's a couple tools for auto-gen normal maps from regular 2d art, with some editing.
https://www.codeandweb.com/spriteilluminator
http://www.snakehillgames.com/spritelamp/

I see 0xdeadc0de everywhere
btaylor2401
Bryan Taylor
55 posts
#11318 Best way to create and use lightmaps for 2d lighting
6 months, 2 weeks ago

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.
msmshazan
Shazan Shums
162 posts

Some day I will make quality software.
Programming FTW.

#11336 Best way to create and use lightmaps for 2d lighting
6 months, 1 week ago

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.

Just being curious on programming....
mmozeiko
Mārtiņš Možeiko
1477 posts
1 project
#11337 Best way to create and use lightmaps for 2d lighting
6 months, 1 week ago

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.