Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL texture with transparency (alpha)

I'm trying to render a texture with part opaque color and other part with transparency.

This is my draw function for the object:

void drawHighGrass(){
glDisable(GL_LIGHTING);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor4f(1.0, 1.0, 1.0, 1.0);

glDisable(GL_DEPTH_TEST);

glDepthMask(GL_FALSE);

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



glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texturas[HIGH_GRASS]);

glPushMatrix();
//glTranslatef(1000, 0, 1000);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(100, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(100, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 0);
glEnd();

glPopMatrix();

glDisable(GL_TEXTURE_2D);

glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);

}

The problem is that in the transparent part it's showing solid white. I can make the texture transparent by using glColor4f(1.0, 1.0, 1.0, 0.5) but that's not what I want because it makes the entire texture transparent and not only the transparent part.

I've checked, my texture files is a PNG with transparency.

like image 422
andrepcg Avatar asked Aug 26 '26 07:08

andrepcg


1 Answers

Restating the solution here so others can find it easily.

Your rendering code seems to be correct, so what seems to have been the problem was the texture loading code. When loading a texture, you must be sure that you are passing in the correct flags for the internal texture pixel format (GL_RGBA8, GL_RGBA16, etc.) as well as the source image pixel format (GL_RGBA or GL_BGRA, etc.).

like image 83
Maple Avatar answered Aug 27 '26 19:08

Maple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!