5
votes

I'm unable to get the depth buffer working correctly on Android OpenGL ES 2.0. Regardless of what I do, the objects are always render in the order provided and completely ignore the depth buffer.

From what I've read, the default setup should have a depth buffer, but I've nonetheless tried every function I could think of to get it working:

//setup
setEGLContextClientVersion(2);
setEGLConfigChooser( true );

GLES20.glEnable( GLES20.GL_DEPTH_TEST );
GLES20.glDepthFunc( GLES20.GL_LEQUAL );
GLES20.glDepthMask( true );

//render
GLES20.glClearColor(0.8f, 0.5f, 0.8f, 1.0f);
GLES20.glClearDepthf(1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

I am placing objects at 0.5, 0.0, -0.5 in the z-axis. I can verify that this works since if I use a perspective view it will correctly alter the sizes of the objects (distant ones smaller) -- my vertex shader does nothing but apply the view matrix. If I'm correct I can't alter the depth in the fragment shader at all in ES, so that can't be wrong.

I'm stumped. I really have no idea what else I could check. Every search I do (in the web, or here) gives answers that don't seem to solve the problem (though it indicates people have had the problem for other reasons). Most examples in ES 2.0 don't actually have enough objects to test the depth buffer, so I can't be positive the sample code is correct either.


To comment: My "setup" section is called just after a call to "super" inside my derived GLSurfaceView, prior to setting the renderer. The "render" part is called first inside my renderes "onDrawFrame".

4
Are you calling GLES20 methods right after creating GLSurfaceView?harism
I clarified where I call these functions.edA-qa mort-ora-y
In that case you could try moving all GLES20 calls within rendering code.harism
I moved the three setup "gl" functions to onSurfaceCreated and it works. Thank you. Strange that no error/warning was printed in the logs.edA-qa mort-ora-y
You should answer you own question if you figured it out.zezba9000

4 Answers

13
votes

Have you specified the buffer depth? This might be the solution to your problem.

myGlSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
11
votes

harism's comment was the correct answer. The below three functions can only be done once you have a rendering context. I put them in the onSurfaceCreated method and it works. You can also put them in the onDraw method if you change them during rendering.

GLES20.glEnable( GLES20.GL_DEPTH_TEST );
GLES20.glDepthFunc( GLES20.GL_LEQUAL );
GLES20.glDepthMask( true );
0
votes

I had the same problem. I fixed it by using a 24-bit depth buffer instead of the default, which is 16 bits (so selecting a 16-bit depth buffer won't make any difference). I used

myGlSurfaceView.setEGLConfigChooser(8,8,8,8,24,0);

Incidentally I had the same problem, with effectively the same solution, on iOS (my code is a portable map renderer, so I need it to run on lots of platforms). The iOS fix was to call

self.drawableDepthFormat = GLKViewDrawableDepthFormat24;

in the init function of my GLKView-derived class.

0
votes

if you're using EglCore class from Google/Android Grafika samples you need to uncomment line to enable depth buffer!

        int[] attribList = {
            EGL14.EGL_RED_SIZE, 8,
            EGL14.EGL_GREEN_SIZE, 8,
            EGL14.EGL_BLUE_SIZE, 8,
            EGL14.EGL_ALPHA_SIZE, 8,
            //EGL14.EGL_DEPTH_SIZE, 16,

https://github.com/google/grafika/blob/master/app/src/main/java/com/android/grafika/gles/EglCore.java#L158