Jump to content
  • Advertisement
Sign in to follow this  
beloxx89

OpenGL Transparency bg in an image.

This topic is 3814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im working with sld and opengl but I cant figure out how can I make the background of an image transparent. I imported a gif library for sdl but it still does not work. glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho (0, XSize, YSize, 0, 0, 1); glMatrixMode (GL_MODELVIEW); glDisable(GL_DEPTH_TEST); glEnable(GL_TEXTURE_2D); SDL_PixelFormat RGBAFormat; RGBAFormat.palette = 0; RGBAFormat.colorkey = 0; RGBAFormat.alpha = 0; RGBAFormat.BitsPerPixel = 32; RGBAFormat.BytesPerPixel = 4; #if SDL_BYTEORDER == SDL_BIG_ENDIAN RGBAFormat.Rmask = 0xFF000000; RGBAFormat.Rshift = 0; RGBAFormat.Rloss = 0; RGBAFormat.Gmask = 0x00FF0000; RGBAFormat.Gshift = 8; RGBAFormat.Gloss = 0; RGBAFormat.Bmask = 0x0000FF00; RGBAFormat.Bshift = 16; RGBAFormat.Bloss = 0; RGBAFormat.Amask = 0x000000FF; RGBAFormat.Ashift = 24; RGBAFormat.Aloss = 0; #else RGBAFormat.Rmask = 0x000000FF; RGBAFormat.Rshift = 24; RGBAFormat.Rloss = 0; RGBAFormat.Gmask = 0x0000FF00; RGBAFormat.Gshift = 16; RGBAFormat.Gloss = 0; RGBAFormat.Bmask = 0x00FF0000; RGBAFormat.Bshift = 8; RGBAFormat.Bloss = 0; RGBAFormat.Amask = 0xFF000000; RGBAFormat.Ashift = 0; RGBAFormat.Aloss = 0; #endif SDL_Surface * orig = NULL; orig = SDL_LoadBMP("you.bmp"); if(orig == NULL){ .... ... Does anyone know what may be the problem?

Share this post


Link to post
Share on other sites
Advertisement
I also tried png. Could anyone help me with that please? Ive been trying to figure it out for more than 5 hours :(

Share this post


Link to post
Share on other sites
Have you setup your blending mode properly?

glEnable( GL_BLEND ) ;
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;

That's the most commonly used method for displaying transparent images.

Share this post


Link to post
Share on other sites
jkleinecke --> yes I have tried that. For the past few hours Ive been trying to browse thorugh pixels and set a color to alpha. Here is what I got so far:


// orig = SDL surface

void *data[orig->w*orig->h*4]; //data = copied pixels (from the surface)
memcpy(data, orig->pixels, orig->w*orig->h*4);


for(int i = 0; i<orig->w*orig->h*4; i++)
{

if(data==(void *)(-65281)){ // -65281 = teh color to be set to trasparent
data=(void *)(0xff); // i thought 0xff represents alpha
}
}
glBindTexture(GL_TEXTURE_2D, textureYou);
glTexImage2D(GL_TEXTURE_2D, 0, 4, orig->w, orig->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glEnable( GL_BLEND ) ;
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ;
SDL_FreeSurface(orig);


As you can see above I tried using 0xff for trasnparency but it does not work...

Please help! Im really desprate....

Share this post


Link to post
Share on other sites
Oh and btw the color is white instead of being transparent :(
By swaping certain pixels like shown above, I managed switch colors but I cant make it alpha....

Share this post


Link to post
Share on other sites
Could anyone please point me to a working example that loads an image with transparent background?

Share this post


Link to post
Share on other sites
void *data[orig->w*orig->h*4]; //data = copied pixels (from the surface)

What does this do? You're allocating a array of pointers of "dynamic" size, but you should use malloc/new for that.

Besides, show your entire code, not the parts you think are failing, such as image loading and texture uploading.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!