[osg-users] Depth texture display

Nicolas Baillard nicolas.baillard at gmail.com
Fri Feb 26 04:35:04 PST 2016


Hello everyone.

I'm trying to save my depth buffer into a texture and then render it on a quad using a fragment shader.

First I create a texture and attach it to a camera

Code:
_depth = new osg::Texture2D();
_depth->setTextureSize(_w, _h);
_depth->setInternalFormat(GL_DEPTH_COMPONENT);
_depth->setSourceFormat(GL_DEPTH_COMPONENT);
_depth->setSourceType(GL_FLOAT);
_depth->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
_depth->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);

_camera = new osg::Camera();
_camera->setViewport(0., 0., _w, _h);
_camera->setRenderOrder(osg::Camera::PRE_RENDER);
_camera->setClearColor(Color(0., 0., 0., 1.));
_camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
_camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
_camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
_camera->attach(osg::Camera::DEPTH_BUFFER, _depth);
_camera->setGraphicsContext(_view->getCamera()->getGraphicsContext());
_view->addSlave(_camera, true);




Then I bind my depth texture to a uniform and try to display it with the following shader.

Code:
uniform sampler2D depth;

float linearize(vec2 uv)
{
   float n = 1000.;   // Near
   float f = 100000.; // Far
   float z = texture2D(depth, uv).r;
   return (2. * n) / (f + n - z * (f - n));
}

void main()
{
   float d = linearize(gl_TexCoord[0].st);
   gl_FragColor = vec4(d, d, d, 1.);
}




It doesn't work, all I see is black, as if my depth texture was cleared.
If I do the same thing with the color buffer (with an RBGA texture) it works as expected, so I know my camera is working and looking at the scene as it should. But I can't make it work with a depth texture. What did I do wrong ?

Thanks,
Nicolas

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=66440#66440








More information about the osg-users mailing list