[osg-users] Problems with MRT
Julien Valentin
julienvalentin51 at gmail.com
Sun May 20 14:44:51 PDT 2018
Hi
Are you sure read back images are those expected?
output im->data(0...image.size) in the standard output in order to be sure your result are good
If it's not the case add code to control where it fails:
1 rendering color or depth buffers to textures
2 display result textures on quads to see if it what u expect
3 Read color or depth buffer in the final draw callback (output readback on console)
4 Write images (color or depth) to disk
Once you've done it you'll know where is the problem (i don't believe it comes from plugin...)
(And again, rtt COLOR_BUFFERX to float texture works like a charm but I'm not sure you can read back those integer buffers to float textures)
Hope it helps
Cheers
Perhaps you should add step in your example in order to diagnose where your issue is
romulogcerqueira wrote:
> Hi,
>
> just updating my progress:
>
> What have I got so far?
> [ ✓ ] Allow rendering for color or depth buffers on the same camera;
> [ ✓ ] Display only the color buffer in the post render camera;
> [ ✓ ] Read color or depth buffer in the final draw callback;
> [ ✗ ] Write collected image (color or depth) in disk - only images as GL_UNSIGNED_BYTE, instead of GL_FLOAT.
>
> I changed the buffer reader in my callback (readImageFromCurrentTexture() instead of readPixels()), however I still got problems when I render floating textures...
>
> Follows my current code. Any suggestions here?
>
>
> Code:
> // OSG includes
> #include <osgDB/ReadFile>
> #include <osgDB/WriteFile>
> #include <osgViewer/Viewer>
> #include <osg/Camera>
> #include <osg/Geode>
> #include <osg/Geometry>
> #include <osg/Texture2D>
>
> struct SnapImage : public osg::Camera::DrawCallback {
> SnapImage(osg::GraphicsContext* gc, osg::Texture2D* tex1, osg::Texture2D* tex2 ) {
> if (gc->getTraits()) {
> _texColor = tex1;
> _texDepth = tex2;
> }
> }
>
> virtual void operator () (osg::RenderInfo& renderInfo) const {
> // color buffer
> renderInfo.getState()->applyTextureAttribute(0, _texColor);
> osg::ref_ptr<osg::Image> mColor = new osg::Image();
> mColor->readImageFromCurrentTexture(renderInfo.getContextID(), true, _texColor->getSourceType());
> osgDB::ReaderWriter::WriteResult wrColor = osgDB::Registry::instance()->writeImage(*mColor, "./Test-color.png", NULL);
> if (!wrColor.success()) {
> osg::notify(osg::WARN) << "Color image: failed! (" << wrColor.message() << ")" << std::endl;
> }
>
> // depth buffer
> renderInfo.getState()->applyTextureAttribute(0, _texDepth);
> osg::ref_ptr<osg::Image> mDepth = new osg::Image();
> mDepth->readImageFromCurrentTexture(renderInfo.getContextID(), true, _texDepth->getSourceType());
> osgDB::ReaderWriter::WriteResult wrDepth = osgDB::Registry::instance()->writeImage(*mDepth, "./Test-depth.png", NULL);
> if (!wrDepth.success()) {
> osg::notify(osg::WARN) << "Depth image: failed! (" << wrDepth.message() << ")" << std::endl;
> }
> }
> osg::ref_ptr<osg::Texture2D> _texColor;
> osg::ref_ptr<osg::Texture2D> _texDepth;
> };
>
> osg::Camera* setupMRTCamera( osg::ref_ptr<osg::Camera> camera, std::vector<osg::Texture2D*>& attachedTextures, int w, int h ) {
> camera->setClearColor( osg::Vec4() );
> camera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
> camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
> camera->setRenderOrder( osg::Camera::PRE_RENDER, 0 );
> camera->setViewport( 0, 0, w, h );
>
> osg::Texture2D* tex = new osg::Texture2D;
> tex->setTextureSize( w, h );
> tex->setInternalFormat( GL_RGB );
> tex->setSourceFormat( GL_RGBA );
> tex->setSourceType( GL_UNSIGNED_BYTE );
> tex->setResizeNonPowerOfTwoHint( false );
> tex->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR );
> tex->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR );
> attachedTextures.push_back( tex );
> camera->attach( osg::Camera::COLOR_BUFFER, tex );
>
> tex = new osg::Texture2D;
> tex->setTextureSize(w,h);
> tex->setSourceFormat(GL_DEPTH_COMPONENT);
> tex->setInternalFormat(GL_DEPTH_COMPONENT);
> tex->setSourceType(GL_UNSIGNED_BYTE);
> attachedTextures.push_back( tex );
> camera->attach( osg::Camera::DEPTH_BUFFER, tex );
>
> return camera.release();
> }
>
>
> int main() {
> osg::ref_ptr< osg::Group > root( new osg::Group );
> root->addChild( osgDB::readNodeFile( "/home/romulo/Tools/OpenSceneGraph-Data/cow.osg" ) );
> unsigned int winW = 800;
> unsigned int winH = 600;
>
> osgViewer::Viewer viewer;
> viewer.setUpViewInWindow( 0, 0, winW, winH );
> viewer.setSceneData( root.get() );
> viewer.realize();
>
> // setup MRT camera
> std::vector<osg::Texture2D*> attachedTextures;
> osg::Camera* mrtCamera ( viewer.getCamera() );
> setupMRTCamera( mrtCamera, attachedTextures, winW, winH );
>
> // set RTT textures to quad
> osg::Geode* geode( new osg::Geode );
> geode->addDrawable( osg::createTexturedQuadGeometry(
> osg::Vec3(-1,-1,0), osg::Vec3(2.0,0.0,0.0), osg::Vec3(0.0,2.0,0.0)) );
> geode->getOrCreateStateSet()->setTextureAttributeAndModes( 0, attachedTextures[0] );
> geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
> geode->getOrCreateStateSet()->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
>
> // configure postRenderCamera to draw fullscreen textured quad
> osg::Camera* postRenderCamera( new osg::Camera );
> postRenderCamera->setClearMask( 0 );
> postRenderCamera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER, osg::Camera::FRAME_BUFFER );
> postRenderCamera->setReferenceFrame( osg::Camera::ABSOLUTE_RF );
> postRenderCamera->setRenderOrder( osg::Camera::POST_RENDER );
> // postRenderCamera->setViewMatrix( osg::Matrixd::identity() );
> // postRenderCamera->setProjectionMatrix( osg::Matrixd::identity() );
> postRenderCamera->addChild( geode );
> root->addChild(postRenderCamera);
>
> // setup the callback
> SnapImage* finalDrawCallback = new SnapImage(viewer.getCamera()->getGraphicsContext(), attachedTextures[0], attachedTextures[1]);
> mrtCamera->setFinalDrawCallback(finalDrawCallback);
>
> return (viewer.run());
> }
>
>
>
> ...
>
> Thank you!
>
> Cheers,
> Rômulo
------------------------
Twirling twirling twirling toward freedom
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=73682#73682
More information about the osg-users
mailing list