[gameprogrammer] Whaley's OpenGL question
- From: Bob Pendleton <bob@xxxxxxxxxxxxx>
- To: Gameprogrammer Mailing List <gameprogrammer@xxxxxxxxxxxxx>
- Date: Wed, 28 Apr 2004 07:24:04 -0500
On Wed, 2004-04-28 at 00:48, Alexander Whaley wrote:
> By the way, I have a small OpenGL question:
> Is there an easy way to set a colour to be transparent when loading a
> bitmap. I mean, if I have a set of pictures and say that magenta
> indicates a transparent colour, can I set it as a default somewhere or
> must I convert each "magenta pixel" as I load the bitmap into memory to
> become a transparent pixel. Something in the lines of:
> "set_transparent_color(RGB)"??
Run through the image and set the alpha for each pixel with your
transparent color so that the alpha makes it transparent. Here is a
block of code that does that using SDL to load the image and to make the
colorkey pixels transparent.
//----------------------------------------------------------
// 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;
}
--
+--------------------------------------+
+ Bob Pendleton: writer and programmer +
+ email: Bob@xxxxxxxxxxxxx +
+ blog: www.Stonewolf.net +
+ web: www.GameProgrammer.com +
+--------------------------------------+
- References:
- [gameprogrammer] Re: PC game Outsourcing
- From: plm
- [gameprogrammer] Re: PC game Outsourcing
- From: Kevin Jenkins
- [gameprogrammer] Re: PC game Outsourcing
- From: Alexander Whaley
Other related posts:
- » [gameprogrammer] Whaley's OpenGL question
- [gameprogrammer] Re: PC game Outsourcing
- From: plm
- [gameprogrammer] Re: PC game Outsourcing
- From: Kevin Jenkins
- [gameprogrammer] Re: PC game Outsourcing
- From: Alexander Whaley