c++copengltexturesimmediate-mode

Immediate mode not drawing alpha texture correctly


As the title says, whenever i enable blending like this:

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

I cannot draw any texture using immediate mode. It is an RGBA texture. I confirmed that image loading and generating works correctly, as when i "downloaded" the pixel from the GPU to debug this, the alpha values seemed to be correct (not all of them were 255, for example.). However, the texture just disappears when drawing it like this with blending enabled:

glColor4ub(255, 255, 255, 0);
_texture->setActive(0);
glBegin(GL_QUADS);
  glTexCoord2f(0.0f, 0.0f);
  glVertex2f(_x, _y);

  glTexCoord2f(1.0f, 0.0f);
  glVertex2f(_x + _width, _y);

  glTexCoord2f(1.0f, 1.0f);
  glVertex2f(_x + _width, _y + _height);

  glTexCoord2f(0.0f, 1.0f);
  glVertex2f(_x, _y + _height);
glEnd();

Where _texture->setActive() simply calls this:

glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, m_ID);

Without blending, i get the following result:

actual_no_blending

But with blending it simply draws nothing (again, alpha values of the texture are confirmed to be correct!):

actual_blending

When it should look something like this:

expected

What is the issue here?

Update

After applying glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);, i now get the expected output:

output_final


Solution

  • I think I found it:

    glColor4ub(255, 255, 255, 0);
    

    Assuming you apply texture using GL_MODULATE the alpha will be mixed with the "primitive" color. If setting alpha of the primitive to 0 then any mix with texture will end up 0 alpha.

    I´m not sure what you want to do so I propose doing one of the following:

    1. Apply texture using GL_REPLACE instead. Call glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE). Texture environment is part of your (active) texture unit config. From https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml

    For OpenGL versions 1.3 and greater, or when the ARB_multitexture extension is supported, glTexEnv controls the texture environment for the current active texture unit, selected by glActiveTexture

    1. Set primitive color to (255, 255, 255, 255)ub.