Christoph
Christoph

Reputation: 2073

Semi-transparency in OpenGL ES 2.0

I'm running into a problem with semi-transparency with OpenGL ES 2.0 on iOS. My scene is rather simple. It consists of a grid of cubes, some of them should appear solid whereas the others should be rendered semi-transparent. I started out with the code below for setting up OpenGL.

glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

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

This renders incorrect transparency for some angles because of the depth-testing and culling. See the two images below

Correct transparency / Culling and depth testing enabled

Incorrect transparency / Culling and depth testing enabled

I tried to disable curling and depth-testing and enabled alpha-testing. The result is correct transparency but no textures (see image below).

//glEnable(GL_CULL_FACE);
//glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LEQUAL);


glAlphaFunc(GL_GREATER, 0.5);
glEnable(GL_ALPHA_TEST);


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

enter image description here

I'm using GLKit to load textures and a GLKBaseEffect to render the scene. Does someone has a hint how to achieve the same result as in the first image with correct transparency for all perspectives? Thank you :)

Upvotes: 1

Views: 985

Answers (1)

JasonD
JasonD

Reputation: 16612

Your main two options are:

  1. Sort all the polygons in your scene, and make sure no polygon intersects any other (because then you can't order them)

  2. Use a sort-independent blending mode instead, such as an additive or subtractive blend.

If you really do just want a grid of cubes, changing the rendering order to be suitable for any viewpoint shouldn't be too tricky, as you just need to traverse the cubes in a different order rather than actually sort anything.

Upvotes: 4

Related Questions