iDevGames Forums
SDL_image png transparency - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: SDL_image png transparency (/thread-5977.html)



SDL_image png transparency - bgoska - Jan 11, 2005 09:44 AM

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!


SDL_image png transparency - ThemsAllTook - Jan 11, 2005 10:22 AM

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


SDL_image png transparency - bgoska - Jan 11, 2005 12:59 PM

Oh, wow, I can't belive I over looked that. Thanks that fixed all my problems.