[osg-users] Viewer slaves and RTTs

Johny Canes psijsma at gmail.com
Sat Feb 18 06:36:31 PST 2017


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








More information about the osg-users mailing list