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.