I am trying to blend textures which have transparent areas:
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, ...);
glVertexPointer( 2, GL_FLOAT, 0, ... );
glEnable (GL_BLEND);
glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
Unless I add glDisable(GL_DEPTH_TEST), transparent parts of the top textures overwrite everything beneath them (instead of blending). Is there any way to do this without disabling depth? I have tried various blending functions but none of the helped.
Enabling depth test doesn’t actually sort your geometry by depth—in the usual GL_LESS
case, it merely prevents primitives from drawing if they aren’t closer to the viewer than what has previously been drawn. This allows you to draw opaque geometry in whatever order you want and still get the desired result, but correct rendering of blended geometry typically requires everything behind the blended object to have already been rendered.
Here’s what you should be doing to get a mix of opaque and blended geometry to look right:
glDepthMask(GL_FALSE)
.Alternately, if your content is always either fully opaque or fully transparent, you can just enable alpha testing and disable blending, but I’m guessing that’s not what you’re looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With