# Modern OpenGL, billboarding, alpha transparency

I'm having some transparency issues when rendering billboards in modern OpenGL.

I've created two billboard sprites, each one with alpha transparency, just for testing.

When I render two sprites without any transparency, it all works as expected:

Whenever I render two sprites with alpha transparency, part of one of the sprites is blocked by the other:

At first I though it had something to do with the render order, so I tried to change the order by simply render the second sprite before the first one, but then the issue is on the first sprite instead:

Blending is enabled by calling glEnable(GL_BLEND); at the start of the application.

I feel like I've tried every glBlendFunc() combination (glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) etc) that exist in OpenGL but nothing helps.

I've also tried disable the depthmask by calling glDepthMask(GL_FALSE);but that doesn't help either.

I am using VBO's (one per sprite) when rendering.

In the vertex- and fragment shader I'm not doing anything more than setting the vertex position, the texture coordinate and the pixel color.

• Are you using a VBO, or are you calling glVertex? – clabe45 Aug 1 '17 at 13:57
• I am using VBO's for rendering. I've edited my question and added that information. – Daniel_1985 Aug 1 '17 at 14:02
• I'm not positive that this will help, but you can also try rendering them in the same VBO. – clabe45 Aug 1 '17 at 14:06

With the help of Bálint, I successfully got the issue fixed.

Bálint suggested using the discard keyword in GLSL to discard pixels that are not visible (i.e, transparent).

So inside my fragment shader, I'm checking the pixel's alpha value and if it's below a certain threshold, the pixel is discarded and therefore not rendered.

vec4 PixelColor = texture2D(Colormap, FragmentUV);

if(PixelColor.a < 0.1)


Result:

• I use threshold 0.5 for my case, it looks better. A different threshold for a different case I assume. – ismailsunni Aug 9 at 14:15

Yes it's the render order, but not in the way you think. The depth buffer only contains 1 dimensional data, it can't represent transparency.

Even if part of the texture is transparent, it will still write in the depth buffer. Because of this when the other plane gets rendered, it'll get obstructed.

It's very hard to solve it in this case, you probably have to slice the plane up into 4 squares and render them separately at which point you could just make the models have an L shape.

• Oh, I see. What if I want to render, for example, grass textures with a lot of transparency (reinerstilesets.de/3dtextures/billboardgrass0002.png). Do I have to cut those up aswell to get the desired result? – Daniel_1985 Aug 1 '17 at 14:15
• @Naith Use glsl's discard for that. It actually discards the fragments before anything gets written to the depth buffer – Bálint Aug 1 '17 at 14:34