We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Answers
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.