Search code examples
c++openglsdlphotoshopsdl-image

Why do images loaded to texture with IMG_Load in SDL and OpenGL look bluish?


I'm loading a PNG texture with:

void Sprite::setTexture(string f) {
    SDL_Surface *image = IMG_Load(f.c_str());
    if (image == NULL) {
        this->texture = -1;
        return;
    }
    SDL_DisplayFormatAlpha(image);
    unsigned object(0);
    glGenTextures(1, &object);
    glBindTexture(GL_TEXTURE_2D, object);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
    SDL_FreeSurface(image);
    this->texture = object;
}

The only problem is that instead of being like this (seen in Photoshop):

Original box

it is shown like this (seen in the app):

What's wrong? I have tried playing a little bit with tha GL_RGBA and tried to invert it to GL_BGRA or change it to GL_RGBA8, GL_RGBA16... but nothing changed (actually GL_BGRA didn't show anything). I need the alpha channel, even in this little PNG the borders are radius at 4px and so the image angles are transparent.

What am I doing wrong? App box


Solution

  • It definitely looks like your channels are inverted. It's hard to see from here if your alpha is working properly, but some textures are ARGB, maybe you should try that!

    Actually, just to validate your data, you should check your

    SDL_Surface *image
    

    And see what's image->format->Rmask Gmask Bmask and Amask, this way you can be sure what format your surface has.