I need help with SDL/OPENGL Color Keying.....

Started by
2 comments, last by MylesEvans 11 years ago

I have an image that I load in SDL. My goal is to load the image in SDL, Color Key, and then send to Opengl to become texture.

I've read some tutorials and the following code is what I have come up with.


int loadImage()
{
    SDL_Surface *surface;
    
    if (surface = IMG_Load("AvatarR.jpg"))
    {
        nofcolors = surface->format->BytesPerPixel;
    
        if(nofcolors == 4)
        {
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGBA;
            else
                texture_format = GL_BGRA;
        }
        else if (nofcolors == 3) //no alpha channel
        {
            // no alpha channel = no transparency = use color key
            Uint32 colorkey = SDL_MapRGB(surface, 255, 255, 255);
            SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colorkey);
            
            // now convert image to 32 bits
            SDL_Surface *temp = SDL_DisplayFormatAlpha(surface);
            
            // release previous surface
            SDL_FreeSurface(surface);
            
            // use the new converted one
            surface = temp;
            
            // reassign nofcolors because image format changed
            nofcolors = surface->format->BytesPerPixel;
            
            if(surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGB;
            else
                texture_format = GL_BGR;
        }
 
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glPixelStorei(GL_UNPACK_ALIGNMENT,4);
 
        glTexImage2D(GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
    }
    else
    {
        return -1;
    }
 
    if (surface)
        SDL_FreeSurface(surface);
 
    return 1;

It doesn't work. The background color is white and I want it to be gone.

If anyone has any succesful methods in C++ they would like to share I'd greatly appreciate it.

Advertisement

Anyone?

A colour key only works for blitting routines, eg. SDL rendering. It won't have any effect on OpenGL textures. So merely converting the format to 32 bits won't help. You need to create a new surface in 32 bits, then perform the SDL blit from the old surface to the new surface, which will copy everything except the colour-keyed values to the new surface. Then the new one should be usable as a texture.

So basically, you need to replace this line:


SDL_Surface *temp = SDL_DisplayFormatAlpha(surface);

with something like this:


SDL_Surface* temp = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
SDL_BlitSurface(surface, NULL, temp, NULL);
 

I'm having a weird problem, whenever I set Uint32 colorkey = SDL_MapRGB(surface, 255, 255, 255); It turns every geometric object I have draw using Opengl black.

If I set Uint32 colorkey = SDL_MapRGB(surface, 255, 255, 254); and RGB aren't all 255 then its fine, but that a problem since I'm trying to remove a white background. So why would Uint32 colorkey = SDL_MapRGB(surface, 255, 255, 255); be making all of my previously drawn shapes black?

This topic is closed to new replies.

Advertisement