Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Draw rectangle with alpha

This thread is locked; no one can reply to it. rss feed Print
Draw rectangle with alpha
mentalDisorder
Member #13,376
August 2011

Hi
I'm trying to draw simple rectgangle with alpha channel:

ALLEGRO_COLOR color = al_map_rgba(100 , 100, 200, 255);

al_clear_to_color(al_map_rgb(0,0,0));
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
al_draw_filled_rectangle(0, 0, 100, 200, color);

Rectangle is drawn but alpha channel is ignored.
What's wrong ?

Arthur Kalliokoski
Second in Command
February 2005
avatar

Did you set_color_depth(32) before setting video mode?

They all watch too much MSNBC... they get ideas.

mentalDisorder
Member #13,376
August 2011

No, because I use allegro5 and there isn't such a function.
Can't find substitute too ;)

Arthur Kalliokoski
Second in Command
February 2005
avatar

I really need to pay more attention :-/.

Maybe this will help.

http://www.allegro.cc/forums/thread/606420

This manual has links to allegro.cc questions concerning the function in question

http://www.allegro.cc/manual/5/index.html

They all watch too much MSNBC... they get ideas.

mentalDisorder
Member #13,376
August 2011

Have no idea what to do with al_set_new_display_option and al_set_blender functions.
What values should they reveive ?

Elias
Member #358
May 2000

You need to multiply the color components with the alpha.
(128,128,128,128) means 50% transparent gray for example.

--
"Either help out or stop whining" - Evert

mentalDisorder
Member #13,376
August 2011

yes I know how RGBA system works but I don't know how to tell allegro5 to use alpha channel !

Elias
Member #358
May 2000

It always uses alpha. My point was that (255, 255, 255, 128) will not be transparent as you might have expected.

Edit: I.e. (128, 128, 128, 128) will be 50% transparent white, not gray as I said above. If you use floating point, then basically if you have a color (r, g, b) and want to add alpha, you need to use: (r * a, g * a, b * a, a). So if you want 50% transparent red, you would use (1, 0, 0, 1) * 0.5 = (0.5, 0, 0, 0.5).

--
"Either help out or stop whining" - Evert

Chris Katko
Member #1,881
January 2002
avatar

Elias said:

I.e. (128, 128, 128, 128) will be 50% transparent white, not gray as I said above. If you use floating point, then basically if you have a color (r, g, b) and want to add alpha, you need to use: (r * a, g * a, b * a, a). So if you want 50% transparent red, you would use (1, 0, 0, 1) * 0.5 = (0.5, 0, 0, 0.5).

I don't think I've used those routines before then. Could you explain why you have to multiply the color values by the alpha to be transparent?

-----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

Elias
Member #358
May 2000

Well, if you look here http://www.liballeg.org/a5docs/refman/graphics.html#al_set_blender

 r = d.r * df.r + s.r * sf.r
 g = d.g * df.g + s.g * sf.g
 b = d.b * df.b + s.b * sf.b
 a = d.a * df.a + s.a * sf.a

Then the default blend mode

al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA)

means you get:

 r = d.r * (1-s.a) + s.r * 1
 g = d.g * (1-s.a) + s.g * 1
 b = d.b * (1-s.a) + s.b * 1
 a = d.a * (1-s.a) + s.a * 1

So if the source color is (1,1,1,0.5) and the dest color is (0,0,0) you get:

 r = 0 * (1-0.5) + 1 * 1 = 1
 g = 0 * (1-0.5) + 1 * 1 = 1
 b = 0 * (1-0.5) + 1 * 1 = 1

So you are drawing fully opaque white. But what you want is to use (0.5,0.5,0.5,0.5) as source color:

 r = 0 * (1-0.5) + 0.5 * 1 = 0.5
 g = 0 * (1-0.5) + 0.5 * 1 = 0.5
 b = 0 * (1-0.5) + 0.5 * 1 = 0.5

This time we were drawing a 50% transparent white pixel onto the black destination pixel correctly.

If you change the blend type to al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA) then the blending itself will do that multiplication for you, but see ex_premul_alpha for why that is a very bad idea with OpenGL/DirectX.

--
"Either help out or stop whining" - Evert

Go to: