Skip to main content
GameDev.net gamedev.net
🔒 Locked

OpenGL Texture Color Key

Started by HandCraftedRadio Dec 12, 2008 at 9:33 PM 6 replies 12.2k views
Original Post
HandCraftedRadio
HandCraftedRadio
I'm in the process of making a 2D game engine in openGL and I ran into a little snag. I can't seem to figure out how to pick a color key so a color in the loaded texture will be completely transparent. I have already tried a number of things and I can't seem to get it working properly. I'm using SDL to load the image files. I'm also using PNG files and I realize that they have an alpha layer but I would really like to get the color keying working for a background color if that is possible. Thanks.
deadstar
deadstar
You need to enable GL_BLEND, set up the necessary blend function with glBlendFunc() and apply the colour key with glColor3f/3ub/3i/whatever().
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
HandCraftedRadio
HandCraftedRadio
Yeah I got that you need to enable blending and use glBlendFunc() but I'm not sure how to set it up so it chooses just the one color to be blended.
HuntsMan
HuntsMan
OpenGL doesn't support color keying directly, you can emulate it with a GLSL shader, but it's preferred to use alpha blending, a 1 bit alpha channel should do the job.
HandCraftedRadio
HandCraftedRadio
Ok well since OpenGL doesn't support color keying would there be any way to convert the specific pixels in the image to the alpha channel using SDL before loading it as an OpenGL texture?
Eskapade
Eskapade
If you don't want to use a shader, you can load the image into an SDL_Surface, then loop through each pixel and set the alpha color if the pixel's color is the color key, then load the image into GL.
HandCraftedRadio
HandCraftedRadio
That's what I was thinking, but I can't figure out how to change the alpha value for a specific pixel in the surface.
HandCraftedRadio
HandCraftedRadio
I figured it out. In case anybody is wondering, here is how I did it:


surf = IMG_Load(filename);     SDL_Color color;  for (int y = 0; y < surf->h; y++)  {   for (int x = 0; x < surf->w; x++)   {     unsigned int pix = ((unsigned int*)surf->pixels)[y*(surf->pitch/sizeof(unsigned int)) + x];    color =  translate_color(pix);    if (color.r == 255 && color.b == 255)        {((unsigned int*)surf->pixels)[y*(surf->pitch/sizeof(unsigned int)) + x] = SDL_MapRGBA(surf->format, 0, 0, 0,0);}   }  }



Converting Uint32 to SDL_Color code:
SDL_Color translate_color(Uint32 int_color)                           {    #if SDL_BYTEORDER == SDL_BIG_ENDIAN        SDL_Color color={(int_color & 0x00ff0000)/0x10000,(int_color &         0x0000ff00)/0x100,(int_color & 0x000000ff),0};    #else        SDL_Color color={(int_color & 0x000000ff),(int_color &0x0000ff00)/0x100,(int_color & 0x00ff0000)/0x10000,0};        #endif    return color;}





Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.