glBlitFramebuffer will not draw transparent texture

Hello,

I’m trying to draw a transparent image to screen using glBlitFramebuffer. I’ve got it drawing correctly, except instead of being transparent, the image has a white background.

The image is created programmatically using CImage and Win32 drawing functions. When I save the image to file as a PNG, it looks correct and properly has transparency, so I’m pretty sure the image itself is not the issue.

Can anyone help?

My code so far:

//Random things I tried
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
glTexEnvf(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_MODULATE);

//Get the bitmap (cImage is created elsewhere)
CBitmap* pBitmap = GetImgBitmap(&cImage);
BITMAP bm;
pBitmap->GetBitmap(&bm);

int width = bm.bmWidth;
int height = bm.bmHeight;

GLdouble world_coords[3];
GLdouble projection[16];
GLdouble modelview[16];
GLdouble screen_coords[3];
GLint viewport[4];

//Sets the vector where I want the label
center.GetVector(world_coords);

glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetIntegerv( GL_VIEWPORT, viewport );

world_coords[0] /= 1000;
world_coords[1] /= 1000;
world_coords[2] /= 1000;

gluProject(world_coords[0], world_coords[1], world_coords[2], modelview, projection, viewport, &screen_coords[0], &screen_coords[1], &screen_coords[2]);

int pos_x = screen_coords[0];
int pos_y = screen_coords[1];

//https://learnopengl.com/Getting-started/Textures
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glPixelStorei(GL_PACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glBindTexture(GL_TEXTURE_2D, texture);			//Connection to texture mapping

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm.bmWidth, bm.bmHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bm.bmBits);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//https://stackoverflow.com/questions/24262264/drawing-a-2d-texture-in-opengl/24266568
GLuint readFboId = 0;
glGenFramebuffers(1, &readFboId);
glBindFramebuffer(GL_READ_FRAMEBUFFER, readFboId);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

int dest_x0 = pos_x;
int dest_x1 = pos_x + width;
int dest_y0 = pos_y;
int dest_y1 = pos_y + height;

glBlitFramebuffer(0, 0, width, height, dest_x0, dest_y0, dest_x1, dest_y1, GL_COLOR_BUFFER_BIT, GL_LINEAR);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &readFboId);

glBlitFramebuffer doesn’t apply rendering operations such as blending; it just copies the values. If the source and destination framebuffers have an alpha channel, the alpha values will be copied. They won’t affect the colour.

Specifically (§18.3.1 of the OpenGL 4.6 specification):

If you want the source image to be blended with the framebuffer contents, you need to copy it to a texture and draw a textured quad (triangle pair).