Transparency bg in an image.

Started by
7 comments, last by jkleinecke 15 years, 11 months ago
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?
Advertisement
I also tried png. Could anyone help me with that please? Ive been trying to figure it out for more than 5 hours :(
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.
-----------------------------------Indium Studios, Inc.
dp
-----------------------------------Indium Studios, Inc.
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....
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....
Could anyone please point me to a working example that loads an image with transparent background?
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.
Run it using GLintercept to verify that your texture was uploaded properly.
-----------------------------------Indium Studios, Inc.

This topic is closed to new replies.

Advertisement