About this Blog

This is my first blog. Ever.

It is simply going to be about my hobby; playing with computer programming. I do not know much about blogging, but I will use this one to learn a bit more about it.

Programming has always been a bit of a passion for me, as from those early days when I first tapped in a sample BASIC program on my old Sinclair Spectrum back in 1986. I have been through many platforms, languages and OS's since, but always carried the hobby with me. I am not particularly good at it; perfection requires a large time investment and continuous practice. I do not have the luxury of the amount of time required to keep the fire burning constantly, so the hobby has inevitably gone through periods of extreme withering. I have, however, finally settled for C++, as the title of this blog implies, and play around with it for some entertainment when ever I can.

This here will serve me as a written record of what I am up to, and hopefully be a reinforcement to my memory every now and then. That is all there is to it.

So, if you read this blog, please don't expect anything snazzy, but be you welcome just the same!

Friday, 16 December 2011

OpenGL / Allegro 5.1 Alpha Transparency Experiment

Very short post this time. I stumbled around and took some shots in the dark to get Allegro 5.1's al_convert_mask_to_alpha() function to work with OpenGL. Strangely, the cookie crumbled correctly. I used a modified version of the code on my previous post, and "doctored" the image of the godesses as such...

It now has a pink background of RGB components (255, 0, 255). That pink area will be turned into an alpha channel so that it can be transparent. Next, in the code, I added the following line into main (pale grey code implies code that was not changed from the previously posted code, slightly modified code is darker grey, and the newly added code is black)...

al_set_new_display_flags(ALLEGRO_OPENGL);
display = al_create_display(SCREEN_X, SCREEN_Y);
tex_bmp = al_load_bitmap("texture2.png");
al_convert_mask_to_alpha(tex_bmp, al_map_rgb(255, 0, 255));
event_queue = al_create_event_queue();
timer = al_create_timer(1.0f/FPS);
ogl_tex = al_get_opengl_texture(tex_bmp);

The added line is taking the loaded bitmap and, using al_convert_mask_to_alpha() function with an al_map_rgb() value of (255, 0, 255), making any part of the image that is this color, technically, transparent.

Now, how to make OpenGL use that channel as transparency? Somewhere, before entering the while() loop of the application, place this snippet of code...

glAlphaFunc(GL_GREATER, 0.5);
glEnable(GL_ALPHA_TEST);


I put it just after the calls to...

al_start_timer(timer); 
setup_3D_camera();


Now, what do they do? Still a bit of a mystery to me, as yet. Here is the page where I got the idea, for future reference...

Let There Be Texture

And here are the respective OpenGL reference pages to that GL function...

glAlphaFunc()

...to scratch my head over in the following days. And as usual, glEnable() is full of useful stuff.

Anyway, here's the result (a bit tacky around the edges as a result of png anti-aliasing), but passable...


That's all, this time...

No comments:

Post a Comment