[osg-users] How do I access the Z-Buffer content from the last render pass

Ralf Stokholm alfmail1 at arenalogic.com
Sun Mar 8 23:04:26 PDT 2009


Hi Daniel

I dont know if its possible to do this from the "native" z buffer, but I
know its possible if you create the zbuffer as part of a render target.

i.e.


osg::Texture2D* CreateTexture( unsigned short w, unsigned short h )

{

osg::Texture2D* texture = new osg::Texture2D() ;

texture->setTextureSize( w, h );

texture->setInternalFormat(GL_RGBA);

texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);

texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);

return texture;

}

osg::Texture2D* CreateDepthTexture( unsigned short w, unsigned short h )

{

osg::Texture2D* texture = new osg::Texture2D() ;

texture->setTextureSize( w, h );

texture->setInternalFormat(GL_DEPTH_COMPONENT);

texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);

texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);

return texture;

}

{

SceneTextureCamera::SceneTextureCamera( int sizeX, int sizeY, const std::
string& camera )

{

//create a texture to use as a render target 512 x 512 for now

m_RttFrameBuffer = CreateTexture(sizeX,sizeY);

m_RttDepthBuffer = CreateDepthTexture(sizeX,sizeY);

//get the camera node and do some initialization

m_RttCamera = new osg::Camera;

m_RttCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);

m_RttCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

m_RttCamera->setViewport(0, 0, m_RttFrameBuffer->getTextureWidth(),
m_RttFrameBuffer->getTextureHeight());

m_RttCamera->setCullingMode(osg::CullSettings::VIEW_FRUSTUM_CULLING);

m_RttCamera->setCullingActive(true);

// Frame buffer objects are the best option

m_RttCamera->setRenderTargetImplementation(osg::Camera::PIXEL_BUFFER_RTT);

// We need to render to the texture BEFORE we render to the screen

m_RttCamera->setRenderOrder(osg::Camera::PRE_RENDER);

// The camera will render into the texture that we created earlier

m_RttCamera->attach(osg::Camera::COLOR_BUFFER, m_RttFrameBuffer.get());

m_RttCamera->attach(osg::Camera::DEPTH_BUFFER, m_RttDepthBuffer.get());

// Set the view transform of the camera

m_RttCamera->setViewMatrixAsLookAt(osg::Vec3(1.0f, 0.0f, -1.0f),
osg::Vec3(278.1f,
982.1f, -1.0f), osg::Vec3(0.0f, 0.0f, 1.0f));

m_RttCamera->setProjectionMatrixAsPerspective(3.0,1.0,20.0,50000.0);

m_RttCamera->setComputeNearFarMode(osg::CullSettings::
DO_NOT_COMPUTE_NEAR_FAR);

// Add whatever children you want drawn to the texture

m_RttCamera->addChild(SceneRoot);

}
Brgs.

Ralf Stokholm
www.arenalogic.com


2009/3/9 Daniel Holz <danielh at cm-labs.com>

> Hi osg-users,
>
> is it possible to access the Z-Buffer content from a render pass 1 in the
> shaders during a second render pass 2 if I do not clear the Z-Buffer content
> in pass 1?
>
> In my project I render my scene in pass 1 and set the clear mask for pass 2
> to "0", hence not clearing neither the color nor the depth buffer:
>   pCamPass2->setClearMask(0);
>
> Now, how do I read out the Z-Buffer content of the gpu for a given pixel in
> the fragment shader of pass2?
> Trying to read from gl_FragDepth did not work.
>
> Any suggestions?
>
> Cheers
> Daniel
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20090309/8cba858a/attachment-0003.htm>


More information about the osg-users mailing list