OSG learning: scene graphics management (4) -- multi view camera rendering

The following is from:

1. Programming Guide for OpenSceneGraph 3D rendering engine, Xiao Peng, Liu gendai, Xu Mingliang, Tsinghua University Press
2. Design and practice of OpenSceneGraph 3D rendering engine, Wang Rui, Qian Xuelei, Tsinghua University Press

3. Own summary

After creating a C + + project, you first need to configure the OSG environment. See the specific steps OSG learning: OSG+VS2017 compilation and operation under WIN10 system Step 6: test the new OSG project.


// stdafx.h

#include <osg/Node>
#include <osg/Geode>  
#include <osg/Geometry>
#include <osg/Group>

#Include < OSG / matrixtransform > / / matrix class of mobile node, the most commonly used class of mobile node. Follow up and rotation control node
#Include < OSG / positionattitudetransform > / / position transformation node class, which provides position transformation, size scaling, setting of origin position and transformation of coordinate system of the model
#Include < OSG / Camera > / / the camera node manages the model view matrix in OSG. The camera management is mainly realized through various transformations

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgUtil/Optimizer>
#include <osgViewer/Viewer>
#Include < osgviewer / compositeviewer > / / manage multiple views, manage and synchronize multiple views

//View window
osg::ref_ptr<osgViewer::Viewer> MultipleView(osg::ref_ptr<osg::Node> node, int i)
	int xoffset = 50;
	int yoffset = 200;

	//Set drawing environment properties
	osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits();
	//The abscissa positions of the two view windows are different, or they will overlap
	if (i == 1)
		traits->x = xoffset + 0;
		traits->x = xoffset + 600;
	traits->y = yoffset + 0;
	traits->width = 600;
	traits->height = 480;
	traits->windowDecoration = true;
	traits->doubleBuffer = true;
	traits->sharedContext = 0;
	//Create view
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
	//Create camera
	osg::ref_ptr<osg::Camera> camera = viewer->getCamera();

	//Create drawing environment properties
	osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());	
	//Setting up the drawing environment

	//Setting Viewports
	camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
	//Set buffer
	GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
	//Set buffer (set which buffer to draw at the beginning of each frame)

	//Set scene data

	return viewer.get();

int main()
	//Read the model as the node of two views
	osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");
	osg::ref_ptr<osg::Node> cessna = osgDB::readNodeFile("cessna.osg");

	//Optimize scene data
	osgUtil::Optimizer optimizer;

	//Create two views
	osg::ref_ptr<osgViewer::Viewer> viewer1 = MultipleView(cow, 1);
	osg::ref_ptr<osgViewer::Viewer> viewer2 = MultipleView(cessna, 2);

	//Create a CompositeViewer object
	osg::ref_ptr<osgViewer::CompositeViewer> viewer = new osgViewer::CompositeViewer();
	//Add view

	return viewer->run();

Download the complete project OSG? 19? Multipleviewmultiplewindow

Tags: Mobile Programming Windows

Posted on Wed, 29 Jan 2020 07:14:10 -0800 by mschrank