Hello folks,

the main intention is to use an off screen RTT scene setting to generate 
textures for the main scene asynchronously. The RTT base seems to work, but 
rebinding the color_buffer at each frame to a new target (image or texture) 
does not work. (During the development osg::Image instances are bound to the 
color buffer instead of textures.)

The off screen RTT approach seems to work (thank to this forum).
Here is the setup:

Code:
// Init the GraphicsContext Traits
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
osg::GraphicsContext::Traits;
traits->x = 0;
traits->y = 0;
traits->width = RenderSettings::ETileSize;
traits->height = RenderSettings::ETileSize;
traits->red = 8;
traits->green = 8;
traits->blue = 8;
traits->alpha = 8;
traits->windowDecoration = false;
traits->pbuffer = true;
traits->doubleBuffer = false;
traits->sharedContext = 0;

// Create the GraphicsContext
osg::ref_ptr<osg::GraphicsContext> gc = 
osg::GraphicsContext::createGraphicsContext(traits);

// Create & Setup Camera
mViewer= new osgViewer::Viewer;
osg::ref_ptr<osg::Camera> camera = mViewer->getCamera();
camera->setGraphicsContext(gc);
camera->setClearColor(osg::Vec4(0.1f, 0.1f, 0.3f, 1.0f));
camera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewport(new osg::Viewport( 0, 0, RenderSettings::ETileSize, 
RenderSettings::ETileSize));

osg::Camera::RenderTargetImplementation renderImplementation = 
osg::Camera::FRAME_BUFFER_OBJECT;
//osg::Camera::RenderTargetImplementation renderImplementation = 
osg::Camera::PIXEL_BUFFER;
camera->setRenderTargetImplementation( renderImplementation );

PrepairScreenshot* prepairScreenshot= new PrepairScreenshot;
camera->setUpdateCallback( prepairScreenshot);

CaptureScreenshot* captureScreenshot= new CaptureScreenshot(prepairScreenshot);
camera->setFinalDrawCallback( captureScreenshot);

mViewer->setSceneData( mScene);

osg::Geode* g= createAxisModel(); // DEB: test scene
//mScene->addChild( g);
camera->addChild(g);

mViewer->setThreadingModel( osgViewer::Viewer::SingleThreaded);

mViewer->realize();




But, I want to change the camera attached color_buffer for each frame -- which 
is not working. The callbacks:

Code:
class PrepairScreenshot : public osg::NodeCallback 
{
   osg::ref_ptr<osg::Image> mImage;
public:
   PrepairScreenshot()
      : osg::NodeCallback()
      , mImage()
   {}

   osg::Image* getImage() const {
      return mImage.get();
   }

   virtual void operator () (osg::Node* node, osg::NodeVisitor* nv) override
   {
      osg::Camera* cam= dynamic_cast<osg::Camera*>(node);
      if (cam)
      {
         // updateProjection(cam);
         static float x=-4.0f;
         cam->setProjectionMatrixAsOrtho2D( x, x + 4, -2, 2);
         x+= 0.1f;

#if 0
         mImage = 0;  // test new img binding for each frame!
         // here: only the first img has receives content "tst_1.png", other 
all gray.
#else
         // here: all frames have valid content "tst_*.png".
#endif
         if (!mImage)
         {
            mImage= new osg::Image;
            mImage->allocateImage( RenderSettings::ETileSize, 
RenderSettings::ETileSize, 1, GL_RGBA, GL_UNSIGNED_BYTE );
            unsigned char* data= (unsigned char*) mImage->getDataPointer();
            for (int i=0; i < 100; ++i) {
               data[i*4+1]= 0xFF;
            }

            cam->setRenderingCache( NULL );
            cam->detach( osg::Camera::COLOR_BUFFER); 
            //cam->attach( osg::Camera::COLOR_BUFFER, tilePtr.getTileTexture());
            cam->attach( osg::Camera::COLOR_BUFFER, mImage.get());
         }
      }

      traverse( node, nv);
   }
};

class CaptureScreenshot : public osg::Camera::DrawCallback
{
   PrepairScreenshot* mPreparator;
public:
   CaptureScreenshot(PrepairScreenshot* preparator)
      : osg::Camera::DrawCallback()
      , mPreparator( preparator)
   {}

   virtual void operator () (osg::RenderInfo& renderInfo) const override
   {
      osg::Image* image= mPreparator->getImage();
      if (image) {
         static int imgCnt= 0;

         std::ostringstream buf;
         buf << "D:\\tst_" << (++imgCnt) << ".png";
         osgDB::writeImageFile( *image, buf.str());
      }
   }
};



Letting the same osg::Image instance attached all the time works (the image 
receives the frame results). But attaching a new osg::Image instance for each 
upcoming frame (in update callback) works only for the initial attached image, 
all subsequent image instances remain gray (untouched). To test this I write 
the image content to a new file in each camera->finalDrawCallback.

The frames are triggered manually by 
Code:
mViewer->frame();

 until the texture update queue is empty.

Can you tell me, what is the right way to change the color buffer binding for 
each new frame?


... 

Thank you!

Cheers,
Alexej

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





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to