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

Table of Contents

Overview

This example shows load an OpenFlight geometry file into a DtOsgArticulationModel and then adds the model to the scene with a DtSceneObject. Of particular interest in this example is how to create and initialize a model and a scene-object.

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 DtOsgArticulatedModel (a derivation of DtModel). A DtModel is a managing container for a single DtModelInstance, providing more attributes about how the DtModelInstance should be rendered. Initialization of a DtModel starts with giving it a model-definition. Unlike the DtModelInstance which can be handed a model-definition identifying the model to load, the DtModel retrieves a model-definition which has been pre-loaded into a definition-manager. Usually DtModelDefinition objects are automatically loaded from disk to populate the definition-manager, however this example demonstrates creating one and registering it with a definition manager in code. This example constructs a DtModelDefinition and registers it in the definition-manager found within the display engine.

DtModelDefinition md("theModel");
md.setParameter("filename", dataFile);

After the model-definitions have been pre-registered within the definition-manager, DtModel objects can access the definitions by name. To load geometry from disk, it is just a matter of telling the DtModel which definition to use. This example uses a DtOsgArticulationModel (derived from DtModel) to load geometry that can be articulated. When given the name of a registered DtModelDefinition, the DtOsgArticulationModel retrieves the definition, internally constructs a DtModelInstance then uses the definition to load the geometry into the model-instance.

DtOsgArticulatedModel* model = new DtOsgArticulatedModel(de, 42);
model->setModelDefinition("theModel");

After loading the geometry, this example manipulates some of the articulation nodes within the geometry.

model->setPartOrientation(turretId,
DtTaitBryan(0.0f, 0.0f, 0.0f),
DtVector(0.25f, 0.0f, 0.0f));
model->setPartOrientation(barrelId,
DtTaitBryan(0.0f, osg::DegreesToRadians(45.0f), 0.0f),
DtVector(0.0f, 0.0f, 0.0f));

Initializing a DtModel only loads the geometry and prepares it for rendering. The DtModel has to be place in the scene to be rendered. To add the model to the scene, a DtSceneObject is used to manage the model. The DtSceneObject is a container class that 'groups' multiple models into a single scene-object. A scene-object is an association of models that can be manipulated as a single unit. Among other features, the DtSceneObject identifies a single position and attitude within the scene for all models associated with it. As such, models are added to (and removed from) the scene object to be inserted to (and removed from) the scene itself. To create and initialize a DtSceneObject it must be constructed with a unique identifier and then associated with a model-set. A model-set is an identifier associating a complete set of visual-types to be rendered within a single channel.

DtSceneObject* sceneObj = new DtSceneObject(de, 3);
sceneObj->setModelSet(0);

Once constructed the DtSceneObject can be positioned and oriented within the scene so that any models added to the object will also be positioned and oriented.

sceneObj->setPosition(0, DtVector(0.0f, 25.0f, 0.0f));
sceneObj->setOrientation(0, DtTaitBryan(
osg::DegreesToRadians(-150.0f),
osg::DegreesToRadians(-30.0f),
osg::DegreesToRadians(-10.0f)));

When a model is added to a DtSceneObject it is automatically inserted into the scene. The DtSceneObject manages retrieving the renderer and identifying which channel rendering the scene to place the model in.

sceneObj->addModel(model, 0);

This example associated the scene-object with the model-set zero (the first model-set), gave arbitrary identifiers to the scene-object and model, and gave an arbitrery visual-type to the model when it was inserted into the scene-object. Of course these arbitrary values have specific meaning in real-world applications.

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

Learn More

Example Source Files


exampleModelArticulation.cxx

/******************************************************************************
** Copyright (c) 2012 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
// This application loads a geometry file into a model instance, locates
// articulation parts by part-id, then sets rotations to each part. As the
// scene is rendered, the parts are continuously articulated. Of particular
// interest in this example application is how to load a geometry file using
// model definitions, how the parts are identified, and how the parts are
// configured for articulation.
#include <iostream>
#include <string>
#include <sstream>
#include <osgSim/DOFTransform>
#include <matrix/vlTaitBryan.h>
#include <matrix/vlVector.h>
#include <vrvCore/DtDe.h>
// The name of the geometry file to load.
static const std::string dataPath("$(DATA_DIR)/Vehicles/Tracked/");
static const std::string M1A2("M1A2/M1A2.medf");
static const std::string M1A2_Abrams("M1A2_Abrams/M1A2_Abrams.medf");
static const std::string M1A2_Desert("M1A2_DESERT_V7.0.flt/DB_M1A2_DESERT_V7.0.medf");
static const std::string dataFile(dataPath + M1A2_Desert);
// The ids of the actuation parts embedded in the model.
// These values are defined in the DIS specification.
static unsigned int turretId(4096);
static unsigned int barrelId(4416);
static unsigned int gun1Id(6016);
static unsigned int gun2Id(5696);
// All VR-Vantage classes are in the same namespace.
using namespace makVrv;
// This helper function populates a DtModelDefinitionManager with a model-
// description for later use. Generally, the manager loads all the model-
// descriptions from the disk, however, this example demonstrates how it is
// populated through code.
void registerModelDefinitions(DtDe& de)
{
// Create a model definition describing the model to load.
// The DtModelDefinition class is used to describe several attributes about
// the model. This example just specifies the name of the geometry file to
// load.
DtModelDefinition md("theModel");
md.setParameter("filename", dataFile);
// Retrieve the DtDeSharedState from the display engine and register the
// definition. The DtDeSharedState contains a model definition manager.
// When registering with the DtDeSharedState, all distributed applications
// are notified to register the same model definition.
}
// This is a helper function to print to the console a list of the parts
// and the names of the DOFTransforms for each part found in a
// DtOsgArticulatedModel object.
void listParts(const DtOsgArticulatedModel& model)
{
// The model holds a map of part-ids to vectors of degree-of-freedom
// transforms. This loop prints out the node-name of the transform for
// each dof associated with a part-id.
DtArticulatedModelParser::PartMap::const_iterator partIter(pm.begin());
DtArticulatedModelParser::PartMap::const_iterator partEndr(pm.end());
for (; partIter != partEndr; ++partIter)
{
std::stringstream partId;
partId << "PartID:" << partIter->first << "\t";
DtArticulatedModelParser::PartTransformList::const_iterator
dofIter(partIter->second.begin());
DtArticulatedModelParser::PartTransformList::const_iterator
dofEndr(partIter->second.end());
for (; dofIter != dofEndr; ++dofIter)
{
std::cout << partId.str() << (*dofIter)->getName() << std::endl;
}
}
}
// This is a helper function to load a geometry file and add it to the scene.
// This function creates a DtSceneObject which is used to position and move
// models around the scene. A DtOsgArticulatedModel is created to load and
// access articulating parts of the model geometry. The DtOsgArticulatedModel
// is added to the DtSceneObject to place the model geometry in to the scene.
void addArticulatedModelToTheScene(DtDe& de)
{
// Create a DtSceneObject to hold the model. DtSceneObjects provide an
// an API to 'group' multiple models together into a single association.
// The scene-object can then be used to manipulate all the associated
// models as a single entity. Among other features like positioning and
// orientating the associated models, the DtSceneObject also provides the
// ability to add models to the scene-graph and channels for rendering.
// This example creates a single DtSceneObject and readies it to position
// its models away from the eye-point in the scene.
DtSceneObject* sceneObj = new DtSceneObject(de, 3);
sceneObj->setModelSet(0);
sceneObj->setPosition(0, DtVector(0.0f, 25.0f, 0.0f));
sceneObj->setOrientation(0, DtTaitBryan(
osg::DegreesToRadians(-150.0f),
osg::DegreesToRadians(-30.0f),
osg::DegreesToRadians(-10.0f)));
// Create an articulated model and set its definition to a preregistered
// definition. A DtModel (in this case a DtOsgArticulatedModel) is a
// managing container for a single model-instance. It provides rendering
// attributes about the model-instance, such as the visualizer-type for
// choosing which channel the model will render in. This example uses a
// DtOsgArticulatedModel to provide access to the various articulation
// nodes within the model-instance. Each model requires a unique id.
// Since this is just an example, arbitrarily pick one for this model.
model->setModelDefinition("theModel");
// For this example list the IDs and names of the parts to the console to
// demonstrate which articulation parts are available for use.
listParts(*model);
// Set the part for the turret to continuously rotate.
model->setPartOrientation(turretId,
DtTaitBryan(0.0f, 0.0f, 0.0f),
DtVector(0.25f, 0.0f, 0.0f));
// Set the part for the barrel to pitch up.
model->setPartOrientation(barrelId,
DtTaitBryan(0.0f, osg::DegreesToRadians(45.0f), 0.0f),
DtVector(0.0f, 0.0f, 0.0f));
// Set the parts for the guns to pitch slightly and continuously rotate.
model->setPartOrientation(gun1Id,
DtTaitBryan(0.0f, osg::DegreesToRadians(5.0f), 0.0f),
DtVector(0.35f, 0.0f, 0.0f));
model->setPartOrientation(gun2Id,
DtTaitBryan(0.0f, osg::DegreesToRadians(4.0f), 0.0f),
DtVector(-0.1f, 0.0f, 0.0f));
// Associate the model with the scene-object. When a model is associated
// with a scene-object, the scene-object automatically inserts the model-
// instance (within the model) into the scene-graph of the renderer in the
// appropriate channel.
sceneObj->addModel(model, 0);
}
int main(int argc,char** argv)
{
// Create and initialize a VR-Vantage application.
igApp.initialize(argc, argv);
// Register the model definition for later use. This process is usually
// performed by reading definition files from disk, however this example
// demonstrates how to create one manually.
registerModelDefinitions(igApp.de());
// Load an articulated-model from file into the scene.
addArticulatedModelToTheScene(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)