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())
    return;

  // add shader to graphics
  graphics.shader(shader);

  depthImage.beginDraw();
  depthImage.image(graphics, 0, 0);
  depthImage.endDraw();

  // reset shader after operation
  if (!shaderApplied)
    graphics.resetShader();
}

Complete sketch: gist.github.com/d6a61bcfe1ffc4f36eb1592d7143ab8f

Tagged:

Answers

  • edited April 2018

    Hi

    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.

    https://www.opengl.org/discussion_boards/showthread.php/179953-(gl_FragCoord-z-gl_FragCoord-w)-for-quick-depth-calculation-camera-to-fragment

    https://stackoverflow.com/a/13731548

    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.

    // forum.processing.org/two/discussion/comment/122724
    // based on: 
    // Alasdair Turner 2009
    // openprocessing.org/sketch/2950#
    
    
    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);
      noStroke();
    
      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 ? 
      // processing.org/reference/camera_.html
      // processing.org/reference/perspective_.html
    
      // perspective(fovy, aspect, zNear, zFar);
      // camera(width/2, height/2, cameraZ, width/2, height/2, 0, 0, 1, 0);
    
      // printCamera();
    
      background(203);
      lights();
      pushMatrix();
    
      translate(width/2.0, height/2.0, -100. );
      fill(155);
      box(200, 100, 0.1 );
      fill(255);
      sphere(50);
      popMatrix();
    
      gl = ((PJOGL) beginPGL()).gl.getGL2ES2();
      endPGL();
      // not optimatl but enough for your porposes 
      // khronos.org/opengl/wiki/Common_Mistakes#Depth_Buffer_Precision
      // 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);
      textSize(24);
      text(nf(cameraZ+worldZ, 0, 1), mouseX, mouseY);
    }
    
Sign In or Register to comment.