views:

37

answers:

1

Hi,

I am trying to make an OpenGLES 2.0 cube application. The idea was to have a texture (with an alpha 75%) applied to all 6 faces of the cube. This would mean that even if I rotate the cube i would be able to see all 6 faces at any given frame. Now I have enabled depth test(my app needs this!!) and blending. The Depth func is LEQUAL and blend func is SRC_ALPHA, ONE_MINUS_SRC_ALPHA.

Now, the issue is that at some cube faces don't show the underlying faces. I am not able to understand this because the logic works fine with the other cube faces. Just for the record, I have disabled CULL_FACE.

Thanks in advance.

Regards,

Puzzler

A: 

Faces are drawn in order they are defined. Now as cube is rotated their draw order is not changed. Some faces are drawn close to camera first. Then some face is drawn after that, but Z-test says that there already is a pixel closer to the camera so no drawing needs to be done.

Problem here is that OpenGL|ES 2.0 does not sort faces when they are rendered. Handling transparency and depth correctly remains as a real challenge in 3D programming, but for easy case like this, you can just sort the faces back to front. Drawing back to front is called "Painter's algorithm" and sorting is called "depth sorting" or "z-sorting".

Simple, definetly not best and surely inefficient way might work like this:

  1. Calculate center vertex of the face by calculating average of three vertices
  2. Project them to screen space by transforming them through camera (view+projection) and possible model-matrices
  3. Sort their Z-values by some sort algorithm
  4. Build vertex buffer with vertices in this order or just rewrite index buffer in new order (less stuff to move around if vertices are not changed)
  5. Render cube as usual

There are better ways to handle this problem and there all kinds of advanced optimizations out there. But I didn't found any tutorial to cover simple case like this. Perhaps I just didn't found right keywords.

Virne
Thanks for the reply.Will try this