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 ?
Did you set_color_depth(32) before setting video mode?
No, because I use allegro5 and there isn't such a function.
Can't find substitute too
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
Have no idea what to do with al_set_new_display_option and al_set_blender functions.
What values should they reveive ?
You need to multiply the color components with the alpha.
(128,128,128,128) means 50% transparent gray for example.
yes I know how RGBA system works but I don't know how to tell allegro5 to use alpha channel !
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).
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?
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.