Archived

This topic is now archived and is closed to further replies.

dynamic lighting in a 2D game...

This topic is 6379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Check this adress out:
http://members.xoom.com/_XMCM/gumbyware/lighting.htm
It explains the basics of 2d lightning....



-------
Hardguy

Currently developing Fiend.
A horror as never seen before.

fiend.ingava.com

Share this post


Link to post
Share on other sites
thanks for the addie, i''ll check it out.

by the way, i looked at your fiend website, and the game looks great! i''m currently trying to write a, well kinda secrect, but 2D action game that''s got a semi-isometric kinda look to it, and it will have great graphics because i''ve obtained a great artist. I wanted to put lighting into it to make it look that much better.

Oh well, thanks for the info, i''ll check it out
david

Share this post


Link to post
Share on other sites
No wonder I couldn''t find this thread to follow up on it, it''s in theory not general.

I myself looked into the 2D lighting and this is what I''ve come up with. Using the example in the Isometric Direct3d tile enging I was able to create a 2d renderer using Direct3d with dynamic lighting.

By drawing every tile as a triangle pair, you can apply vertex lighting (wich is done by almost all hardware) to create your lighting. By pre-calculating a light map every frame (Take all your light "sources" and mathematically will an array with light values) you can apply the lighting as you render your tiles. Not only do you get dynamic lighting, you get colored lighting. And the more advanced your pre-calculation routines the fancier your lighting will look.

My first Idea was to use Direct3d Lights, but they don''t fit at all in a 2d game for two reasons. 1) They rely on 3d geometry for bouding, since your map is flat, you''d always get concentric circles vs. bounding by walls etc. and 2) they are alot more computensively heavy.

But with vertex lighting and a little pre-calculation some really nice lighting can be done. If you want the little test thing I hacked together just e-mail me.

-Zims

Share this post


Link to post
Share on other sites
Does any one have an idea how diablo 2 makes their
lightning in software mode?

-------
Hardguy

Currently developing Fiend.
A horror as never seen before.

fiend.ingava.com


Edited by - hardguy on June 28, 2000 5:01:41 PM

Share this post


Link to post
Share on other sites