Advertisement

Transparent pixels in image are rendered as black when using glBlitFramebuffer()

Started by August 09, 2024 02:51 AM
0 comments, last by biell2015 3 months, 1 week ago

If I use this code to draw a image WITH TRANSPARENCY to the screen, the transparent pixels are rendered as black, as you can see here:

GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboId);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
glBlitFramebuffer(0, 0, textureW, textureH-50, 0, 0, textureW, textureH-50, GL_COLOR_BUFFER_BIT, GL_LINEAR);

Setting the red color as the clear color:

glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

This is how I load the texture.

int channels = 0;
        
unsigned char* image = SOIL_load_image(fileName.c_str(), &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO);
if (image == nullptr)
{
   SDL_Log("SOIL failed to load image %s: %s", fileName.c_str(), SOIL_last_result());
   return false;
}
int format = GL_RGB;
if (channels == 4)
{
   format = GL_RGBA;
}
glGenTextures(1, &mTextureID);
glBindTexture(GL_TEXTURE_2D, mTextureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SOIL_free_image_data(image);

The window:

mScreenW = screenW;
mScreenH = screenH;

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);

mWindow = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, mScreenW, mScreenH, SDL_WINDOW_OPENGL);
if (!mWindow)
{
	SDL_Log("Failed to create window: %s", SDL_GetError());
	return false;
}

mContext = SDL_GL_CreateContext(mWindow);

SDL_SetWindowFullscreen(mWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);

Original image, you can't see here I guess, but it transparent, was made using GIMP.

This topic is closed to new replies.

Advertisement