Render depth buffer onto 2d PGraphics

edited February 2018 in GLSL / Shaders

I would like to use the depth buffer of a PGraphics3D in a texture shader on a PGraphics2D. Now I could not really find a way to share the depth buffer of the 3d scene with the 2d texture, so I decided to draw the 3D depth as fragment shader onto a texture and then use this texture as separate input to the 2d texture fragment shader.

In the example sketch, you can enable the depth pass shader for the 3D scene (left picture) with any key. So the depth buffer is correctly drawn onto the screen.

Now the problem that I have is, that I am not able to shade / filter the 3d context with the depth buffer shader, and draw it onto the 2d canvas. Without doing that, I am not able to use it inside the next texture shader I would like to apply to the 2d graphics.

I hope it is not too confusing. What I want is to have the depth information in a 2d texture shader.

Attached is the example sketch, which currently is working on the right side (no depth information is shaded onto the 2d context).

void createDepthImage(PGraphics3D graphics)
  if (!graphics.is3D())

  // add shader to graphics

  depthImage.image(graphics, 0, 0);

  // reset shader after operation
  if (!shaderApplied)

Complete sketch:



  • edited April 2018


    gl_FragCoord.z / gl_FragCoord.w

    would be nice if it would be that easy.

    this is serious math i save for a rainy, lonely sunday.

    Modern OpenGL has actualy build in functions like a precalculated Depth Texture, also gl_FragDepth will saves some headache. In native JOGL mode with modern Hardware, i can image, - it could be done. (Also thinking of times when people has to calculate Perspective-Correct Texture Mapping and such things ...)

    Reimplement the hole thing is paintful, but yes and some point, - and also in mind of backward compatibility, i think, people had to do it in in someway, unfortunately i can't find some good copy +paste examples.

    What you can do is experiment on the CPU and then paste your code to the shader GPU version.

    Don't forget to share your results : )

    Good Luck.

    // based on: 
    // Alasdair Turner 2009
    import com.jogamp.opengl.GL2ES2;
    import com.jogamp.opengl.util.GLBuffers;
    import java.nio.FloatBuffer;
    GL2ES2 gl;
    FloatBuffer zbuff;
    void setup() {
      size(640, 360, P3D);
      zbuff = GLBuffers.newDirectFloatBuffer(new float[]{1f});
    void draw() {
      float aspect = float(width)/float(height);
      float fovy =PI/3.; 
      float cameraZ =(height/2.0) / tan(degrees(fovy)*PI / 360.0);  
      float zNear =  cameraZ/10.0; 
      float zFar=cameraZ*10.0;
      // processings default ? 
      // perspective(fovy, aspect, zNear, zFar);
      // camera(width/2, height/2, cameraZ, width/2, height/2, 0, 0, 1, 0);
      // printCamera();
      translate(width/2.0, height/2.0, -100. );
      box(200, 100, 0.1 );
      gl = ((PJOGL) beginPGL()).gl.getGL2ES2();
      // not optimatl but enough for your porposes 
      // note in case you want multiple objects: detph texture is native opengl means it's flipped on the y axis in jogl
      gl.glReadPixels( mouseX, mouseY, 1, 1, GL2ES2.GL_DEPTH_COMPONENT, GL2ES2.GL_FLOAT, zbuff);
      float z = 2.0 * ( zbuff.get(0) - 0.5f);
      float worldZ =  2.0f*zFar * zNear / (z*(zFar-zNear)-(zFar+zNear) );
      fill(0, 102, 153, 250);
      text(nf(cameraZ+worldZ, 0, 1), mouseX, mouseY);
Sign In or Register to comment.