OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Resizing FBO camera while rendering


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 65
Location: Brazil

PostPosted: Tue Oct 02, 2018 3:34 am    Post subject:
Resizing FBO camera while rendering
Reply with quote

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<osg::Camera> 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<osg::Texture2D> 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<osg::GraphicsContext::Traits> 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<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());

    // set the main camera
    _viewer = new osgViewer::Viewer;
    osg::ref_ptr<osg::Texture2D> tex = createFloatTexture(width, height);
    osg::ref_ptr<osg::Camera> 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<osg::Image> ImageViewerCaptureTool::grabImage(osg::ref_ptr<osg::Node> 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<osg::GraphicsContext> 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<osg::Image> 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<osg::GraphicsContext> 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
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12145

PostPosted: Tue Oct 02, 2018 8:01 am    Post subject:
Resizing FBO camera while rendering
Reply with quote

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:
Quote:

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<osg::Camera> 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<osg::Texture2D> 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<osg::GraphicsContext::Traits> 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<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());

// set the main camera
_viewer = new osgViewer::Viewer;
osg::ref_ptr<osg::Texture2D> tex = createFloatTexture(width, height);
osg::ref_ptr<osg::Camera> 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<osg::Image> ImageViewerCaptureTool::grabImage(osg::ref_ptr<osg::Node> 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<osg::GraphicsContext> 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<osg::Image> 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<osg::GraphicsContext> 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








------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 65
Location: Brazil

PostPosted: Tue Oct 02, 2018 11:39 am    Post subject:
Reply with quote

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
Back to top
View user's profile Send private message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 65
Location: Brazil

PostPosted: Tue Oct 02, 2018 11:49 am    Post subject:
Reply with quote

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
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12145

PostPosted: Tue Oct 02, 2018 1:45 pm    Post subject:
Resizing FBO camera while rendering
Reply with quote

Hi Rômulo,

On Tue, 2 Oct 2018 at 12:40, Rômulo Cerqueira
<> wrote:
Quote:
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.


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 65
Location: Brazil

PostPosted: Tue Oct 02, 2018 2:02 pm    Post subject:
Reply with quote

Hi,

Quote:
So you are setting a whole new graphics context and associated data on
each resize?


Yes, Robert.

Quote:
With this you are getting the GL error?


Yes, but it happens sometimes when I "resize" the FBO texture.

...

Thank you!

Cheers,
Rômulo
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12145

PostPosted: Tue Oct 02, 2018 5:26 pm    Post subject:
Resizing FBO camera while rendering
Reply with quote

On Tue, 2 Oct 2018 at 16:20, Rômulo Cerqueira
<> wrote:
Quote:
Quote:
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.


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12145

PostPosted: Tue Oct 02, 2018 6:40 pm    Post subject:
Resizing FBO camera while rendering
Reply with quote

Hi Rômulo,

On Tue, 2 Oct 2018 at 18:45, Rômulo Cerqueira
<> wrote:
Quote:
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.


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
romulogcerqueira
User


Joined: 11 Jun 2015
Posts: 65
Location: Brazil

PostPosted: Tue Oct 02, 2018 6:43 pm    Post subject:
Re: Resizing FBO camera while rendering
Reply with quote

Quote:
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
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 12145

PostPosted: Wed Oct 03, 2018 8:22 am    Post subject:
Resizing FBO camera while rendering
Reply with quote

Hi Rômulo,

On Tue, 2 Oct 2018 at 22:10, Rômulo Cerqueira
<> wrote:
Quote:
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.


------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts Rendering multiple screens njr3 General 1 Wed Oct 31, 2018 8:46 pm View latest post
No new posts Matrices with Camera and CameraManipu... Tare General 0 Fri Oct 19, 2018 12:54 pm View latest post
No new posts How to speed up rendering in this case. pwang General 3 Tue Jul 31, 2018 3:00 am View latest post
No new posts Volume rendering issues... tommwilliamson General 4 Wed Jul 25, 2018 12:19 pm View latest post
No new posts massive text rendering OmegaDoom General 3 Mon Jul 09, 2018 12:14 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP