OrangeMan
OrangeMan

Reputation: 692

Transparency issue with opengl/lwjgl

I am attempting to draw two textures to 3D space that containing transparency. When they do not overlap they work fine:

enter image description here

However when one texture overlaps the other the the transparency means that you can see through the one behind:

enter image description here

I use GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA when initialising blending.

Upvotes: 1

Views: 522

Answers (2)

the swine
the swine

Reputation: 11031

One possibility is to use the discard keyword in the fragment shader, as the alpha test is no longer with us. This has the disadvantage of having aliased edges of objects.

Another possibility is to depth-sort the objects and draw back to front. Obvious disadvantage is having to perform the transformations and the sorting in the first place. This can be sometimes avoided if the order of the objects can be determined statically (when the camera doesn't change much). Another disadvantage is overdrawing of the shaded pixels by something different, therefore throwing away performance.

Finally, you can use alpha-to-coverage, where the antialiassing hardware is employed to take care of the transparency. This doesn't require sorting and makes the edges of the objects smooth. The disadvantage is that this is enabled per rendering context and may not be available everywhere.

Upvotes: 1

SDLeffler
SDLeffler

Reputation: 585

You need to either depth sort or use alpha testing:

glEnable(GL_ALPHA_TEST);
glAlphaTest(GL_GREATER, 0.0f);

which will only draw pixels that have an alpha value of more than 0f. However, this doesn't work for blending transparent pixels. Andon's solution is the one that I use, although I work in 2D and I have to have transparency for smoke effects.

Upvotes: 1

Related Questions