SDL_image png transparency

bgoska
Unregistered
 
Post: #1
I've been trying to get transparency to work with SDL_image and pngs. I have a png image with the alpha channel, but when I display it nothing is affected.

Loading code
Code:
    //Init variables
    itsFileName = fileName;
    itsID = 0;
    itsStatus = false;
    SDL_Surface * TextureImage[1];
    int pixelFormat;
    if(fileName.substr(fileName.length() - 3) == "bmp") //if its a bmp
        {
            pixelFormat = GL_BGR_EXT;
        }
        else
        {
            pixelFormat = GL_RGBA;
        }
    
     //Try and load bitmap
         if ( ( TextureImage[0] = IMG_Load( fileName.c_str() ) ) )
         {
        itsStatus = true;

        //Generate texture
        glGenTextures( 1, &itsID );

        //Set our focus to new texture
        glBindTexture( GL_TEXTURE_2D, itsID );
        
        if(gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->w, TextureImage[0]->h, pixelFormat, GL_UNSIGNED_BYTE, TextureImage[0]->pixels))
        {
                 itsStatus = false; //non-zero return = failure to load
            }
        //Set filtering for this texture to Linear, I don't know if this is needed - oh well works with it :)
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
            }
    
    if ( TextureImage[0] )
        SDL_FreeSurface( TextureImage[0] );

and the display code:
Code:
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor3f(1.0f,1.0f,1.0f);
    glBindTexture(GL_TEXTURE_2D, poly->getTextureID() );
    glBegin(GL_QUADS);
        glTexCoord2f( 0.0f, 1.0f ); glVertex2f(  0.0f,  poly->getYSize() );
        glTexCoord2f( 1.0f, 1.0f ); glVertex2f(  poly->getXSize(),  poly->getYSize() );
        glTexCoord2f( 1.0f, 0.0f ); glVertex2f(  poly->getXSize(),  0.0f );
        glTexCoord2f( 0.0f, 0.0f ); glVertex2f(  0.0f,  0.0f );        
    glEnd();
    glDisable(GL_BLEND);

I don't understand why it doesn't work, the image shows up but is not transparent at all. Thanks for the help!
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
I see a problem in your call to gluBuild2DMipmaps. The second parameter should be GL_RGBA. Passing 3 basically tells GL to discard any alpha channel you pass it in the texture image.

- Alex Diener
Quote this message in a reply
bgoska
Unregistered
 
Post: #3
Oh, wow, I can't belive I over looked that. Thanks that fixed all my problems.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Sprite transparency in OpenGL? Guest! 26 29,525 Feb 17, 2012 09:24 AM
Last Post: Skorche
  Trouble using SDL_image with XCode 3.2.3 Snow Leopard code4fun 1 4,325 Sep 22, 2010 10:47 PM
Last Post: OneSadCookie
  OpenGL ES 2.0, 2D Alpha Transparency Artifacts Macmenace 3 7,890 Mar 28, 2010 11:18 PM
Last Post: AnotherJake
  SDL_image and bmp loading Duane 13 8,513 Dec 21, 2009 09:14 AM
Last Post: Skorche
  libpng transparency problem wyrmmage 3 5,314 Mar 1, 2007 05:53 PM
Last Post: OneSadCookie