dynamic lighting in a 2D game...

Started by
13 comments, last by neonstar 23 years, 9 months ago
i was wondering if anyone knew how to implement lighting in a 2D game. i''m asking because i''d like to have certain predefined places inside a map which have different lighting values which would darken (or lighten) the overall area of the screen. this would work to darken character sprites, enemy sprites, objects, map tiles, and soforth. just trying to give my game a bit more realism. sorry if i''ve overlooked and obvious articles or past posts that have asked the same question, but i''m in kind of a rush to post this message since i''m at work ;-) thanks again, david
--david@neonstar.netneonstar entertainment
Advertisement
Use a lightmap. A light map is just a greyscale bitmap that is mixed with other bitmaps (presumably u''d just be mixing part of it with the backbuffer, in this case, though)
U''d have to code a function for the mixing, but, basically, if it were the middle grey tone, it wouldn''t change the color at all, and then the darker it got, the more it would darken the pixel at that corresponding spot, and the lighter it got, the more it would lighten.

Hope that helps...

good idea. but that would only work well if i were using pre-rendered screens. i''m using tile maps, so i dunno how well that would work...

if anyone has an idea, please let me know!


thanks,
dave
--david@neonstar.netneonstar entertainment
quote:Original post by neonstar
good idea. but that would only work well if i were using pre-rendered screens. i''m using tile maps, so i dunno how well that would work...


A lightmap should work well enough.

If you''re doing a tile engine, you''re probably using some form of blit to write the tiles to the screen/graphics buffer. Instead of doing a straight blit, do a blending blit, where each pixel to be drawn is multiplied by the lightmap before being drawn.



---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

so would i multiply the color value of each pixel by it''s corresponding light map value? no bit shifting involved with rotating the color? plus, since i might have to change the area of one of my maps, changing the lightmap might not be too easy to accomplish...

another idea i had was to use alpha blitting with differently lit or shaded sprites, but that only allows it to be shaded/lit as much as the color of the shadow/light is...

could alpha blending be a good way to do it?
--david@neonstar.netneonstar entertainment
quote:Original post by neonstar
so would i multiply the color value of each pixel by it''s corresponding light map value? no bit shifting involved with rotating the color? plus, since i might have to change the area of one of my maps, changing the lightmap might not be too easy to accomplish...

another idea i had was to use alpha blitting with differently lit or shaded sprites, but that only allows it to be shaded/lit as much as the color of the shadow/light is...

could alpha blending be a good way to do it?



I didn''t mean to imply that the end result would come of simple multiplication (although, with careful layout, this could be done). Whether you use a lightmap or alpha blending, it comes down to a composite of two values. In the case of the lightmap, it''s a composite of the tile pixel with a lightmap pixel; for alpha blending, a composite of the tile pixel with the destination screen pixel.

For alpha blending, you essentially do this:

    dest.r = (1 - alpha)*dest.r + alpha*src.r;dest.g = (1 - alpha)*dest.g + alpha*src.g;dest.b = (1 - alpha)*dest.b + alpha*src.b;    


where dest is the screen pixel and src is the pixel from the tile to be plotted. Here, alpha == 0 is fully transparent and alpha == 1 is fully opaque.

This is probably fairly similar to using lightmaps. If you first draw the entire screen in your tiles at full intensity, then you could apply patterns of dark and light gradiated tiles with alpha to achieve your effect.

I''m sure there might be better, more efficient methods to doing this, but this might get you started.


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
i guess the only reason i''d have to use alpha blending would be for colored lighting, which is a definite possibility. i''m just trying to give me 2D game a more 3D-ish feel...

hey, thanks for the info, mossmoss, it helped a lot.

david
--david@neonstar.netneonstar entertainment
Some food for thought:
If you don''t mind the lighting being done on a per tile basis (look at Diablo) then you could set a light value for each tile and act accordingly.

IF you want more rounded lighting, you could do a couple things. 1) would be to have your map editor generate a lightmap. (basically a big bitmap) but this takes alot more space and you can''t do dynamic stuff easily. Method 2) could be to write some routines that would take a point location and brightness/color, and then mathematically brighten up a radius (and clip for walls/etc). If you got real fancy you could do some ray-tracing for spill out of doorways etc.. but this becomes very computaionaly intensive, and if your buffers are in vidmem you have a slow bus to deal with.

Another option is to use direct3d. You can specify point lights and apply them to a scene. This is done either my hardware or software automatically. (I don''t know what other cards other than the GeForce do hardware lighting) But a problem with this is that the light will spill past walls etc. cause your map is flat... One way around this might be height-mapping (look at the "using direct3d for isometric tile games" or something like that, demo4)

Hope this inspires someone, cause I''d really like to implement dynamic lighting in my game as well, just not sure whats the best path to take...

-Zims

Zimans,

using Direct3D is one thing that I was wondering about. i didn''t know if it was possible or not to do, but i guess it would be... i''ll try to look for that article... if i get it to work, then i''ll tell you about it. maybe i''ll even like direct3D enough to where i''ll just scrap 2D and do it 2D anyway.... ;-)

thanks alot,
david
--david@neonstar.netneonstar entertainment
If you''re using Direct3D for your lightning, don''t put too much lights in your scene or your performance will drop very fast.

Darkening
---------------------------Unfortunately, no one can be told what a bug is.You have to see it for yourself...

This topic is closed to new replies.

Advertisement