<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi Ivar,</div><div>Your call to Camera::attach is wrong, requesting anything other than 0 for multisampleSamples will cause the setup to fail.</div><div><br></div><div dir="ltr">backgroundCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, false, 0, 0);<br></div><div dir="ltr">sceneCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, false, 0, 0);<br></div><div dir="ltr"><br></div><div>Laurens.</div><div dir="ltr"><br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 13, 2018 at 4:41 PM ivar out <<a href="mailto:ivarout@gmail.com">ivarout@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
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:<br>
<br>
Code:<br>
<br>
osg::Group* root = new osg::Group;<br>
<br>
osg::Camera* backgroundCamera = new osg::Camera;<br>
osg::Camera* sceneCamera = new osg::Camera;<br>
osg::Camera* finalCamera = new osg::Camera;<br>
<br>
root->addChild(backgroundCamera);<br>
root->addChild(sceneCamera);<br>
root->addChild(finalCamera);<br>
<br>
//-----------------------------------------<br>
// create texture to render to <br>
//-----------------------------------------<br>
osg::ref_ptr<osg::Texture2D> tex = new osg:<br>
tex->setSourceType(GL_FLOAT);<br>
tex->setSourceFormat(GL_RGBA);<br>
tex->setInternalFormat(GL_RGBA);<br>
tex->setTextureSize(512, 512);<br>
<br>
//-------------------------------------------------------------------<br>
// add geometry to background camera<br>
//-------------------------------------------------------------------<br>
osg::Geometry* background = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(0, 1.0f, 0), osg::Vec3(1.0f, 0, 0));<br>
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;<br>
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));<br>
colors->push_back(osg::Vec4(1.0, 1.0, 1.0, 1.0));<br>
colors->push_back(osg::Vec4(0, 0, 0, 1.0f));<br>
colors->push_back(osg::Vec4(0, 0, 0, 1.0));<br>
background->setColorArray(colors.get());<br>
background->setColorBinding(osg::Geometry::BIND_PER_VERTEX);<br>
osg::Geode* geode = new osg::Geode;<br>
geode->addChild(background);<br>
backgroundCamera->addChild(geode);<br>
<br>
backgroundCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);<br>
backgroundCamera->setViewport(0, 0, tex->getTextureWidth(), tex->getTextureHeight());<br>
backgroundCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, true, 512, 512);<br>
backgroundCamera->setRenderOrder(osg::Camera::PRE_RENDER, 0);<br>
backgroundCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);<br>
backgroundCamera->setProjectionMatrixAsOrtho2D(0.0f, 1.0f, 0.0f, 1.0f);<br>
backgroundCamera->getOrCreateStateSet()->setMode(GL_LIGHTING, FALSE);<br>
backgroundCamera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);<br>
backgroundCamera->setClearColor(osg::Vec4(0, 0, 0, 1));<br>
<br>
//-------------------------------------------------------------------<br>
// CREATE Camera to display scene<br>
//-------------------------------------------------------------------<br>
sceneCamera->setRenderOrder(osg::Camera::PRE_RENDER, 1);<br>
osg::Node* node = osgDB::readNodeFile("C:\\Software\\OpenSceneGraph-3.6.0\\Models\\cessna.osg");<br>
sceneCamera->addChild(node);<br>
sceneCamera->setViewMatrixAsLookAt(osg::Vec3(100, 0, 0), osg::Vec3(), osg::Vec3(0, 0, 1));<br>
sceneCamera->setClearMask(GL_DEPTH_BUFFER_BIT); //don't clear color buffer please<br>
sceneCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);<br>
sceneCamera->attach(osg::Camera::COLOR_BUFFER, tex.get(), 0, 0, true, 512, 512);<br>
sceneCamera->setViewport(0, 0, tex->getTextureWidth(), tex->getTextureHeight());<br>
<br>
//-------------------------------------------------------------------<br>
// CREATE Camera to display texture<br>
//-------------------------------------------------------------------<br>
finalCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);<br>
finalCamera->setProjectionMatrix(osg::Matrix::ortho2D(0, 1.0f, 0, 1.0f));<br>
osg::Geometry* geom = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(1.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 1.0f, 0.0f));<br>
osg::ref_ptr<osg::Geode> quad = new osg::Geode;<br>
quad->addDrawable(geom);<br>
finalCamera->addChild(quad.get());<br>
<br>
// add shaders that draw the texture on the quad<br>
osg::ref_ptr<osg::Program> program = new osg::Program;<br>
program->addShader(osgDB::readShaderFile("shader.frag"));<br>
program->addShader(osgDB::readShaderFile("shader.vert"));<br>
osg::StateSet* stateset = finalCamera->getOrCreateStateSet();<br>
stateset->setTextureAttributeAndModes(0, tex.get());<br>
stateset->setAttributeAndModes(program.get());<br>
stateset->addUniform(new osg::Uniform("myTexture", 0));<br>
<br>
osgViewer::Viewer viewer;<br>
viewer.setSceneData(root);<br>
return viewer.run();<br>
<br>
<br>
<br>
<br>
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? <br>
<br>
Thanks!<br>
<br>
Cheers,<br>
ivar<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=75191#75191" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=75191#75191</a><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org" target="_blank">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</blockquote></div>