SDL_image png transparency
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
and the display code:
I don't understand why it doesn't work, the image shows up but is not transparent at all. Thanks for the help!
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!
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
- Alex Diener
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Sprite transparency in OpenGL? | Guest! | 26 | 43,470 |
Feb 17, 2012 09:24 AM Last Post: Skorche |
|
Trouble using SDL_image with XCode 3.2.3 Snow Leopard | code4fun | 1 | 5,891 |
Sep 22, 2010 10:47 PM Last Post: OneSadCookie |
|
OpenGL ES 2.0, 2D Alpha Transparency Artifacts | Macmenace | 3 | 11,010 |
Mar 28, 2010 11:18 PM Last Post: AnotherJake |
|
SDL_image and bmp loading | Duane | 13 | 13,156 |
Dec 21, 2009 09:14 AM Last Post: Skorche |
|
libpng transparency problem | wyrmmage | 3 | 7,536 |
Mar 1, 2007 05:53 PM Last Post: OneSadCookie |