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 

Multiple cameras sharing multisample fbo


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


Joined: 06 Nov 2015
Posts: 32

PostPosted: Sat Mar 11, 2017 8:17 am    Post subject:
Multiple cameras sharing multisample fbo
Reply with quote

Hi all,

I would like to know how to get multiple slave cameras to successively render to the same texture using multisampling. I've looked at every osg example, but multisampled FBOs are only discussed in the context of a single rendering camera.

Without multisampling life is simple. For example if I have 2 cameras attached to the same color texture, then everything works fine, like this:
[EDIT: fixed a type-o in the code below; backCamera and frontCamera are osg::Camera objects.]
Code:

colorTex = new Texture2D(); // Also perform texture setup...
backCamera->setRenderOrder(Camera::PRE_RENDER, 0); // Render first
backCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
backCamera->attach(Camera::COLOR_BUFFER0, colorTex);

frontCamera->setRenderOrder(Camera::PRE_RENDER, 1); // Render after backCamera
frontCamera->setClearMask(GL_DEPTH_BUFFER_BIT); // Don't erase backCamera's results
frontCamera->attach(Camera::COLOR_BUFFER0, colorTex);

Unless I'm mistaken, this setup works because the FBOs for both cameras render directly to colorTex, and frontCamera is set to not clear the color buffer.

Now, I want to enable multisampling for frontCamera. I tried simply adding the samples/colorsamples argument to attach(), like so:
Code:

frontCamera->attach(Camera::COLOR_BUFFER0, colorTex, 0, 0, false, 2, 2); // 2 samples & colorsamples

This results in the frontCamera's FBO overwriting colorTex entirely, so the backCamera's results are erased. It seems this happens because frontCamera internally renders to a multisampled renderbuffer, then blits this onto colorTex, which overwrites the texture's contents.

So this brings me back to my original question: how do I get the multisample-enabled frontCamera to render ON TOP OF its attached texture instead of overwriting it? Is there a way to "initialize" its renderbuffers with the contents of the texture?

I briefly tried going down the path of using Texture2DMultisample, but there are no examples of that so I didn't get very far.

Thank you!
Ravi
Back to top
View user's profile Send private message
ravidavi
User


Joined: 06 Nov 2015
Posts: 32

PostPosted: Sun Mar 12, 2017 1:05 am    Post subject:
Reply with quote

OK, after thinking about this, reading MANY other forum posts, and looking into the osg::Camera code, I've realized that my post title itself was incorrect. Sharing FBOs is not the appropriate approach, since FBOs are an internal object to a camera's RenderStage, which itself doesn't exist until the cull stage.

I really should have asked: How can I have a camera render on top of an existing texture, if that camera has a multisample attachment? The answer to that question is much easier to find: use a view-aligned textured quad and nested camera with the desired "background" texture bound to it.

Here's the code I'm using for this:
[EDIT: Updated how the copyCamera's render order is specified]
Code:

// Create Texture that will be used for rendering
colorTex = new Texture2D(); // Also perform texture setup, not shown here...

// Create Camera that draws background scene
backCamera = new osg::Camera();
backCamera->setRenderOrder(Camera::PRE_RENDER, 0); // Render first
backCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
backCamera->attach(Camera::COLOR_BUFFER0, colorTex); // Not multi-sampled
backCamera->addChild(backSceneNode);

// Create Camera that draws foreground scene
frontCamera = new osg::Camera();
frontCamera->setRenderOrder(Camera::PRE_RENDER, 1); // Render after backCamera
frontCamera->setClearMask(GL_DEPTH_BUFFER_BIT); // Don't erase backCamera's results
frontCamera->attach(Camera::COLOR_BUFFER0, colorTex, 0, 0, false, 4, 4); // multi-sampled
frontCamera->addChild(frontSceneNode);

// Create view-aligned textured quad
osg::Geometry* geom = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(1, 0, 0), osg::Vec3(0, 1, 0));
osg::Geode *quad = new osg::Geode;
quad->addDrawable(geom);

// Create Camera that copies colorTex to current render buffer
copyCamera = new osg::Camera();
copyCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); // Use our own view/projection matrix
copyCamera->setProjectionMatrixAsOrtho2D(0, 1, 0, 1); // Same as textured quad's bounds
copyCamera->addChild(quad);
osg::StateSet *ss = copyCamera->getOrCreateStateSet();
ss->setRenderBinDetails(-100, "RenderBin"); // Render before rest of scene
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF); // Don't need lighting to copy a texture
ss->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); // Don't want depth test to clip out texture
ss->setTextureAttributeAndModes(0, colorTex, osg::StateAttribute::ON); // Bind colorTex

// Add copy camera as child of frontCamera
frontCamera->addChild(copyCamera);
copyCamera->setRenderOrder(Camera::NESTED_RENDER); // Render within frontCamera's render stage


This seems to work correctly. The render order is:
1: backCamera renders its scene directly to colorTex without multisampling
2: frontCamera creates its multisampled renderbuffer
3: copyCamera renders colorTex to frontCamera's renderbuffer via the textured quad
4: frontCamera renders its scene to its renderbuffer
5: frontCamera resolves its renderbuffer to colorTex

I can confirm that, using this method, colorTex contains both the background and foreground scenes in the correct order.

So now my question is, is this the correct way to approach the problem of chaining a texture between multi-sampled cameras?

Thanks!
Ravi
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 Recommended way to render a scene fro... Hannes Naude General 6 Tue Apr 11, 2017 11:12 am View latest post
No new posts How do cameras handle the render target? wernerM General 3 Wed Mar 15, 2017 9:59 am View latest post
No new posts How to handle cameras in the scene gr... wernerM General 7 Fri Feb 24, 2017 4:47 pm View latest post
No new posts How to improve frame rate when loadin... vinothrajendran4 General 9 Wed Feb 22, 2017 2:01 pm View latest post
No new posts multiple input with osgwidgets Meldryt General 6 Fri Aug 12, 2016 6:49 am 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