[osg-users] glCopyTexImage2D or Frame Buffer Object

John Ivar Haugland john.haugland at gmail.com
Wed Apr 27 02:08:34 PDT 2011


Hi,

I found a solution using a shared FBO object and two draw callbacks.

_fbo = new osg::FrameBufferObject();
_fbo->setAttachment(osg::Camera::DEPTH_BUFFER,
osg::FrameBufferAttachment(new
osg::RenderBuffer(width,height,GL_DEPTH_COMPONENT24)));
_fbo->setAttachment(osg::Camera::COLOR_BUFFER0,
osg::FrameBufferAttachment(_texture_left.get()));
_fbo->setAttachment(osg::Camera::COLOR_BUFFER1,
osg::FrameBufferAttachment(_texture_right.get()));


_camera_left = createFBOCamera(gc, _fbo.get(), GL_COLOR_ATTACHMENT0_EXT);
_camera_right = createFBOCamera(gc, _fbo.get(), GL_COLOR_ATTACHMENT1_EXT);


osg::Camera* createFBOCamera(osg::GraphicsContext* context,
osg::FrameBufferObject* fbo, GLenum buffer) {
osg::ref_ptr<osg::Camera> camera = new osg::Camera;

camera->setGraphicsContext(context);
 camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 0.0f));
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->setViewport(new osg::Viewport(0,0, 100, 100));
camera->setProjectionMatrix(osg::Matrixd::identity());

camera->setPreDrawCallback(new PreDrawFBOCallback(fbo));
camera->setPostDrawCallback(new PostDrawFBOCallback());
camera->setDrawBuffer(buffer);
camera->setReadBuffer(buffer);


camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
camera->setRenderOrder(osg::Camera::PRE_RENDER,0);
camera->setAllowEventFocus(false);

return camera.release();
}

struct PreDrawFBOCallback : public osg::Camera::DrawCallback
{
    PreDrawFBOCallback( osg::FrameBufferObject* fbo )
: _fbo(fbo)
    {
    }

    virtual void operator () (osg::RenderInfo& renderInfo) const
    {
_fbo->apply( *renderInfo.getState() );
}

osg::ref_ptr<osg::FrameBufferObject> _fbo;
};

struct PostDrawFBOCallback : public osg::Camera::DrawCallback
{
PostDrawFBOCallback()
    {
    }

    virtual void operator () (osg::RenderInfo& renderInfo) const
    {
osg::FBOExtensions::instance( renderInfo.getState()->getContextID(), false
)->glBindFramebuffer( GL_FRAMEBUFFER_EXT, 0 );
}

};


On Tue, Apr 26, 2011 at 9:54 AM, John Ivar Haugland <john.haugland at gmail.com
> wrote:

> Robert,
>
> How do you arrange the camera setup so that two cameras share the same FBO
> ?
>
> John Ivar Haugland
>
> On Wed, Apr 20, 2011 at 10:00 AM, Robert Osfield <robert.osfield at gmail.com
> > wrote:
>
>> HI Martin,
>>
>> 2011/4/20 "Martin Großer" <Grosser.Martin at gmx.de>:
>> > Ok thank you. And what do you think about
>> osg::Texture2D::copyTexImage2D(...)? Is this a worse solution than the FBO?
>>
>> In what respect?  Performance wise rendering directly to a texture
>> using FBO is likely to be more efficient than rendering to the frame
>> buffer and then using Texture2D::copyTextImage2D(..).  Using FBO's is
>> also more flexible as there is no need to write the frame buffer
>> multiple times within one frame.
>>
>> Robert.
>>  _______________________________________________
>> 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/20110427/828f4f3c/attachment-0004.htm>


More information about the osg-users mailing list