[osg-users] Problems with MRT

Rômulo Cerqueira romulogcerqueira at gmail.com
Mon May 21 16:49:04 PDT 2018


Hi Robert and mp3butcher,

thanks for all comments. I tried the mp3butcher`s suggestion (save the rendered image data to txt) without errors. Please see my current source code and outputs.


Code:
// OSG includes
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgViewer/Viewer>
#include <osg/Camera>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Texture2D>

#include <fstream>
#include <iterator>
#include <string>
#include <vector>

struct SnapImage : public osg::Camera::DrawCallback {
    SnapImage(osg::GraphicsContext* gc, osg::Texture2D* tex ) {
        if (gc->getTraits()) {
            _texColor = tex;
        }
    }

    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, GL_FLOAT);
        mColor->setInternalTextureFormat( GL_RGBA32F_ARB );

        float* data = (float*) mColor->data();
        std::vector<float> vec = std::vector<float>(data, data + (mColor->s() * mColor->t()));

        std::ofstream fout("vector.txt");
        std::copy(vec.begin(), vec.end(), std::ostream_iterator<float>(fout, "\n"));
    }
    osg::ref_ptr<osg::Texture2D> _texColor;
};

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_RGBA32F_ARB );
    tex->setSourceFormat( GL_RGBA );
    tex->setSourceType( GL_FLOAT );
    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 );

    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 = 300;
    unsigned int winH = 200;

    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->addChild( geode );
    root->addChild(postRenderCamera);

    // setup the callback
    SnapImage* finalDrawCallback = new SnapImage(viewer.getCamera()->getGraphicsContext(), attachedTextures[0]);
    mrtCamera->setFinalDrawCallback(finalDrawCallback);

    return (viewer.run());
}


 

... 

Thank you!

Cheers,
Rômulo[/img]

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



-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: vector.txt
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20180522/34b5f284/attachment-0001.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: screenshot_post_render.png
Type: image/png
Size: 61789 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20180522/34b5f284/attachment-0001.png>


More information about the osg-users mailing list