VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exampleSceneRoot

Table of Contents

Overview

This example shows how to add nodes to the scene graph via a renderer. The application loads a geometry file in OpenFlight format and inserts it directly into the scene. Of particular interest in this example is how to create, initialize and run a VR-Vantage application, and how to gain access to the scene in order to insert a node.

Example details

To create a very basic VR-Vantage application, first instantiate the application, initialize it and finally call run.

DtVrvApplication igApp;
igApp.initialize(argc, argv);

... add some content to the application ...

igApp.run();

The content that this example application displays is a geometry file in OpenFlight format. This application implements a helper function to load the geometry and add the geometry to the scene-graph.

void loadGeometryAndAddToSceneGraph(DtDe& de)

The function accepts a reference to a display engine (DtDe) where the scene renderer can be found. The display engine passed to this function is retrieved directly from the application.

loadGeometryAndAddToSceneGraph(igApp.de());

The geometry to display is in a file on disk. When the application loads the geometry, a small scene-graph branch is created with a transform node at its root so the geometry can be moved in the scene.

osg::ref_ptr<osg::Node> model = osgDB::readNodeFile(dataFile);
osg::ref_ptr<osg::PositionAttitudeTransform> pat =
new osg::PositionAttitudeTransform();
pat->addChild(model.get());

To add the geometry into the VR-Vantage scene, the renderer is retrieved from the display engine and the root-node of the geometry is added to a root of the scene.

DtOsgRenderer& renderer = (DtOsgRenderer&)de.renderer();
renderer.addNodeToRoot(pat, renderer.propRoot());

In this example, the geometry is placed under the 'propRoot' of the renderer. The renderer is responsible for managing how the scene is rendered. As such, the renderer defines several 'root domains' such as Environment, Terrain, Prop, ModelSets, etc. These domains separate the one scene into 'logical' divisions or groupings. The renderer provides access to these different groupings using an API that abstracts the logical purpose of the geometry from the detailed implementation of the scene-graph.

Building the Example

VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.

Running the Example

This example is an application. You can run it by running ./bin/exampleSceneRoot.exe (on Windows) or ./bin/exampleSceneRoot (on Linux). For more information about running examples, please see Running Applications and Examples.

Learn More

Example Source Files


exampleSceneRoot.cxx

/******************************************************************************
** Copyright (c) 2012 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#include <vrvCore/DtDe.h>
#include <osgDB/ReadFile>
#include <osg/PositionAttitudeTransform>
// The name of the geometry file to load.
static const std::string dataFile =
"../data/Vehicles/FixedWing/DC-10_RED_V7.0.flt/DB_DC10_RED_V7.0.medf";
// All VR-Vantage classes are in the same namespace.
using namespace makVrv;
// This is a helper method to load a geometry file and add it to the scene-
// graph. The geometry file is loaded directly with osgDB::loadNodeFile() and
// added as a child to the prop-root node in the renderer.
void loadGeometryAndAddToSceneGraph(DtDe& de)
{
// Load a geometry file from disk and get its root node.
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile(dataFile);
// Create a transform to move the model away from the origin.
// The default observer starts at 0,0,0. Move the transform, 100 units
// away and rotate it to look nice.
osg::ref_ptr<osg::PositionAttitudeTransform> pat =
new osg::PositionAttitudeTransform();
pat->setPosition(osg::Vec3(0,100,0));
pat->setAttitude(osg::Quat(
osg::DegreesToRadians(-10.0f), osg::Vec3(1.0f, 0.0f, 0.0f),
osg::DegreesToRadians(-30.0f), osg::Vec3(0.0f, 1.0f, 0.0f),
osg::DegreesToRadians(150.0f), osg::Vec3(0.0f, 0.0f, 1.0f)));
// Add the root node of the model to the transform.
pat->addChild(model.get());
// Add the transform to the prop-root of the renderer.
DtOsgRenderer& renderer = (DtOsgRenderer&)de.renderer();
renderer.addNodeToRoot(pat, renderer.propRoot());
}
int main(int argc,char** argv)
{
// Create and initialize a VR-Vantage application.
igApp.initialize(argc, argv);
// Load geometry from file and add it to the scene graph.
loadGeometryAndAddToSceneGraph(igApp.de());
// Run the application and render the loaded models.
igApp.run();
return 0;
}

Document ID: Generated on Mon Jul 4 01:00:18 EDT 2016 from SVN revision 166489
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)