[osg-users] Resizing FBO camera while rendering
Hi, I have rendered a FBO camera to image by using a callback (as seen in the code below), however some OpenGL warnings/erros are raised when I resize at runtime by setupViewer() method. I debugged the code by using Code: export OSG_GL_ERROR_CHECKING=ON and got the following error: Code: Warning: detected OpenGL error 'invalid operation' after applying attribute Viewport 0x7fb35406e500 How can I properly do the resizing of my FBO camera? Code: // create a RTT (render to texture) camera osg::Camera *ImageViewerCaptureTool::createRTTCamera(osg::Camera* cam, osg::Camera::BufferComponent buffer, osg::Texture2D *tex, osg::GraphicsContext *gfxc) { osg::ref_ptr camera = cam; camera->setClearColor(osg::Vec4(0, 0, 0, 1)); 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, tex->getTextureWidth(), tex->getTextureHeight()); camera->setGraphicsContext(gfxc); camera->setDrawBuffer(GL_FRONT); camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); camera->attach(buffer, tex); return camera.release(); } // create float textures to be rendered in FBO osg::Texture2D* ImageViewerCaptureTool::createFloatTexture(uint width, uint height) { osg::ref_ptr tex2D = new osg::Texture2D; tex2D->setTextureSize( width, height ); tex2D->setInternalFormat( GL_RGB32F_ARB ); tex2D->setSourceFormat( GL_RGBA ); tex2D->setSourceType( GL_FLOAT ); tex2D->setResizeNonPowerOfTwoHint( false ); tex2D->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR ); tex2D->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR ); return tex2D.release(); } void ImageViewerCaptureTool::setupViewer(uint width, uint height, double fovY) { // set graphics contexts osg::ref_ptr traits = new osg::GraphicsContext::Traits; traits->x = 0; traits->y = 0; traits->width = width; traits->height = height; traits->pbuffer = true; traits->readDISPLAY(); osg::ref_ptr gfxc = osg::GraphicsContext::createGraphicsContext(traits.get()); // set the main camera _viewer = new osgViewer::Viewer; osg::ref_ptr tex = createFloatTexture(width, height); osg::ref_ptr cam = createRTTCamera(_viewer->getCamera(), osg::Camera::COLOR_BUFFER0, tex, gfxc); cam->setProjectionMatrixAsPerspective(osg::RadiansToDegrees(fovY), (width * 1.0 / height), 0.1, 1000); // render texture to image _capture = new WindowCaptureScreen(gfxc, tex); cam->setFinalDrawCallback(_capture); } osg::ref_ptr ImageViewerCaptureTool::grabImage(osg::ref_ptr node) { // set the current node _viewer->setSceneData(node); // if the view matrix is invalid (NaN), use the identity if (_viewer->getCamera()->getViewMatrix().isNaN()) _viewer->getCamera()->setViewMatrix(osg::Matrixd::identity()); // grab the current frame _viewer->frame(); return _capture->captureImage(); } WindowCaptureScreen METHODS WindowCaptureScreen::WindowCaptureScreen(osg::ref_ptr gfxc, osg::Texture2D* tex) { _mutex = new OpenThreads::Mutex(); _condition = new OpenThreads::Condition(); _image = new osg::Image(); // checks the GraficContext from the camera viewer if (gfxc->getTraits()) { _tex = tex; int width = gfxc->getTraits()->width; int height = gfxc->getTraits()->height; _image->allocateImage(width, height, 1, GL_RGBA, GL_FLOAT); } } WindowCaptureScreen::~WindowCaptureScreen() { delete (_condition); delete (_mutex); } osg::ref_ptr WindowCaptureScreen::captureImage() { //wait to finish the capture image in call back _condition->wait(_mutex); return _image; } void WindowCaptureScreen::operator ()(osg::RenderInfo& renderInfo) const { osg::ref_ptr gfxc = renderInfo.getState()->getGraphicsContext(); if (gfxc->getTraits()) { _mutex->lock(); // read the color buffer as 32-bit floating point renderInfo.getState()->applyTextureAttribute(0, _tex); _image->readImageFromCurrentTexture(renderInfo.getContextID(), true, GL_FLOAT); // grants the access to image _condition->signal(); _mutex->unlock(); } } ... Thanks in advance, Cheers, Rômulo -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=75005#75005 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
Hi Rômulo, I didn't spot any code you posted that handles resizing, did I miss it? Robert. On Tue, 2 Oct 2018 at 08:10, Rômulo Cerqueira wrote: > > Hi, > > I have rendered a FBO camera to image by using a callback (as seen in the > code below), however some OpenGL warnings/erros are raised when I resize at > runtime by setupViewer() method. I debugged the code by using > > > Code: > export OSG_GL_ERROR_CHECKING=ON > > > > and got the following error: > > > Code: > Warning: detected OpenGL error 'invalid operation' after applying attribute > Viewport 0x7fb35406e500 > > > > How can I properly do the resizing of my FBO camera? > > > > Code: > // create a RTT (render to texture) camera > osg::Camera *ImageViewerCaptureTool::createRTTCamera(osg::Camera* cam, > osg::Camera::BufferComponent buffer, osg::Texture2D *tex, > osg::GraphicsContext *gfxc) > { > osg::ref_ptr camera = cam; > camera->setClearColor(osg::Vec4(0, 0, 0, 1)); > 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, tex->getTextureWidth(), > tex->getTextureHeight()); > camera->setGraphicsContext(gfxc); > camera->setDrawBuffer(GL_FRONT); > camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); > camera->attach(buffer, tex); > return camera.release(); > } > > // create float textures to be rendered in FBO > osg::Texture2D* ImageViewerCaptureTool::createFloatTexture(uint width, uint > height) > { > osg::ref_ptr tex2D = new osg::Texture2D; > tex2D->setTextureSize( width, height ); > tex2D->setInternalFormat( GL_RGB32F_ARB ); > tex2D->setSourceFormat( GL_RGBA ); > tex2D->setSourceType( GL_FLOAT ); > tex2D->setResizeNonPowerOfTwoHint( false ); > tex2D->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR ); > tex2D->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR ); > return tex2D.release(); > } > > void ImageViewerCaptureTool::setupViewer(uint width, uint height, double fovY) > { > // set graphics contexts > osg::ref_ptr traits = new > osg::GraphicsContext::Traits; > traits->x = 0; > traits->y = 0; > traits->width = width; > traits->height = height; > traits->pbuffer = true; > traits->readDISPLAY(); > osg::ref_ptr gfxc = > osg::GraphicsContext::createGraphicsContext(traits.get()); > > // set the main camera > _viewer = new osgViewer::Viewer; > osg::ref_ptr tex = createFloatTexture(width, height); > osg::ref_ptr cam = createRTTCamera(_viewer->getCamera(), > osg::Camera::COLOR_BUFFER0, tex, gfxc); > cam->setProjectionMatrixAsPerspective(osg::RadiansToDegrees(fovY), (width > * 1.0 / height), 0.1, 1000); > > // render texture to image > _capture = new WindowCaptureScreen(gfxc, tex); > cam->setFinalDrawCallback(_capture); > } > > osg::ref_ptr > ImageViewerCaptureTool::grabImage(osg::ref_ptr node) > { > // set the current node > _viewer->setSceneData(node); > > // if the view matrix is invalid (NaN), use the identity > if (_viewer->getCamera()->getViewMatrix().isNaN()) > _viewer->getCamera()->setViewMatrix(osg::Matrixd::identity()); > > // grab the current frame > _viewer->frame(); > return _capture->captureImage(); > } > > > WindowCaptureScreen METHODS > > > WindowCaptureScreen::WindowCaptureScreen(osg::ref_ptr > gfxc, osg::Texture2D* tex) { > _mutex = new OpenThreads::Mutex(); > _condition = new OpenThreads::Condition(); > _image = new osg::Image(); > > // checks the GraficContext from the camera viewer > if (gfxc->getTraits()) { > _tex = tex; > int width = gfxc->getTraits()->width; > int height = gfxc->getTraits()->height; > _image->allocateImage(width, height, 1, GL_RGBA, GL_FLOAT); > } > } > > WindowCaptureScreen::~WindowCaptureScreen() { > delete (_condition); > delete (_mutex); > } > > osg::ref_ptr WindowCaptureScreen::captureImage() { > //wait to finish the capture image in call back > _condition->wait(_mutex); > return _image; > } > > void WindowCaptureScreen::operator ()(osg::RenderInfo& renderInfo) const { > osg::ref_ptr gfxc = > renderInfo.getState()->getGraphicsContext(); > > if (gfxc->getTraits()) { > _mutex->lock(); > > // read the color buffer as 32-bit floating point > renderInfo.getState()->applyTextureAttribute(0, _tex); > _image->readImageFromCurrentTexture(renderInfo.getContextID(), true, > GL_FLOAT); > > // grants the access to image > _condition->signal(); > _mutex->unlock(); > } > } > > > > ... > > Thanks in advance, > > Cheers, > Rômulo > > -- > Read this topic online here: > http://forum.openscenegrap
Re: [osg-users] Resizing FBO camera while rendering
Hi Robert, I use the method setupViewer() "to resize" the FBO as well (by instantiating the viewer, camera, texture and callback again). This approach was the best way so far to minimize this problem. ... Thank you! Cheers, Rômulo -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=75014#75014 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
Hi Robert, I use the method setupViewer() "to resize" the FBO as well (by instantiating the viewer, camera, texture and callback again). This approach was the best way so far to minimize this problem. ... Thank you! Cheers, Rômulo -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=75016#75016 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
Hi Rômulo, On Tue, 2 Oct 2018 at 12:40, Rômulo Cerqueira wrote: > I use the method setupViewer() "to resize" the FBO as well (by instantiating > the viewer, camera, texture and callback again). This approach was the best > way so far to minimize this problem. So you are setting a whole new graphics context and associated data on each resize? With this you are getting the GL error? Robert. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
Hi, > So you are setting a whole new graphics context and associated data on > each resize? Yes, Robert. ... Thank you! Cheers, Rômulo -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=75019#75019 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
On Tue, 2 Oct 2018 at 16:20, Rômulo Cerqueira wrote: > > So you are setting a whole new graphics context and associated data on > > each resize? Is it only the FBO that is forcing you to do this? Robert. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
Hi Rômulo, On Tue, 2 Oct 2018 at 18:45, Rômulo Cerqueira wrote: > I use the method setupViewer() "to resize" the FBO as well (by instantiating > the viewer, camera, texture and callback again). This approach was the best > way so far to minimize this problem. What I was trying to work out is whether calling setupViewer() was just done because of the FBO resize of whether it was being done for other reasons as well. As a general approach I'd recommend sticking with a single GraphicsWindow where possible and just handling the resize of the FBO, or at least the mapping of the resize in other ways. For instance one approach you could take is to create a FBO in the maximum window size then just use a viewport to select which part is active. Another approach is to force a rebuild of the FBO by clearing the RenderingCache via camera->setRenderingCache(0). Robert. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
> Is it only the FBO that is forcing you to do this? Yes. Do you have any tip how can I solve this issue? I found similar problems into links below, but I could not have the same success. https://groups.google.com/forum/#!topic/osg-users/Oxb9QF8Myyo https://groups.google.com/forum/#!topic/osg-users/ScUN5VSj6W8 -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=75025#75025 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Resizing FBO camera while rendering
Hi Rômulo, On Tue, 2 Oct 2018 at 22:10, Rômulo Cerqueira wrote: > Do you have any tip how can I solve this issue? I haven't personally implemented what you are after so I provided tips as to what direction to go in. Robert. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org