Depth and Color buffer attachemnts do not play along together

34 views
Skip to first unread message

Trajce Nikolov NICK

unread,
Mar 28, 2021, 7:49:56ā€ÆAM3/28/21
to osg-...@googlegroups.com
Hi Community,

I am attaching to the main camera DepthBuffer and ColorBuffer as Texture2D - attached is the setup. I also have shader to check the depth buffer - so it is ok as on the image. And I am displaying it on a quad. The depth buffer is still getting me zeros when the colorbuffer is attached, otherwise when it is not, then it works as it should.

Any hints?

mOsgExternalDepth = new osg::Texture2D;
mOsgExternalDepth->setSourceFormat(GL_DEPTH_COMPONENT);
mOsgExternalDepth->setSourceType(GL_FLOAT);
mOsgExternalDepth->setInternalFormat(GL_DEPTH_COMPONENT32F);
mOsgExternalDepth->setTextureWidth(screenWidth);
mOsgExternalDepth->setTextureHeight(screenHeight);
mOsgExternalDepth->setResizeNonPowerOfTwoHint(false);\

mOsgExternalColor = new osg::Texture2D;
mOsgExternalColor->setInternalFormat(GL_RGBA16F_ARB);
mOsgExternalColor->setSourceFormat(GL_RGBA);
mOsgExternalColor->setSourceType(GL_FLOAT);
mOsgExternalColor->setTextureWidth(screenWidth);
mOsgExternalColor->setTextureHeight(screenHeight);
mOsgExternalColor->setResizeNonPowerOfTwoHint(false);

mainCamera->attach(osg::Camera::COLOR_BUFFER, (osg::Texture*)mOsgExternalColor);
mainCamera->attach(osg::Camera::DEPTH_BUFFER, (osg::Texture*)mOsgExternalDepth);

osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setProjectionMatrix(osg::Matrixd::ortho2D(0, screenWidth, 0, screenHeight));
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrixd::identity());
camera->setClearMask(0);
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->setAllowEventFocus(false);

context.getImageGenerator()->getViewer()->getView(0)->getSceneData()->asGroup()->addChild(camera);

osg::ref_ptr<osg::Geometry> quad = osg::createTexturedQuadGeometry(osg::Vec3(0, 0, 0), osg::Vec3(screenWidth/2, 0, 0), osg::Vec3(0, screenHeight/2, 0));
quad->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
quad->getOrCreateStateSet()->setTextureAttributeAndModes(0, mOsgExternalDepth);

const std::string vertShader = ""\
"void main() "\
"{ "\
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; "\
" gl_TexCoord[0] = gl_TextureMatrix[0] *gl_MultiTexCoord0;"\
"}";

const std::string fragShader = ""\
"uniform sampler2D texture0; "\
"void main() "\
"{ "\
" float d = texture2D( texture0, gl_TexCoord[0].xy ).x; "\
" gl_FragColor = vec4(d, d, d, 1); "\
"}";

osg::ref_ptr<osg::Shader> vert = new osg::Shader(osg::Shader::VERTEX, vertShader);
osg::ref_ptr<osg::Shader> frag = new osg::Shader(osg::Shader::FRAGMENT, fragShader);

osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(vert);
program->addShader(frag);

quad->getOrCreateStateSet()->setAttributeAndModes(program);
quad->getOrCreateStateSet()->addUniform(new osg::Uniform(osg::Uniform::SAMPLER_2D, "texture0", 0));
osg::ref_ptr<osg::Geometry> quadColor = osg::createTexturedQuadGeometry(osg::Vec3(screenWidth / 2, 0, 0), osg::Vec3(screenWidth/2 , 0, 0), osg::Vec3(0, screenHeight/2, 0));
quadColor->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
quadColor->getOrCreateStateSet()->setTextureAttributeAndModes(0, mOsgExternalColor);

camera->addChild(quadColor);
camera->addChild(quad);

--
trajce nikolov nick
Both Attached.jpg
Only Depth Attached.jpg

Trajce Nikolov NICK

unread,
Mar 28, 2021, 10:24:22ā€ÆAM3/28/21
to osg-...@googlegroups.com
Attached is the minimal code that reproduce the problem

Thanks a bunch as always!

Nick
--
trajce nikolov nick
osgviewer.cpp

Trajce Nikolov NICK

unread,
Mar 29, 2021, 10:45:27ā€ÆPM3/29/21
to osg-...@googlegroups.com
Nonone on this? Robert?

Thank you
Nick
--
trajce nikolov nick

Glenn Waldron

unread,
Mar 30, 2021, 8:07:54ā€ÆAM3/30/21
to OpenSceneGraph Users
Nick,
Pretty sure you can only attach one texture to a FRAME_BUFFER render target. And in that case, it is just copying the framebuffer to your texture after rendering it. Consider using a FRAME_BUFFER_OBJECT instead.

Trajce Nikolov NICK

unread,
Mar 30, 2021, 10:10:43ā€ÆAM3/30/21
to osg-...@googlegroups.com
Thanks Glenn. How this will work with setting the render target implementation to the main view camera? I am trying to avoid multiple cameras and I am not sure this is doableĀ 

--
You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osg-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/64c1877d-50c2-4fad-801d-41cd63529376n%40googlegroups.com.

Trajce Nikolov NICK

unread,
Mar 30, 2021, 11:09:24ā€ÆAM3/30/21
to osg-...@googlegroups.com
Actually it worked after rethinking it. Thanks Glenn
depth_and_color_buffers.jpg
Reply all
Reply to author
Forward
0 new messages