Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 18 March 2013 - 04:06 PM

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.


Edited by GDsnakes, 18 March 2013 - 04:07 PM.


Sponsor:

#2 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 21 March 2013 - 06:10 AM

Anyone?



#3 Kylotan   Moderators   -  Reputation: 3338

Like
1Likes
Like

Posted 21 March 2013 - 06:29 AM

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);
 


#4 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 22 March 2013 - 07:32 AM

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?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS