Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] lighting

This thread is locked; no one can reply to it. rss feed Print
[A5] lighting
bleepBlop
Member #14,822
January 2013

I am trying to create the ability to have lighting in my games. I first wanted to have ones which would cast a certain shadow on objects, or just not to execute if entering the pixels of an object, but atm I'd be happy with even a simple light and no other rules.

I tried making a transparent sphere, but the edges would need to be blurred and I don't know how to do it.
I tried to add a dark rectangle over the background and then put a transparent yellowish circle, but again, no blur, I thought maybe I could use the circle to just cut darkened rectangle, however I didn t know how to do it.
I tried to take an image premade in photoshop using Gaussian blur but in allegro it just showed like a square, I guess filters don't pass well in allegro.

Basically, I'm studying java for a year now, made a simple snake-like game in c++/allegro a year ago and now I am trying to do something more complicated with this.
Is there any tutorial or someone showing how he/she achieves this with allegro 5? I understand I could somehow attain this with blenders however I do not understand much from the documentation, could you guys maybe know of some more extensive help?

I don't know how the community here is relating to newbies but please, if you have nothing constructive to add, just don't bother, I already feel bad about not figuring this out by myself.

Thank you for reading this

Kris Asick
Member #1,424
July 2001

One of the key things of note with Allegro 5, and probably one of the reasons why you're running into trouble, is that A5 originally did not support multiplication blending on its own, which is somewhat necessary to make a good lightning system for a 2D game, so you have three options:

1. The best approach that would provide the strongest framerate would be to write a fragment shader that could do the lighting for you, by giving it a source texture with all your light details on it and blending it with the overall scene prior to drawing it all to the screen.

2. If you want to avoid fragment shaders for whatever reason, A5 does support subtractive blending, which does indeed work in making a lighting engine, so long as you keep in mind that the texture that will act as your lighting needs to be inverted. (Black = Full Brightness, White = Total Darkness, Red = Cyan, Blue = Yellow, etc.)

3. You can trick more recent versions of A5 into doing multiplication blending by doing something like this, which is mentioned in the manual:

al_set_blender(ALLEGRO_ADD, ALLEGRO_DEST_COLOR, ALLEGRO_ZERO);

As for your mishap with the gaussian blur, you need to remember to engage blending modes using al_set_blender() and you also need to make sure you are blurring across the alpha channel with your graphics software, instead of doing the blur on the main RGB colour channels, since when working with 32-bit colour you want transparent parts of your images to have an alpha of 0.0, and the visible parts to have an alpha of 1.0 (or 255, depending on if the channel is described in floating point or as a byte value.)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Edgar Reynaldo
Member #8,592
May 2007
avatar

I can think of a couple ways to achieve lighting.

Option A)
1 - Draw scene at full brightness.
2 - On separate bitmap color it black with varying levels of alpha. An alpha of zero would make it clear and when drawn normally these would be your brightest areas. alpha 127 would be normal lighting, and alpha 255 would be fully opaque black, casting a full shadow.
3 - Draw this shadow map over your scene normally.
4 - Flip

Option B)
1 - Clear to black
2 - Create a simple light map with one byte for each pixel, representing the alpha value of that pixel on the scene.
3 - Write a shader that reads this light map when drawing to your [back] buffer, applying the alpha value of the light map to the color value of the pixel it is drawing to the buffer.
4 - Flip

bleepBlop
Member #14,822
January 2013

I don t know what a fragmented shader is, or a lightmap, as I said I have only done java and very very simple allegro game.
However I made the following code which in a loop imitates light almost perfectly :

#SelectExpand
1void makeCircleLight(Player &player) 2{ 3 float r = 1.0f; 4float g = 1.0f; 5float b = 0.0f; 6float a = 0.03f; 7int size = 125; 8 for(int i = 0; i < 30; i++) 9 { 10 al_draw_filled_circle(player.x, player.y, size, al_map_rgba_f(r*a , g*a , b*a , a)); 11 if(i < 6) 12 { 13 size -= (rand() % 7) + 3; 14 } 15 else 16 size -= (rand() % 4) + 1; 17 } 18 19}

If anyone can find any use for it, I'd be very happy. I also would like to ask if it is possible to somehow tell the circle not to draw where the pixels of another object are.

I tried doing the same with pixels, drawing their alpha less and less the furtherer away they would be from the origin, however it would be too slow to draw hundred of pixels then to just draw 30 circles constantly of different sizes.

Kris Asick
Member #1,424
July 2001

Dynamic lighting which reacts to the state of the world around you is typically a difficult thing to do, both in 2D and 3D. Even expert game programmers can struggle with it sometimes. Thus if you're very new to it then there's little we can do to help you get going other than suggest what pieces of Allegro to use, since no one's gonna spend days on end writing a lighting engine for you. (Unless you pay them to of course.)

There's a bunch of tutorials online if you look for them and when you see how in-depth they are you'll understand why it's not something we can just give you little pieces of info about and make it magically work. We can guide you down the right path of what pieces of Allegro to use, but you still need to know the fundamental concepts as most of us aren't teachers. ::)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

bleepBlop
Member #14,822
January 2013

sorry I did not expect it was so hard to do that, I simply can't find any tutorials even on doing something as basic as what I did with my little piece of code.
I should probably use keywords as in what I use to make lighting when I search for it not simply look for "allegro lighting tutorials".

At the moment, the light I make is enough for my needs with a bit of fixes but if there is anyway for you guys to give me some examples of the tools I need to use in usage, even for something else. Or if you could name them in a way in which I could actually find them, not the name you guys know them by but actually how they are called, that would be great.

Sorry for being a nuissance:(

Kris Asick
Member #1,424
July 2001

Could be worse. You could've come on here asking us flat out to make a lighting system for you. ;)

Here's a link you should read that will help you understand the concepts behind this: http://elysianshadows.com/phpBB3/viewtopic.php?f=13&t=5439

Following this, just search Google for "2D lighting tutorial" and you'll find stuff. You won't find Allegro-specific stuff, but then you can apply the concepts you learn the Allegro's methodology.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

bleepBlop
Member #14,822
January 2013

Thank you very much, the link is extremely useful.
Again, very sorry for asking stupid things I just had no idea where to go to.

Go to: