alpha纹理openGL + SDL2的问题

时间:2016-08-17 23:29:12

标签: c++ opengl textures sdl alpha

我在程序中使用以下代码在PNG中加载具有透明度的纹理:

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

不幸的是,透明部分出现了一些瑕疵。

那是有点远的网格(窗户和门是透明的) mesh

那是近距离的窗口 window

你知道可能导致什么原因吗?

0 个答案:

没有答案