This is pretty easy to do if you just want to use the Z position of your geometry. You can have a vertex shader as shown below:
attribute vec4 position; varying float zDepth; uniform mat4 modelViewProjMatrix; void main() { vec4 newPosition = modelViewProjMatrix * position; gl_Position = newPosition; zDepth = (2.0 - (1.0 + newPosition.z))/2.0; }
and fragment shader for example
varying highp float zDepth; void main() { gl_FragColor = vec4(zDepth, zDepth, 0.0, 1.0); }
which will look like this:
I have an example of this in this sample code for iPad I have compiled for my class OpenGL ES 2.0.
source share