OpenGL ES depth buffer android, can't get to w

2019-02-13 21:48发布

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".

3条回答
Deceive 欺骗
2楼-- · 2019-02-13 21:59

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

myGlSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
查看更多
Juvenile、少年°
3楼-- · 2019-02-13 22:15

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 );
查看更多
Deceive 欺骗
4楼-- · 2019-02-13 22:15

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.

查看更多
登录 后发表回答