Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » AllegroGL and translucent textures

Credits go to ImLeftFooted and Jakub Wasilewski for helping out!
This thread is locked; no one can reply to it. rss feed Print
AllegroGL and translucent textures
samwyse
Member #7,610
August 2006
avatar

I can't get the alpha-channel to work on textures. I've gone so far as to dump the bitmap that I'm using and, yes, the information is there. I'm suspecting that there's a flag somewhere that I'm forgetting to set, but as this is the first time I've tried doing this, I'm not sure what it would be.

Anyway, here's the code I'm using to create the texture (with embedded logging):

1GLuint LoadTexture(char * bmp_name, int translucent = 0)
2{
3 GLuint texture_handle;
4 
5 // Load the texture
6 set_color_depth(32);
7 BITMAP* bmp = load_bitmap(bmp_name, 0);
8 cerr << "loading " << bmp_name << endl;
9 if (translucent) {
10 cerr << "setting alpha" << endl;
11 set_write_alpha_blender();
12 drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
13 for (int y=0; y<bmp->h; y++)
14 for (int x=0; x<bmp->w; x++)
15 putpixel(bmp, x, y, 0x77);
16 drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);
17 }
18 for (int y=0; y<bmp->h; y+=0x55)
19 for (int x=0; x<bmp->w; x+=0x55)
20 cerr << hex << x << "," << y << ": " << getpixel(bmp,x,y) << endl;
21 texture_handle = allegro_gl_make_texture_ex(
22 AGL_TEXTURE_HAS_ALPHA, bmp, -1);
23 glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
24 glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
25 destroy_bitmap(bmp);
26 return texture_handle;
27}

And here's what I get when I run it:

1loading clouds2.tga
20,0: c88365
355,0: db9c81
40,55: aa5536
555,55: d59377
6loading drock03048477.tga
70,0: 90b6b6
855,0: 94baba
90,55: 90b6b6
1055,55: 789e9e
11loading water038.tga
12setting alpha
130,0: 77ff795f
1455,0: 77e95d4c
150,55: 77f6684f
1655,55: 77ff7a5e

I've got a half-meg screen dump if you really want to see it, but trust me when I say that you can't see the river bottom through the water. But that does bring up a second small issue. I've noticed that save_bitmap is inverting my screenshots. I presume that this is due to Allegro putting (0,0) in the upper left and OpenGL putting it in the lower left. Is there any way to fix this, or should I resign myself to manually flipping things?

Jakub Wasilewski
Member #3,653
June 2003
avatar

Just to be sure - you do call glBlendFunc and glEnable(GL_BLEND) prior to drawing anything with those textures, right?

You can also try using GL_RGBA instead of the -1 with make_texture_ex to force a pixel format that contains alpha.

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

ImLeftFooted
Member #3,935
October 2003
avatar

Heres my init code

1 if(0 != install_allegro_gl())
2 throw "Unable to init Allegro GL.";
3
4 allegro_gl_clear_settings();
5 allegro_gl_set(AGL_DOUBLEBUFFER, 1);
6 allegro_gl_set(AGL_RENDERMETHOD, 1);
7 allegro_gl_set(AGL_COLOR_DEPTH, settings.colorDepth);
8 allegro_gl_set(AGL_RENDERMETHOD, 1);
9 allegro_gl_set(AGL_Z_DEPTH, 0);
10 allegro_gl_set(AGL_SAMPLE_BUFFERS, 0);
11 allegro_gl_set(AGL_SAMPLES, 0);
12
13 allegro_gl_set(AGL_DONTCARE, AGL_Z_DEPTH | AGL_SAMPLE_BUFFERS | AGL_SAMPLES);
14 allegro_gl_set(AGL_SUGGEST, AGL_COLOR_DEPTH | AGL_RENDERMETHOD);
15 allegro_gl_set(AGL_REQUIRE, AGL_DOUBLEBUFFER);
16
17 set_color_depth(settings.colorDepth);
18
19 int mode = GFX_OPENGL;
20
21 if((curProfile && curProfile->fullscreen) || (!curProfile && settings.fullscreen)) {
22
23 allegro_gl_set(AGL_FULLSCREEN, 1);
24 allegro_gl_set(AGL_WINDOWED, 0);
25 allegro_gl_set(AGL_SUGGEST, AGL_FULLSCREEN | AGL_WINDOWED);
26 mode=GFX_OPENGL_FULLSCREEN;
27 }
28 else {
29
30 allegro_gl_set(AGL_FULLSCREEN, 0);
31 allegro_gl_set(AGL_WINDOWED, 1);
32 allegro_gl_set(AGL_SUGGEST, AGL_FULLSCREEN | AGL_WINDOWED);
33 mode=GFX_OPENGL_WINDOWED;
34 }
35
36 if(0 != set_gfx_mode(mode, settings.screenWidth, settings.screenHeight, 0, 0))
37 throw "Unable to set OpenGL graphics mode.";
38
39 switch(allegro_gl_get(AGL_COLOR_DEPTH)) {
40
41 case 16: set_color_conversion(COLORCONV_32_TO_16);
42 break;
43 case 24: set_color_conversion(COLORCONV_32_TO_24);
44 break;
45 }
46
47 initAllegro();
48
49 allegro_gl_begin();
50 glMatrixMode(GL_PROJECTION);
51 glLoadIdentity();
52 glOrtho(0.0, 640.0, 480.0, 0.0, -1.0, 1.0);
53 glMatrixMode(GL_MODELVIEW);
54 glLoadIdentity();
55 glClearColor(0, 0, 0, 1);
56 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
57 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
58 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
59 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
60 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
61 glDisable(GL_DEPTH_TEST);
62 allegro_gl_use_alpha_channel(1);
63 allegro_gl_set_texture_format(GL_RGBA8);
64 glEnable(GL_BLEND);
65 glEnable(GL_TEXTURE_2D);
66 allegro_gl_end();

For loading textures i just do this:

texture = allegro_gl_make_texture(src);

Alpha channel works perfect for me.

How are you drawing the texture to the screen?

samwyse
Member #7,610
August 2006
avatar

Jakub Wasilewski said:

Just to be sure - you do call glBlendFunc and glEnable(GL_BLEND) prior to drawing anything with those textures, right?

D'oh! :-[ Note to self: In the future, I must make sure that I haven't commented out any code before posting questions to the forum.

Dustin Dettmer said:

Heres my init code

Wow!!! That's some heavy duty code. Thanks for sharing, I think I'll spend the rest of the week-end studying it.

Jakub Wasilewski
Member #3,653
June 2003
avatar

Quote:

Note to self: In the future, I must make sure that I haven't commented out any code before posting questions to the forum.

Nothing to be ashamed of, I just mentioned it because I once made the same error (GL_BLEND disabled) and then wondered for an hour what's wrong.

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

ImLeftFooted
Member #3,935
October 2003
avatar

If anything doesnt make sense you can ask me about it.

Heres how i draw.

1int texture = allegro_gl_make_texture(src);
2 
3while(!quit) {
4 
5 int x = 0;
6 int y = 0;
7 int w = src->w;
8 int h = src->h;
9 
10 glBindTexture(GL_TEXTURE_2D, texture);
11
12 glBegin(GL_QUADS);
13 glTexCoord2f(0.0f, 1.0f);
14 glVertex2i(x, y);
15 glTexCoord2f(1.0f, 1.0f);
16 glVertex2i(x + w, y);
17 glTexCoord2f(1.0f, 0.0f);
18 glVertex2i(x + w, y + h);
19 glTexCoord2f(0.0f, 0.0f);
20 glVertex2i(x, y + h);
21 glEnd();
22 
23 allegro_gl_flip();
24}

Go to: