Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [AGL]How do you make texture w/ alpha channel with allegro_gl_make_texture_ex()?

This thread is locked; no one can reply to it. rss feed Print
[AGL]How do you make texture w/ alpha channel with allegro_gl_make_texture_ex()?
BrknPhoenix
Member #7,304
June 2006

I asked this question further down in a different topic, however it was kind of appended as an afterthought and wasn't the main issue, so I think it just got lost in the nether.

Using either a PNG or TGA, what is necessary to assure that the alpha channel or transparency is used when the texture is bound?

I read the AllegroGL manual as well as some OpenGL stuff for the final parameter for allegro_gl_make_texture_ex() but none of the combinations of things I've been trying seem to be working. I think right now I'm doing something like this on a PNG.

texSprite = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA | AGL_TEXTURE_FLIP,
 m_sprite, GL_RGBA)

I've also used GL_ALPHA at the end, as well as a combination of the two, and nothing is getting the desired result. After the above code the texture is later blended with GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA.

Bob
Free Market Evangelist
September 2000
avatar

Does your loaded bitmap contain the alpha channel you expect? The call to AGL your pasted looks correct.

--
- Bob
[ -- All my signature links are 404 -- ]

Matt Smith
Member #783
November 2000

Attach the TGA file so we can check if it does have an alpha channel saved. Some paint progs will strip it on saving, and I for one am not sure under which circumstances load_tga() will strip it on loading.

BrknPhoenix
Member #7,304
June 2006

Alright, I'll post it when I get off work... And am I the only one who only sees a horizontal scrolling bar in my initial post in this topic and no code?

Here's the PNG I've been using

[img]http://img.photobucket.com/albums/v160/BrknPhoenix/greencursor.png[/img]

I'm not even going to bother with the TGA version because I'm pretty confident already it wasn't saving the alpha in photoshop. I've read on numerous forums that Photoshop has crap Targa support, and on Adobe's site it said PS7 required a patch for it to properly save the Targa alpha channel. And I only have Photoshop Elements 4.0 :(

And I just posted this PNG image on a forum with a non-white background and the transparency is good. So I'm guessing that something with AllegroPNG isn't getting the transparency.

gnolam
Member #2,030
March 2002
avatar

Are you loading the bitmap as 32 bit?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Erkle
Member #3,493
May 2003
avatar

Quote:

Are you loading the bitmap as 32 bit?

ie. Are you doing this?

int old_depth = get_color_depth();
BITMAP* texSprite = load_tga("blah.tga", NULL);
set_color_depth(old_depth);

texSprite = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA | AGL_TEXTURE_FLIP
    , m_sprite, GL_RGBA);

This makes allegro load the bitmap as 32-bit. In my opinion the API should make this native somehow (ie. load_tga_ex(32, "blah.tga", NULL)) but it would require API breakage. Maybe in Allegro 5.0.

If the writing above has offended you, you've read it wrong.....fool.
And if you read that wrong it means 'All of the above is opinion and is only to be treated as such.'

Fladimir da Gorf
Member #1,565
October 2001
avatar

Did you remember to enable blending in OpenGL? ;)

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

BrknPhoenix
Member #7,304
June 2006

Yes, blending is enabled. And no, I'm not saving as 32 bit in targa :p That's part of the problem. I haven't been using TGA images because I can't find anything that saves 32 bit Targa's. My photoshop elements 4.0 apparently doesn't save TGA alpha channels at all, and GIMP keeps saving as 24 bit even though the targa has an alpha channel.

And I'm not loading the png in 32 bit because it's not a 32 bit png...

I really don't know what to do here :\

Milan Mimica
Member #3,877
September 2003
avatar

GIMP saves the alpha channel in TGA fine.

BrknPhoenix
Member #7,304
June 2006

Hm, I don't suppose someone could upload a Targa that they know for a fact is 32-bit with transparency so I could test it with my program? So I could know whether it's the image or the code that is my problem?

Milan Mimica
Member #3,877
September 2003
avatar

BrknPhoenix
Member #7,304
June 2006

edit: Solved! The problem was that GL_DEPTH_TEST was enabled while this was being done, so objects were failing the depth test instead of being blended transparently.
---
edit: Interesting update... The texture appears transparent over the mouse coordinates I drew with Allegro's generic text functions. But not over a white quad I placed in the middle of the screen!
---
It would appear the problem is my code, as the transparency doesn't show with that picture either :( I don't know what the problem is, since everyone says I'm doing it right...

Well, let me post the whole of what is going on in my code, and maybe someone will spot something. Note that this code is rather poorly put together, mostly because it's the result of me deleting and re-typing over and over again to try and make things work.

As for what actually happens... The image will display, but the parts that should be transparent appear as black instead.

Setting up the screen initially...

        glMatrixMode(GL_PROJECTION);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_DEPTH_TEST);   
        glEnable(GL_ALPHA_TEST);
        allegro_gl_use_alpha_channel(TRUE);     
        glLoadIdentity();
       gluPerspective(45.0f, (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, 1.0f, 1000.0f);         
        glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);           
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

Loading the texture...

    int old_c_depth = get_color_depth();
    set_color_depth(32);
    m_sprite = load_tga(filename, NULL);
    texSprite = allegro_gl_make_texture_ex(AGL_TEXTURE_HAS_ALPHA | AGL_TEXTURE_FLIP, m_sprite, GL_RGBA);    
    set_color_depth(old_c_depth);

Setting up the screen for 2D drawing for the mouse cursor...

1 glMatrixMode(GL_PROJECTION);
2 glPushMatrix();
3 glLoadIdentity();
4 gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
5 glMatrixMode(GL_MODELVIEW);
6 
7 if (Mouse->Visible())
8 {
9 Draw->Blit(Mouse->TextureSprite(),
10 Mouse->X(), Mouse->Y(),
11 Mouse->X() + Mouse->CursorWidth(),
12 Mouse->Y() + Mouse->CursorHeight());
13 }
14 
15 glMatrixMode(GL_PROJECTION);
16 glPopMatrix();
17 glMatrixMode(GL_MODELVIEW);

The code that actually draws it...

1void objDraw::Blit(GLuint sprite, int x1, int y1, int x2, int y2)
2{
3 glEnable(GL_TEXTURE_2D);
4 glEnable(GL_BLEND);
5 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6
7 glBindTexture(GL_TEXTURE_2D, sprite);
8 
9 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
10 
11 glBegin(GL_QUADS);
12 glTexCoord3f(0, 0, 0); glVertex3f(x1, y2, 0);
13 glTexCoord3f(1, 0, 0); glVertex3f(x2, y2, 0);
14 glTexCoord3f(1, 1, 0); glVertex3f(x2, y1, 0);
15 glTexCoord3f(0, 1, 0); glVertex3f(x1, y1, 0);
16 glEnd();
17}

Go to: