[osg-users] Viewer slaves and RTTs

Robert Osfield robert.osfield at gmail.com
Sat Feb 18 09:12:01 PST 2017


Hi Johny,

It's a bit painful to ready example code that mixes global C pointers
with reference counting objects.  There is very strong chance that'll
you be leaking objects or end up with dangling pointers.  I strongly
recommend against doing this even in an example as it not only sets a
bad example to yourself and potential problems down the line, it also
gives others in the community, who might read your code looking for
inspiration, and poor start as they might not realize that it's a
really bad programming practice.

As for you problem, reviewing the code the item that jumps out at me
is setting osg::State to use uniforms for the modelview and project
matrix and aliasing of the vertex attributes but you never provide any
shaders to utilize them., so you are disabling part of the fixed
function pipeline but not privide any replacement, so if you don;t see
what you are expecting it's not too surprising  The OSG doesn't
provide these shaders for you and will be default use the fixed
function pipeline.  These setting in osg::State are only appropriate
for GLES2 or GL3 core profile where shaders are required and no built
in uniform or vertex attributes are provided.

I would recommend you remove these lines of code and just rely upon
the fixed function pipeline.  Once you've got a bit further with you
leaning about OpenGL/shaders/OSG then you can start thinking about
re-enabling them.

Robert



On 18 February 2017 at 14:36, Johny Canes <psijsma at gmail.com> wrote:
> Hi,
>
> Here is a test. It loads the image but I can't get either the mainCamera color buffer (gTexture) to render on the quad or the OpenSceneGraph-Data\Images\forestRoof.png.
>
> Ideally, what I wanted is for mainCamera to not(!) draw on the screen, only inside of gTexture. However, in other tests setting the rendertargetimp. to FBO will produce black everything.
>
>
> Code:
> #include "stdafx.h"
>
> #include <osgViewer/Viewer> // includes a lot of things for us
> #include <osg/Texture2D>
> #include <osgDB/ReadFile>
> #include <osg/Material>
>
>
> osg::ref_ptr<osg::GraphicsContext> gGc;
> osgViewer::Viewer* gViewer;
> osg::Camera* gCamera;
> osg::Texture* gTexture;
>
> osg::Camera* gOrtho;
> osg::ref_ptr<osg::Geode> gQuad;
>
> osg::Group* gRoot;
>
> void funcSlave() {
>         osg::Texture2D* texture2D = new osg::Texture2D;
>         texture2D->setTextureSize(1024, 1024);
>         texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
>         texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
>         gTexture = texture2D;
>
>         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
>         traits->x = 300;
>         traits->y = 100;
>         traits->width = 1024;
>         traits->height = 768;
>         traits->windowDecoration = true;
>         traits->doubleBuffer = true;
>         traits->sharedContext = 0;
>         traits->samples = 4; // MSAA
>         traits->vsync = false;
>
>         gGc = osg::GraphicsContext::createGraphicsContext( traits.get() );
>         gGc->getState()->setUseModelViewAndProjectionUniforms( true );
>         gGc->getState()->setUseVertexAttributeAliasing( true );
>
>         GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
>
>         osg::Camera* camera = gViewer->getCamera(); // alternatively: new osg::Camera( viewer->getCamera() );
>         camera->setName( "Main" );
>         camera->setGraphicsContext( gGc.get() );
>
>         camera->setClearColor( osg::Vec4f(1, 0, 0, 1) );
>         camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
>
>         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
>
>         camera->setDrawBuffer( buffer );
>         camera->setReadBuffer( buffer );
>         camera->setRenderOrder( osg::Camera::RenderOrder::PRE_RENDER );
>         //camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
>
>         camera->attach( osg::Camera::COLOR_BUFFER, gTexture );
>
>         gCamera = camera;
>
> }
>
> void funcOrtho() {
>         int Width = 512, Height = 512;
>
>         osg::Camera* ortho = gOrtho = new osg::Camera;
>         ortho->setName( "Ortho" );
>
>         ortho->setGraphicsContext( gGc.get() );
>
>         ortho->setClearColor( osg::Vec4f(1,0,1,1) );
>         ortho->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
>
>         ortho->setProjectionMatrix(osg::Matrix::ortho2D(0, Width, 0, Height));
>
>         //ortho->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
>
>         ortho->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
>         ortho->setViewMatrix( osg::Matrix::identity() );
>         ortho->setRenderOrder( osg::Camera::POST_RENDER );
>
>         ortho->setViewport(0, 0, Width, Height);
>
>         auto qaud = osg::createTexturedQuadGeometry(osg::Vec3(), osg::Vec3(Width, 0.0, 0.0), osg::Vec3(0.0, Height, 0.0));
>
>         auto quad = gQuad = new osg::Geode;
>         quad->addDrawable( qaud );
>
>         auto image = osgDB::readImageFile("Images/forestRoof.png");
>         if (!image)
>                 OSG_NOTICE<<"no image";
>
>         auto texture = new osg::Texture2D( image );
>
>         quad->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture );
>
>         osg::ref_ptr<osg::Material> m = new osg::Material;
>         m->setColorMode(osg::Material::DIFFUSE);
>         m->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1));
>
>         quad->getOrCreateStateSet()->setAttributeAndModes( m.get(), osg::StateAttribute::ON);
>
>         // Can't get it to brighten up...
>
>         ortho->addChild( quad );
>
>         gViewer->addSlave(ortho, osg::Matrix(), osg::Matrix(), false);
>
>         // ortho->setPreDrawCallback( new Postprocesser( texture ) );
> }
>
> int main(int argc, char **argv) {
>
>
>         gViewer = new osgViewer::Viewer;
>         gRoot = new osg::Group;
>
>         funcSlave();
>
>         osg::ref_ptr<osg::Node> cessna = osgDB::readRefNodeFile("cessna.osgt");
>
>         if (!cessna) {
>         OSG_NOTICE<<"Cannot not find model 'cessna.osg' to render"<<std::endl;
>         return 404;
>     }
>
>         gRoot->addChild( cessna.get() );
>
>         funcOrtho();
>
>         //gViewer->getLight()->setLightNum(0);
>         //gViewer->setLightingMode(osgViewer::View::LightingMode::NO_LIGHT);
>
>         gViewer->setSceneData( gRoot );
>
>     //gRoot->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
>
>         gViewer->realize();
>
>         gViewer->run();
> };
>
>
>
> Thank you!
>
> Cheers,
> Johny
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=70253#70253
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



More information about the osg-users mailing list