sinsro
sinsro

Reputation: 915

Setting the depth buffer content quickly in OpenGL ES 2.0

I am using OpenTK, opengl ES 2.0, and want to clear the depth buffer to one loaded externally for each redraw.

What I want to achieve is to clear the screen to a prerendered image and its depth buffer for situations where the camera and the background objects does not move, and then only render moving objects on top of this in the traditional way.

How to do this in a performance efficient way?

Upvotes: 1

Views: 547

Answers (1)

Tommy
Tommy

Reputation: 100622

gl_FragCoord is immutable and GLES SL doesn't supply gl_FragDepth. So there's no way to set the output depth of a fragment within the fragment shader.

Two options present themselves as a result:

  • don't use the hardware depth buffer
  • set depths in the vertex shader

To do the former, you'd bind your depth texture to a spare unit and subsequently would do an explicit compare and discard in the fragment shader, based on gl_FragCoord.

To do the latter you'd submit an array of points, one per fragment, sample the depth texture within the vertex shader and output the depth appropriately. If your hardware doesn't support texture sampling within vertex shaders then you could do the same thing on the CPU. Leave the CPU-computed fragments in a VBO and you shouldn't end up paying that much frame-on-frame.

Upvotes: 1

Related Questions