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

半世苍凉 提交于 2019-12-23 17:09:13

问题


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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/13666196/why-do-images-loaded-to-texture-with-img-load-in-sdl-and-opengl-look-bluish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!