Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Blending mode for additive lighting

This thread is locked; no one can reply to it. rss feed Print
Blending mode for additive lighting
Chris Katko
Member #1,881
January 2002
avatar

HURRY. A.CC IS FAST. ANSWER MY QUESTION.

I draw all my tiles and then, I draw "light" rectangles for each tile with this blender:

al_set_blender(ALLEGRO_BLEND_OPERATIONS.ALLEGRO_DEST_MINUS_SRC, ALLEGRO_ONE, ALLEGRO_ALPHA);

In retrospect, I'm almost certain I didn't even need it. (I think I'm just inverting black and white.)

What's the right one (or steps) for creating a lighting pass, where unless I draw "light" to this buffer, I get complete darkness?

Should I have two separate buffers? A lighting buffer, and a tile buffer, draw to them both (allowing multiple lights) and then combine them at the end?

I don't have relevant image examples but its like this left image:

{"name":"Color_add_subtract_450.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/4\/e43343cc13f700335b8b446467b03276.jpg","w":450,"h":339,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/4\/e43343cc13f700335b8b446467b03276"}Color_add_subtract_450.jpg

or maybe I should be using MULTIPLICATIVE lighting per this second image?:

{"name":"2sHjXGr.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/6\/d65454164423691dc730a10473ba13cb.png","w":1280,"h":1024,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/6\/d65454164423691dc730a10473ba13cb"}2sHjXGr.png

Either way, the more I think about it, the more I believe I need a second lighting buffer, that is then combined together at the end.

I wonder if shadows can be drawn the same way, or I should use a third pass / buffer for shadows?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Chris Katko
Member #1,881
January 2002
avatar

I read your post and it seems like techno babble.

Then I read a few hours of articles and I come back and go "Hmm, yeah, basically."

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

princeofspace
Member #11,874
April 2010
avatar

For what it's worth, in my code it looks like this:

[draw stage here]
al_set_blender(ALLEGRO_DEST_MINUS_SRC, ALLEGRO_ONE, ALLEGRO_ONE);
[draw shadows here]
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
[draw lighting here]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I read your post and it seems like techno babble.

Then I read a few hours of articles and I come back and go "Hmm, yeah, basically."

Mathematically, it's the only thing that makes sense. The multiply blender ;
al_set_blender(ALLEGRO_ADD , ALLEGRO_DEST , ALLEGRO_ZERO);
Takes src*dest + 0*dest and gives you src darkened by the dark areas in dest. Anywhere your shadow mask is white, it will 'show through'. Anywhere it is colored, it will tint whatever is behind it.

Additive lighting is the only thing that can make things lighter.

For what it's worth, in my code it looks like this:

[draw stage here]
al_set_blender(ALLEGRO_DEST_MINUS_SRC, ALLEGRO_ONE, ALLEGRO_ONE);
[draw shadows here]

al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
[draw lighting here]

How does the premultiplied alpha blender draw lights? By drawing partially opaque white? That works.

princeofspace
Member #11,874
April 2010
avatar

Yeah, for the pixel art games I make, it gives me the look I want. It also makes it easier (for me, anyway) for drawing primitives as dynamic lighting sources. To be honest, though, I wrote this code a while back, and don't remember why I came to this process. I think I read about I here on this forum!

Go to: