VR-Vantage
Loading...
Searching...
No Matches
exampleDistributedObject

Overview

This example shows how to create an agent for an object and create an instance of the agent in a driver. It creates a distributed object, DtExampleObject, that loads a model and rotates it using an agent.

Expected Result

Distributed Object Result

Example details

To fully appreciate this example, use the Distributed Code Generator to generate the agent classes.

The object is defined as follows:

class DtExampleObject
{
public:
DtExampleObject(DtDe& de, DtUniqueID id);
virtual ~DtExampleObject();
void loadModel(const std::string& filename);
void setRotationAngle(double rotationAngleInDegrees);

To generate the agent files:

  1. Run ./bin64/dcgen.exe. The Distributed Code Generator opens.
  2. Choose Files -> Open. An Open dialog box opens.
  3. Select ./examples/exampleDistributedObject/DtExampleObject.ocdx.
  4. Examine the object class definition and compare it to the object code to see how functions for the agent were chosen.
  5. Choose Build -> Set Output Directory.
  6. Specify a directory for the output files. Be careful not to overwrite the files shipped with VR-Vantage.
  7. Choose Build -> Generate Files. (You can also generate files from the command line or from within your IDE. For details, please see VR-Vantage Developer's Guide.)

To add the agent to the driver:

Drivers get ticked every frame. In our driver's tick method, we will tell the agent to rotate to a certain degree:

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 a plugin. You can run it by running ./bin64/exampleDistributedObject_stealth.bat (on Windows) or ./bin64/exampleDistributedObject_stealth.sh (on Linux). For more information about running examples, please see Running Applications and Examples.

Example Source Files


DtExampleObject.h

// /******************************************************************************
// ** Copyright (c) 2019 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#ifndef DtExampleObject_H_
#define DtExampleObject_H_
#include <vrvCore/DtDe.h>
#include <osg/PositionAttitudeTransform>
#include <string>
namespace makVrv
{
class DtExampleObject
{
public:
DtExampleObject(DtDe& de, DtUniqueID id);
virtual ~DtExampleObject();
void loadModel(const std::string& filename);
void setRotationAngle(double rotationAngleInDegrees);
protected:
DtDe& myDe;
osg::ref_ptr<osg::PositionAttitudeTransform> myPat;
};
}
#endif
Contains makVrv::DtDe class.
Contains makVrv::DtUniqueID type.
void setRotationAngle(double rotationAngleInDegrees)
Set the new rotation angle of the model.
DtDe & myDe
Definition DtExampleObject.h:36
void loadModel(const std::string &filename)
Load the provided model.
DtExampleObject(DtDe &de, DtUniqueID id)
The Constructor for a distributed object must take a DtDe and a Unique Id.
virtual ~DtExampleObject()
Virtual DTOR.
osg::ref_ptr< osg::PositionAttitudeTransform > myPat
Definition DtExampleObject.h:37
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16

DtExampleObject.cxx

// /******************************************************************************
// ** Copyright (c) 2020 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>
#include <osg/Geode>
namespace makVrv
{
DtExampleObject::DtExampleObject( DtDe& de, DtUniqueID id )
: myDe(de)
, myPat(0)
{
}
DtExampleObject::~DtExampleObject()
{
if (myPat)
{
myPat->removeChild(0,1);
}
myPat = 0;
}
void DtExampleObject::loadModel( const std::string& filename )
{
// Add a model...
osg::ref_ptr<osg::Node> modelNode = osgDB::readNodeFile(filename);
// Create a transform for the model.
myPat = new osg::PositionAttitudeTransform();
// Add the model to the transform.
myPat->addChild(modelNode.get());
// The observer starts at 0,0,0. Move the transform, 45 meters away.
myPat->setPosition(osg::Vec3(0.0,45.0,-5.0));
const DtModelSetRealizationManager& manager = myDe.sharedState().modelSetRealizationManager();
DtModelSetRealizationManager::ModelSetRealizations::const_iterator iter = manager.modelSetRealizations().begin();
DtModelSetRealizationManager::ModelSetRealizations::const_iterator end = manager.modelSetRealizations().end();
while (iter != end)
{
if (iter->second)
{
// Add the boat to the root of the scene graph.
((DtOsgRenderer&)myDe.renderer()).addNodeToRoot(myPat.get(),
DtOsgRenderer::visualizerTypeRoot( iter->first, 0 ) );
}
++iter;
}
}
void DtExampleObject::setRotationAngle( double rotationAngleInDegrees )
{
myPat->setAttitude(osg::Quat(
osg::DegreesToRadians(0.0), osg::Vec3d(1.0, 0.0, 0.0),
osg::DegreesToRadians(0.0), osg::Vec3d(0.0, 1.0, 0.0),
osg::DegreesToRadians(rotationAngleInDegrees), osg::Vec3d(0.0, 0.0, 1.0)));
}
}
Contains makVrv::DtChannelSpecificGroup class.
Contains makVrv::DtDeSharedState class.
Contains makVrv::DtModelSetRealizationManager class.
Contains makVrv::DtOsgRenderer class.

exampleDistributedObjectPlugin.cxx

// /******************************************************************************
// ** Copyright (c) 2024 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#include <vrvCore/DtDe.h>
// The header for the driver generated by the Distributed Object Compiler
#include <boost/bind/bind.hpp>
// Use the VR-Vantage namespace. All classes in VR-Vantage are in this namespace.
using namespace makVrv;
static vrvSignalsLib::connection postInitializeConnection;
// This driver creates and owns the example driver.
class DtExampleDriver : public DtDriver
{
public:
: DtDriver(am,"DtExampleDriver")
, myAgent(0)
, myRotationDegree(0.0)
{
setAutoStart(true);
}
virtual ~DtExampleDriver()
{
if(myAgent)
delete myAgent;
myAgent = 0;
}
virtual const std::string& className() const
{
static std::string name = "DtExampleDriver";
return name;
}
virtual bool onStart()
{
// Create the agent
// Load the model
std::string sharedDataPath = myAgentManager.de().dePathConfiguration().sharedDataPath();
myAgent->loadModel( sharedDataPath + "/ModelData/Vehicles/Surface/FishingVessel/FishingVessel.medf");
return true;
}
virtual bool onStop()
{
if (myAgent)
{
delete myAgent;
myAgent = 0;
}
return true;
}
virtual bool onTick()
{
// Rotate the object a little bit every frame.
myAgent->setRotationAngle(myRotationDegree);
myRotationDegree += 0.15;
if(myRotationDegree > 360.0)
{
myRotationDegree = 0.0;
}
return true;
}
protected:
virtual void slot_displayEngineAdded(const DtDeRecord& igRecord)
{
// When a new display engine is added, we need to restart the driver so that everything is
// correctly created on the new DE.
if (DtDriver::myStartedFlag)
{
stop();
start();
}
}
DtExampleObjectAgent* myAgent;
double myRotationDegree;
};
void init(DtDe& de)
{
// Ensure that init only gets called once
// (not strictly necessary here, but this is good practice in general)
// We only want to create the example driver if we're running in master mode:
if (de.isInMasterMode())
{
// The driver must be created after the DE's initialization is finished,
// to make sure all the objects it uses exist.
postInitializeConnection = de.signal_postInitialize.connect(boost::bind(
&loadDriver, &de));
}
}
{
// Setup the plugin. Normally, the init function and functionality
// should be in its own library.
init(*de);
return true;
}
void loadDriver(DtDe* de)
{
// We're done with the signal, so disconnect from it
postInitializeConnection.disconnect();
// Create the driver that will manage the DtExampleObjectAgents
de->agentManager());
// After the driver is added to the display engine below, the DE will manage
// its memory. The driver will be started automatically when added since we
// set the autoStart property in its CTOR
de->driverManager().addDriver(driver);
}
Contains makVrv:DtAgentManager class.
#define DT_DE_INIT_ONCE(de)
This macro is called by modules in their init() function. This macro ensures that the module is only ...
Definition DtDe.h:30
Contains makVrv:DtDriver class.
Contains makVrv:DtMasterScene class.
Contains makVrv::DtOsgEmptySceneLogo class.
Definition DtExampleDriver.h:20
virtual ~DtExampleDriver()
Virtual DTOR.
DtExampleDriver(makVrv::DtAgentManager &am)
CTOR.
virtual bool onTick()
virtual const std::string & className() const
virtual bool onStop()
virtual bool onStart()
virtual void slot_displayEngineAdded(const makVrv::DtDeRecord &igRecord)
The DtAgentManager manages the distributed scene object framework. This includes the objects and upda...
Definition DtAgentManager.h:39
DtDe & de()
Get the scene manager's Ig object.
Definition DtAgentManager.h:107
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer,...
Definition DtDe.h:77
DtAgentManager & agentManager()
Get the scene manager.
virtual bool isInMasterMode() const
Returns true if the DtDe is in master mode.
virtual const DtDePathConfiguration & dePathConfiguration() const
Return the DtDePathConfiguration.
virtual DtDriverManager & driverManager()
The DtMasterScene accessor. The function will throw before the initialize function is called.
boost::signalslib::signal< void()> signal_postInitialize
Definition DtDe.h:535
virtual std::string sharedDataPath() const
The DtDriver is an abstract class. Developers derive from this to create logic that "directs" things ...
Definition DtDriver.h:32
DtAgentManager & myAgentManager
Definition DtDriver.h:190
virtual void setAutoStart(bool autoStart)
Set whether this driver automatically starts if it is persistent.
void start()
Start the driver. Called by the Master Scene. Calls installAccessories().
void stop()
Stop the driver. Called by the Master Scene. Calls uninstallAccessories().
void addDriver(DtDriver *driver)
Add a host driver to the master scene. The host is now owned by the Master Scene.
static DtExampleObjectAgent * create(makVrv::DtAgentManagerInterface &sceneInterface, bool bDistribute=true)
Static helper creator.
static void setEnabled(bool enabled)
This global method will enable or disable the use of an empty scene overlay for all uses of this clas...
DT_DLL_EXAMPLEACCESSORY void init(makVrv::DtDe &de)
void loadDriver(DtDe *de)
DT_DE_PLUGIN_EXPORT_MACRO bool initDeModule(makVrv::DtDe *de)