[osg-users] Advice on how to best inject behavoir regarding FBOs
Robert Milharcic
robert.milharcic at ib-caddy.si
Mon Aug 10 00:50:17 PDT 2015
On 9.8.2015 17:04, BjXXrn Blissing wrote:
> Hi Robert,
>
> Well, I was hoping to be able to do the integration without modifications to OSG. If such modifications proves to be necessary, then I agree with Jan that we should wait for v0.7 to see if that integration is easier. It seems unnecessary to change OSG for a problem that may disappear in later Oculus SDK versions.
>
> But I am happy to report that I have a working, flicker free solution. Although I had to make a rather cumbersome call inside my pre draw camera callback to get the FBO handle:
>
>
> Code:
>
> osg::Camera* camera = renderInfo.getCurrentCamera();
> osgViewer::Renderer *camRenderer = (dynamic_cast<osgViewer::Renderer*>(camera->getRenderer()));
> if (camRenderer != NULL) {
> osgUtil::SceneView* sceneView = camRenderer->getSceneView(0);
> if (sceneView != NULL) {
> osgUtil::RenderStage* renderStage = sceneView->getRenderStage();
> if (renderStage != NULL) {
> osg::FrameBufferObject* fbo = renderStage->getFrameBufferObject();
> GLuint fboHandle = fbo->getHandle(ctx);
> }
> }
> }
>
>
>
>
> This also forced me to link with osgUtil. I don't know if there is a simpler way of getting the FBO handle, which does not involve osgUtil?
>
> Best regards
>
> Björn
>
Hi Björn,
If you need low level manipulation of the FBO, it migh be best to simply create one that is fully independent/transparent to the osg (as opposed to fully automated high-level camera attachment). Having said that, you could simply create new FBO, in let's say, CustomCamera constructor:
CustomCamera::CustomCamera()
{
//usual camera setup for rtt
//...
osg::Texture2D* texture = createTexture();
//attach(osg::Camera::COLOR_BUFFER, texture);
_fbo = new osg::FrameBufferObject;
_fbo->setAttachment(osg::Camera::COLOR_BUFFER, osg::FrameBufferAttachment(texture));
setPreDrawCallback(new BindFboPreDrawCallback(_fbo.get()));
setPostDrawCallback(new UnBindFboPostDrawCallback);
}
class BindFboPreDrawCallback : public osg::Camera::DrawCallback
{
public:
BindFboPreDrawCallback(osg::FrameBufferObject* fbo) : _fbo(fbo) {}
virtual void operator () (osg::RenderInfo& renderInfo) const
{
_fbo->apply(*renderInfo.getState()); //bind FBO
}
protected:
osg::ref_ptr<osg::FrameBufferObject> _fbo;
};
class UnBindFboPostDrawCallback : public osg::Camera::DrawCallback
{
public:
virtual void operator () (osg::RenderInfo& renderInfo) const
{
renderInfo.getState()->get<osg::GLExtensions>()->glBindFramebuffer(osg::FrameBufferObject::READ_DRAW_FRAMEBUFFER, 0);
}
};
You can the manipulate CustomCamera's owned fbo or reattach textures at any suitable stage ...
P.S.: You may also need to call _fbo->resizeGLObjectBuffers(maxSize) and _fbo->releaseGLObjects(state) when appropriate.
Best Regards,
Robert Milharcic
More information about the osg-users
mailing list