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 

Render multiple PRE_RENDER cameras to FBO


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


Joined: 10 Oct 2017
Posts: 5

PostPosted: Tue Nov 13, 2018 3:41 pm    Post subject:
Render multiple PRE_RENDER cameras to FBO
Reply with quote

Hi,

I'm having trouble properly rendering a couple of cameras to an FBO. I have two PRE_RENDER cameras that use a FBO and render to a texture. The color buffer seems to be reset before rendering the second camera though. This is my code:
Code:

osg::Group* root = new osg::Group;

osg::Camera* backgroundCamera = new osg::Camera;
osg::Camera* sceneCamera = new osg::Camera;
osg::Camera* finalCamera = new osg::Camera;
   
root->addChild(backgroundCamera);
root->addChild(sceneCamera);
root->addChild(finalCamera);

//-----------------------------------------
// create texture to render to
//-----------------------------------------
osg::ref_ptr<osg::Texture2D> tex = new osg:
tex->setSourceType(GL_FLOAT);
tex->setSourceFormat(GL_RGBA);
tex->setInternalFormat(GL_RGBA);
tex->setTextureSize(512, 512);

//-------------------------------------------------------------------
// add geometry to background camera
//-------------------------------------------------------------------
osg::Geometry* background = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(0, 1.0f, 0), osg::Vec3(1.0f, 0, 0));
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));
colors->push_back(osg::Vec4(0, 0, 0, 1.0f));
colors->push_back(osg::Vec4(0, 0, 0, 1.0));
background->setColorArray(colors.get());
background->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
osg::Geode* geode = new osg::Geode;
geode->addChild(background);
backgroundCamera->addChild(geode);

backgroundCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
backgroundCamera->setViewport(0, 0, tex->getTextureWidth(), tex->getTextureHeight());
backgroundCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, true, 512, 512);
backgroundCamera->setRenderOrder(osg::Camera::PRE_RENDER, 0);
backgroundCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
backgroundCamera->setProjectionMatrixAsOrtho2D(0.0f, 1.0f, 0.0f, 1.0f);
backgroundCamera->getOrCreateStateSet()->setMode(GL_LIGHTING, FALSE);
backgroundCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
backgroundCamera->setClearColor(osg::Vec4(0, 0, 0, 1));

//-------------------------------------------------------------------
// CREATE Camera to display scene
//-------------------------------------------------------------------
sceneCamera->setRenderOrder(osg::Camera::PRE_RENDER, 1);
osg::Node* node = osgDB::readNodeFile("C:\\Software\\OpenSceneGraph-3.6.0\\Models\\cessna.osg");
sceneCamera->addChild(node);
sceneCamera->setViewMatrixAsLookAt(osg::Vec3(100, 0, 0), osg::Vec3(), osg::Vec3(0, 0, 1));
sceneCamera->setClearMask(GL_DEPTH_BUFFER_BIT); //don't clear color buffer please
sceneCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
sceneCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, true, 512, 512);
sceneCamera->setViewport(0, 0, tex->getTextureWidth(), tex->getTextureHeight());

//-------------------------------------------------------------------
// CREATE Camera to display texture
//-------------------------------------------------------------------
finalCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
finalCamera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1.0f, 0, 1.0f));
osg::Geometry* geom = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(1.0f, 0.0f, 0.0f),   osg::Vec3(0.0f, 1.0f, 0.0f));
osg::ref_ptr<osg::Geode> quad = new osg::Geode;
quad->addDrawable(geom);
finalCamera->addChild(quad.get());

// add shaders that draw the texture on the quad
osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(osgDB::readShaderFile("shader.frag"));
program->addShader(osgDB::readShaderFile("shader.vert"));
osg::StateSet* stateset = finalCamera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tex.get());
stateset->setAttributeAndModes(program.get());
stateset->addUniform(new osg::Uniform("myTexture", 0));

osgViewer::Viewer viewer;
viewer.setSceneData(root);
return viewer.run();


Either the background or scene camera can correctly be rendered to the FBO (by commenting the other out) but not both at the same time. Does anyone understand why this doesn't work? When I render to FRAME_BUFFER it works perfectly fine, but I figured since I don't want the texture rendered on screen an FBO would be a more efficient option perhaps?

Thanks!

Cheers,
ivar
Back to top
View user's profile Send private message
Voerman, L.
Guest





PostPosted: Wed Nov 14, 2018 9:37 am    Post subject:
Render multiple PRE_RENDER cameras to FBO
Reply with quote

Hi Ivar,
Your call to Camera::attach is wrong, requesting anything other than 0 for multisampleSamples will cause the setup to fail.


backgroundCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, false, 0, 0);

sceneCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, false, 0, 0);



Laurens.






On Tue, Nov 13, 2018 at 4:41 PM ivar out < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
Hi,

I'm having trouble properly rendering a couple of cameras to an FBO. I have two PRE_RENDER cameras that use a FBO and render to a texture. The color buffer seems to be reset before rendering the second camera though. This is my code:

Code:

osg::Group* root = new osg::Group;

osg::Camera* backgroundCamera = new osg::Camera;
osg::Camera* sceneCamera = new osg::Camera;
osg::Camera* finalCamera = new osg::Camera;

root->addChild(backgroundCamera);
root->addChild(sceneCamera);
root->addChild(finalCamera);

//-----------------------------------------
// create texture to render to
//-----------------------------------------
osg::ref_ptr<osg::Texture2D> tex = new osg:
tex->setSourceType(GL_FLOAT);
tex->setSourceFormat(GL_RGBA);
tex->setInternalFormat(GL_RGBA);
tex->setTextureSize(512, 512);

//-------------------------------------------------------------------
// add geometry to background camera
//-------------------------------------------------------------------
osg::Geometry* background = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(0, 1.0f, 0), osg::Vec3(1.0f, 0, 0));
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));
colors->push_back(osg::Vec4(0, 0, 0, 1.0f));
colors->push_back(osg::Vec4(0, 0, 0, 1.0));
background->setColorArray(colors.get());
background->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
osg::Geode* geode = new osg::Geode;
geode->addChild(background);
backgroundCamera->addChild(geode);

backgroundCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
backgroundCamera->setViewport(0, 0, tex->getTextureWidth(), tex->getTextureHeight());
backgroundCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, true, 512, 512);
backgroundCamera->setRenderOrder(osg::Camera::PRE_RENDER, 0);
backgroundCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
backgroundCamera->setProjectionMatrixAsOrtho2D(0.0f, 1.0f, 0.0f, 1.0f);
backgroundCamera->getOrCreateStateSet()->setMode(GL_LIGHTING, FALSE);
backgroundCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
backgroundCamera->setClearColor(osg::Vec4(0, 0, 0, 1));

//-------------------------------------------------------------------
// CREATE Camera to display scene
//-------------------------------------------------------------------
sceneCamera->setRenderOrder(osg::Camera::PRE_RENDER, 1);
osg::Node* node = osgDB::readNodeFile("C:\Software\OpenSceneGraph-3.6.0\Models\cessna.osg");
sceneCamera->addChild(node);
sceneCamera->setViewMatrixAsLookAt(osg::Vec3(100, 0, 0), osg::Vec3(), osg::Vec3(0, 0, 1));
sceneCamera->setClearMask(GL_DEPTH_BUFFER_BIT); //don't clear color buffer please
sceneCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
sceneCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, true, 512, 512);
sceneCamera->setViewport(0, 0, tex->getTextureWidth(), tex->getTextureHeight());

//-------------------------------------------------------------------
// CREATE Camera to display texture
//-------------------------------------------------------------------
finalCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
finalCamera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1.0f, 0, 1.0f));
osg::Geometry* geom = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(1.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 1.0f, 0.0f));
osg::ref_ptr<osg::Geode> quad = new osg::Geode;
quad->addDrawable(geom);
finalCamera->addChild(quad.get());

// add shaders that draw the texture on the quad
osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(osgDB::readShaderFile("shader.frag"));
program->addShader(osgDB::readShaderFile("shader.vert"));
osg::StateSet* stateset = finalCamera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tex.get());
stateset->setAttributeAndModes(program.get());
stateset->addUniform(new osg::Uniform("myTexture", 0));

osgViewer::Viewer viewer;
viewer.setSceneData(root);
return viewer.run();




Either the background or scene camera can correctly be rendered to the FBO (by commenting the other out) but not both at the same time. Does anyone understand why this doesn't work? When I render to FRAME_BUFFER it works perfectly fine, but I figured since I don't want the texture rendered on screen an FBO would be a more efficient option perhaps?

Thanks!

Cheers,
ivar

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75191#75191





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


------------------
Post generated by Mail2Forum
Back to top
ivar
Newbie


Joined: 10 Oct 2017
Posts: 5

PostPosted: Wed Nov 14, 2018 9:59 am    Post subject:
Reply with quote

Hi Laurens,

You're right, wow that was a silly mistake. Thank you so much, it works now!

Thank you!

Cheers,
ivar
Back to top
View user's profile Send private message
ivar
Newbie


Joined: 10 Oct 2017
Posts: 5

PostPosted: Wed Dec 05, 2018 3:39 pm    Post subject:
Reply with quote

Hi,

One more question, do you have any suggestions on how to achieve multisampling when rendering multiple multisampled cameras to the same texture (as in the code above) ? I haven't been able to find an answer on how to achieve this using OpenSceneGraph yet.

Thank you!

Cheers,
ivar
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 Running multiple (independent) viewer... Aneoshun General 1 Wed May 22, 2019 4:49 pm View latest post
No new posts osgprerender and render to Window Trajce Nikolov NICK General 0 Mon Feb 04, 2019 2:52 pm View latest post
No new posts osgText crash with multiple Viewers ravidavi General 3 Tue Dec 18, 2018 8:50 am View latest post
No new posts Rendering multiple screens njr3 General 1 Wed Oct 31, 2018 8:46 pm View latest post
No new posts CompositeViewer with multiple view, a... Angel General 3 Tue Aug 28, 2018 1:15 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