1

im rendering png's on simple squares in opengl es 2.0, but when i try and draw something behind an square i have already drawn the transparent area in my top square are rendered the same color as the background.

I am calling these at the start of every render call.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable (GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
genpfault
  • 51,148
  • 11
  • 85
  • 139
user346443
  • 4,672
  • 15
  • 57
  • 80

3 Answers3

5

Your title is essentially the answer to your question!

Generally transparency is done by first rendering all opaque objects in the scene (letting the z-buffer figure out what's visible), then rendering all transparent objects from back to front.

Drew Hall
  • 28,429
  • 12
  • 61
  • 81
  • oh ok, so i need to render the back ones first. Is there no other way around it. – user346443 May 12 '11 at 22:02
  • 1
    @user346443: No there isn't. This is a basic principle of all rasterizers, and OpenGL is defined in terms of rasterizers. Order independent rendering of transparent faces is a very hot research topic, but so far all methods ivented so far implement a back to front sorting algorithm using multiple render targets or similar. – datenwolf May 12 '11 at 22:21
5

Drew Hall gave you a good answer but another option is to set glEnable(GL_ALPHA_TEST) with glAlphaFunc(GL_GREATER, 0.1f). This will prevent transparent pixels (in this case, ones with alpha < 0.1f) from being rendered at all. That way they do not write into the Z buffer and other things can "show through". However, this only works on fully transparent objects. It also has rough edges wherever the 0.1 alpha edge is and this can look bad for distant features where the pixels are large compared to the object.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
5

Figured it out. You can discard in the fragment shader

mediump vec4 basecolor = texture2D(sTexture, TexCoord);

if (basecolor.a == 0.0){
    discard;
}

gl_FragColor = basecolor;
vallentin
  • 23,478
  • 6
  • 59
  • 81
user346443
  • 4,672
  • 15
  • 57
  • 80
  • Yes, this is equivalent to what `GL_ALPHA_TEST` did in the fixed function pipeline, except the test was configured with `glAlphaFunc` – Ben Jackson May 13 '11 at 01:47
  • I found this very useful for z ordering issues when drawing trees. While my trees are sorted back to front there are times when two close trees are so close together that their distance is equal and therefore one gets drawn over the other causing a nasty box to draw. Tossing out low alpha pixels solves that but I still have to sort first because the crude discard can look unpleasant if I don't. – locka Jan 24 '13 at 14:38
  • 2
    This is a better answer, IMO - since most folks are simply looking for simple transparency. Translucency requires other techniques and it's highly misleading how many people tell you you can't do transparency without re-ordering (or something like depth peeling.) –  May 18 '13 at 19:17
  • really important answer!! as already said for many purposes (e.g. foliage textures with alpha channel) simple binary transparency is sufficient and no alpha blending is needed. – Nikole Nov 19 '16 at 04:39