0

I load a texture in a PNG with transparency in my program with this code :

// Loading the image in a SDL_Surface
SDL_Surface* imageSDL = IMG_Load(m_imageFile.c_str());

if (imageSDL == 0)
{
    std::cout << "Error while loading texture \"" << m_imageFile.c_str() << "\" : "
    << IMG_GetError() << std::endl;
    return false;
}

SDL_Surface* invertedImage = invertPixels(imageSDL);
SDL_FreeSurface(imageSDL);

// Destruction of an eventual old texture
if(glIsTexture(m_id) == GL_TRUE)
    glDeleteTextures(1, &m_id);

// ID Generation
glGenTextures(1, &m_id);

// Locking
glBindTexture(GL_TEXTURE_2D, m_id);

// Assessing format and internal format
GLenum internalFormat(0);
GLenum format(0);

// Assessing number of components (alpha or no alpha)
if (invertedImage->format->BytesPerPixel == 3)
{
    internalFormat = GL_RGB;

    if (invertedImage->format->Rmask == 0xff)
        format = GL_RGB;

    else
        format = GL_BGR;
}
else if (invertedImage->format->BytesPerPixel == 4)
{
    internalFormat = GL_RGBA;

    if (invertedImage->format->Rmask == 0xff)
        format = GL_RGBA;

    else
        format = GL_BGRA;
}
else
{
    std::cout << "Error, unknown format of image \"" << m_imageFile.c_str() << "\" : "
    << SDL_GetError() << std::endl;
    return false;
}

// pixels copying
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, invertedImage->w, invertedImage->h, 0, format, GL_UNSIGNED_BYTE, invertedImage->pixels);

// Filters application
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// Unlocking
glBindTexture(GL_TEXTURE_2D, 0);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

SDL_FreeSurface(invertedImage);

unfortunately, the transparent parts appear with some artifacts.

that's the mesh for a bit far (the window and the door are transparent) mesh

that's the window from up close window

do you know what might cause that?

  • Not necessarily related, but your code comments about "locking" and "unlocking" by modifying the texture binding brought this to mind. Binding **0** doesn't really disable anything, there is an actual texture in OpenGL with name **0**. That texture is not immutable -- commands that modify texture data continue to function when **0** is bound and you can even draw using that texture, it's not a good idea because so many people assume that texture **0** has some special meaning that it usually has an inconsistent state. – Andon M. Coleman Aug 18 '16 at 01:02
  • Can you provide a link to the texture you are using? Also, what kind of mesh are you rendering the texture onto? – Ben Damer Aug 19 '16 at 16:38
  • Remember to render back to front to properly compose colors with alpha blending. – Jonny D Sep 23 '16 at 23:24

0 Answers0