Get rid of the black pixels?

I have a bitmap that i loaded with SDL and put on the screen with
OpenGL. Now, i cant find anywhere how to make the black pixels 100%
transparent while keeping the rest of them that are not black 100%
opaque.

I have a bitmap that i loaded with SDL and put on the screen with
OpenGL. Now, i cant find anywhere how to make the black pixels 100%
transparent while keeping the rest of them that are not black 100%
opaque.

You have to create an OpenGL texture with an alpha channel (see the glTexImage2D documentation), then enable blending with glEnable(GL_BLEND) and finally set the blending function to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).

Now another issue is if you’re starting from a picture without an alpha channel. Here is what to do :
SDL_Surface* image,*alpha_image;
image=SDL_LoadBMP(…);
SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format,0,0,0));
alpha_image=SDL_CreateRGBSurface(SDL_SWSURFACE,image->w,image->h,32,0xff,0xff00,0xff0000,0xff000000)
SDL_BlitSurface(image,NULL,alpha_image,NULL);
glGenTextures(1,&tex);
glBindTexture(GL_TEXTURE_2D,tex);
glTexParameter(…); // set texture parameters as you wish
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,alpha_image->w,alpha_image->h,0,GL_RGBA,GL_UNSIGNED_BYTE,alpha_image->pixels);
SDL_FreeSurface(image);
SDL_FreeSurface(alpha_image);

Note that if you want to make it big endian-friendly, you should revert the masks from SDL_CreateRGBSurface on big endian architectures.
Stephane

Stephane

I have a bitmap that i loaded with SDL and put on the screen with
OpenGL. Now, i cant find anywhere how to make the black pixels 100%
transparent while keeping the rest of them that are not black 100%
opaque.

OpenGL doesn’t suport colorkeys AFAIK so you’ll have to turn it into a
surface with alpha.–
Gabriel Gambetta
Mystery Studio - http://www.mysterystudio.com
Gabriel on Graphics - http://gabrielongraphics.blogspot.com

I have a bitmap that i loaded with SDL and put on the screen with
OpenGL. Now, i cant find anywhere how to make the black pixels 100%
transparent while keeping the rest of them that are not black 100%
opaque.
OpenGL doesn’t suport colorkeys AFAIK so you’ll have to turn it into a
surface with alpha.

Sorry, I didn’t notice this thread until I saw this message. The follow
is based on code in testgl.c found in the testing directory of the SDL
source code. It converts an SDL surface to an OpenGL texture, but first
adds an alpha channel based on a color key.

I strongly feel that the version of LoadTexture in testgl.c and this
color key version should be added to SDL.

	Bob Pendleton

//----------------------------------------------------------

// Quick utility function for texture creation. It computes
// the smallest power of two that is greater than its
// input. The width and height of a texture must both be a
// power of two. Not the same power of two, just a power of
// two.

static int powerOfTwo(int input)
{
int value = 1;

while ( value < input )
{
value <<= 1;
}
return value;
}

//----------------------------------------------------------

// Create a texture from a surface. Set the alpha according
// to the color key. Pixels that match the color key get an
// alpha of zero while all other pixels get an alpha of
// one.

GLuint loadTextureColorKey(SDL_Surface *surface,
GLfloat *texcoord,
int ckr,
int ckg,
int ckb)
{
GLuint texture;
int w, h;
SDL_Surface *image;
SDL_Rect area;
Uint32 colorkey;

// Use the surface width and height expanded to powers of 2

w = powerOfTwo(surface->w);
h = powerOfTwo(surface->h);
texcoord[0] = 0.0f; // Min X
texcoord[1] = 0.0f; // Min Y
texcoord[2] = (GLfloat)surface->w / w; // Max X
texcoord[3] = (GLfloat)surface->h / h; // Max Y

image = SDL_CreateRGBSurface(
SDL_SWSURFACE,
w, h,
32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN // OpenGL RGBA masks
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);
if (image == NULL)
{
return 0;
}

// Set up so that colorkey pixels become transparent

colorkey = SDL_MapRGBA(image->format, ckr, ckg, ckb, 0);
SDL_FillRect(image, NULL, colorkey);

colorkey = SDL_MapRGBA(surface->format, ckr, ckg, ckb, 0);
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colorkey);

// Copy the surface into the GL texture image
area.x = 0;
area.y = 0;
area.w = surface->w;
area.h = surface->h;
SDL_BlitSurface(surface, &area, image, &area);

// Create an OpenGL texture for the image

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
w, h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixels);

SDL_FreeSurface(image); // No longer needed

return texture;
}On Wed, 2006-01-25 at 18:14 -0300, Gabriel wrote:


Gabriel Gambetta
Mystery Studio - http://www.mysterystudio.com
Gabriel on Graphics - http://gabrielongraphics.blogspot.com


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±-------------------------------------+

I strongly feel that the version of LoadTexture in testgl.c and this
color key version should be added to SDL.

Is there any particular reason you didn’t just use the colorkey information
in the incoming SDL Surface?

-Sam Lantinga, Senior Software Engineer, Blizzard Entertainment

I strongly feel that the version of LoadTexture in testgl.c and this
color key version should be added to SDL.

Is there any particular reason you didn’t just use the colorkey information
in the incoming SDL Surface?

I suspect that I just didn’t think of doing it that way. But, it may
have been that I didn’t feel like grubbing the color information out of
a uint32 in the pixel format structure. I’m not married to any
particular way of implementing the function, I just think we need for an
alpha based and a color based texture loading functions in SDL.

	Bob PendletonOn Tue, 2006-01-31 at 06:55 -0800, Sam Lantinga wrote:

-Sam Lantinga, Senior Software Engineer, Blizzard Entertainment


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


±-------------------------------------+