I am trying to load a texture with RGBA values, but alpha values โโjust make the texture more white and not adjust the transparency. I heard about this issue with 3D scenes, but I just use OpenGL for 2D. Anyway, can I fix it?
I initialize OpenGL with
glViewport(0, 0, winWidth, winHeight); glDisable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); glClearColor(0, 0, 0, 0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, winWidth, 0, winHeight); // set origin to bottom left corner glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glColor3f(1, 1, 1);
Screenshot:
This blurry bitmap should be translucent. Black bits should be completely transparent. As you can see, there is an image that is not displayed.
The code for creating such a texture is quite long, so I will describe what I did. This is an array of 40 * 30 * 4 of type unsigned char. Every fourth char is set to 128 (should it be 50% transparent, right?).
Then I pass it to this function, loads the data into the texture:
void Texture::Load(unsigned char* data, GLenum format) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, _texID); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _w, _h, format, GL_UNSIGNED_BYTE, data); glDisable(GL_TEXTURE_2D); }
And ... I think I just found a problem. Initialized a full-sized texture with this code:
glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, _texID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glDisable(GL_TEXTURE_2D);
But I think glTexImage2D should also be GL_RGBA? Can't I use two different internal formats? Or at least not from different sizes (3 bytes versus 4 bytes)? GL_BGR works fine even when it is initialized as follows ...