This does it, Re: [SDL] Get rid of the black pixels?

Bob Pendleton bob at pendleton.com
Wed Jan 25 12:57:23 PST 2006


On Wed, 2006-01-25 at 18:14 -0300, Gabriel wrote:
> > 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;
}




> 
> -- 
> 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
-- 
+--------------------------------------+
+ Bob Pendleton: writer and programmer +
+ email: Bob at Pendleton.com             +
+ web: www.GameProgrammer.com          +
+ www.Wise2Food.com                    +
+ nutrient info on 7,000+ common foods +
+--------------------------------------+





More information about the SDL mailing list