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.
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:
-
Run ./bin/dcgen.exe. The Distributed Code Generator opens.
-
Choose Files -> Open. An Open dialog box opens.
-
Select ./examples/exampleDistributedObject/DtExampleObject.ocdx.
-
Examine the object class definition and compare it to the object code to see how functions for the agent were chosen.
-
Choose Build -> Set Output Directory.
-
Specify a directory for the output files. Be careful not to overwrite the files shipped with VR-Vantage.
-
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:
myAgent = DtExampleObjectAgent::create(myAgentManager);
myAgent->loadModel("../data/Lifeforms/Animals/cow.osg");
Drivers get ticked every frame. In our driver's tick method, we will tell the agent to rotate to a certain degree:
myAgent->setRotationAngle(myRotationDegree);
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/exampleDistributedObject.exe (on Windows) or ./bin/exampleDistributedObject (on Linux). For more information about running examples, please see Running Applications and Examples.
Example Source Files
DtExampleObject.h
#ifndef DtExampleObject_H_
#define DtExampleObject_H_
#include <vrvCore/DtDe.h>
#include <vrvCore/DtUniqueID.h>
#include <osg/PositionAttitudeTransform>
#include <string>
namespace makVrv
{
class DtExampleObject
{
public:
protected:
osg::ref_ptr<osg::PositionAttitudeTransform>
myPat;
};
}
#endif
DtExampleObject.cxx
#include <vrvOsg/DtOsgRenderer.h>
#include <vrvCore/DtModelSetRealizationManager.h>
#include <vrvCore/DtDeSharedState.h>
#include <vrvOsg/DtOsgRenderer.h>
#include <vrvOsg/DtChannelSpecificGroup.h>
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>
#include <osg/Geode>
namespace makVrv
{
: myDe(de)
, myPat(0)
{
}
DtExampleObject::~DtExampleObject()
{
}
void DtExampleObject::loadModel( const std::string& filename )
{
osg::ref_ptr<osg::Node> modelNode = osgDB::readNodeFile(filename);
myPat = new osg::PositionAttitudeTransform();
myPat->addChild(modelNode.get());
myPat->setPosition(osg::Vec3(0,100,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)
{
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)));
}
}
exampleDistributedObject.cxx
#include <vrvCore/DtVrvApplication.h>
#include <vrvCore/DtDe.h>
#include <vrvCore/DtAgentManager.h>
#include <vrvCore/DtDriverManager.h>
#include <vrvCore/DtDriver.h>
#include "DtExampleObjectAgent.hpp"
using namespace makVrv;
{
public:
, myAgent(0)
, myRotationDegree(0.0)
{
}
{
if(myAgent)
delete myAgent;
myAgent = 0;
}
virtual const std::string& className() const
{
static std::string name = "DtExampleDriver";
return name;
}
virtual bool onStart()
{
myAgent = DtExampleObjectAgent::create(myAgentManager);
myAgent->loadModel("../data/Lifeforms/Animals/cow.osg");
return true;
}
virtual bool onStop()
{
if(myAgent)
delete myAgent;
myAgent = 0;
return true;
}
virtual bool onTick()
{
myAgent->setRotationAngle(myRotationDegree);
myRotationDegree += 0.15;
if(myRotationDegree > 360.0)
{
myRotationDegree = 0.0;
}
return true;
}
protected:
DtExampleObjectAgent* myAgent;
double myRotationDegree;
};
int main(int argc,char** argv)
{
application.initialize(argc, argv);
application.de().agentManager());
application.de().driverManager().addDriver(driver);
application.de().driverManager().startDriver(driver);
application.run();
}