VR-Vantage 2.3 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exampleModelInstance

Table of Contents

Overview

This example shows how to load an OpenFlight geometry file into a DtModelInstance and then add it to the scene. Of particular interest in this example is how to create and initialize a model instance.

Example details

To create a 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 geometry in a DtModelInstance. This application implements a helper function to create and load a DtModelInstance and add the geometry to the scene-graph.

DtModelInstance* loadModelInstanceToTheSceneGraph(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.

DtModelInstance* mi = loadModelInstanceToTheSceneGraph(igApp.de());

To load a geometry file into a DtModelInstance, the model-instance must first be created, then 'realized' with a model-definition. This example uses a DtOsgSimpleModelInstance (derived from DtModelInstance) to load the geometry. The DtModelDefinition holds information on how to load and initialize the geometry. This example only use the DtModelDefinition to specify the geometry file to load.

DtModelDefinition md("theModel");
md.setParameter("filename", dataFile);
DtOsgSimpleModelInstance* mi = new DtOsgSimpleModelInstance(de);
if (! mi->realize(md))
{
delete mi;
return NULL;

This example places the DtModelInstance geometry under a transform node so the model can be moved in the scene.

osg::ref_ptr<osg::PositionAttitudeTransform> pat =
new osg::PositionAttitudeTransform();
pat->addChild(mi->rootNode());

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

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

This example chose to place the geometry under the 'propRoot' of the renderer. Depending on the purpose of the model it could have been placed under one of the other root domains; Environment, Terrain, ModelSets, etc.

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/exampleModelInstance.exe (on Windows) or ./bin/exampleModelInstance (on Linux). For more information about running examples, please see Running Applications and Examples.

Learn More

Example Source Files


exampleModelInstance.cxx

/******************************************************************************
** Copyright (c) 2012 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
// This application loads a geometry file in OpenFlight format into a
// makVrv::DtModelInstance and then adds the geometry to the scene.
// Of particular interest in this example is how to create and initialize
// model-instance.
#include <vrvCore/DtDe.h>
#include <osg/PositionAttitudeTransform>
// The name of the geometry file to load.
static const std::string dataFile =
"$(DATA_DIR)/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 function to load a geometry file and add it to the scene-
// graph. A model-definition is first given to a model-instance telling it
// which geometry file is to be loaded. Then the realize() function is called
// on the model-instance to load the file from disk. Last the root node from
// the model-instance is added to the scene-graph.
DtModelInstance* loadModelInstanceToTheSceneGraph(DtDe& de)
{
// Create a model definition describing the model to load.
// The DtModelDefinition class is used to describe several attributes about
// the model. For this example we just specify the name of the geometry
// file to load.
DtModelDefinition md("theModel");
md.setParameter("filename", dataFile);
// Create a model instance and load a file from the model definition.
// In this example we use a DtOsgSimpleModelInstance as the type of model-
// instance that will be added to the scene. Since we are creating this
// object directly without the use of a factory, we're required to call
// the realize() function to actually load the model.
if (! mi->realize(md))
{
delete mi;
return NULL;
}
// 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(mi->rootNode());
// Add the transform to the prop-root of the renderer.
DtOsgRenderer& renderer = (DtOsgRenderer&)de.renderer();
renderer.addNodeToRoot(pat, renderer.propRoot());
return mi;
}
int main(int argc,char** argv)
{
// Create and initialize a VR-Vantage application.
igApp.initialize(argc, argv);
// Load a model-instance from file and add it to the scene graph.
// Get a handle to the model-instance for safe keeping.
DtModelInstance* mi = loadModelInstanceToTheSceneGraph(igApp.de());
if (! mi)
return 1;
// Run the application and render the loaded models.
igApp.run();
return 0;
}


Copyright © 2005-2018 VT MAK. All Rights Reserved (www.mak.com)