![]() |
VR-Vantage 1.4.1 API Class Documentation
|
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:
To add the agent to the driver:
// Create the agent myAgent = DtExampleObjectAgent::create(myAgentManager); // Load the model 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);
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.
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.
// /****************************************************************************** // ** Copyright (c) 2008 MAK Technologies, Inc. // ** All rights reserved. // ******************************************************************************/ // /****************************************************************************** // ** $RCSfile: DtExampleObject.h,v $ $Revision: 1.4 $ $State: Exp $ // ******************************************************************************/ #ifndef DtExampleObject_H_ #define DtExampleObject_H_ #include <vrvCore/DtDe.h> #include <vrvCore/DtUniqueID.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
// /****************************************************************************** // ** Copyright (c) 2008 MAK Technologies, Inc. // ** All rights reserved. // ******************************************************************************/ // /****************************************************************************** // ** $RCSfile: DtExampleObject.cxx,v $ $Revision: 1.4 $ $State: Exp $ // ******************************************************************************/ #include "DtExampleObject.h" #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 { DtExampleObject::DtExampleObject( DtDe& de, DtUniqueID id ) : myDe(de) , myPat(0) { } DtExampleObject::~DtExampleObject() { } 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, 100 units away. 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) { // Add the cow 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))); } }
// /****************************************************************************** // ** Copyright (c) 2008 MAK Technologies, Inc. // ** All rights reserved. // ******************************************************************************/ // /****************************************************************************** // ** $RCSfile: exampleDistributedObject.cxx,v $ $Revision: 1.12 $ $State: Exp $ // ******************************************************************************/ #include <vrvCore/DtVrvApplication.h> #include <vrvCore/DtDe.h> #include <vrvCore/DtAgentManager.h> #include <vrvCore/DtDriverManager.h> #include <vrvCore/DtDriver.h> // The header for the driver generated by the Distributed Object Compiler #include "DtExampleObjectAgent.hpp" // Use the VR-Vantage namespace. All classes in VR-Vantage are in this namespace. using namespace makVrv; // This driver creates and owns the example driver. class DtExampleDriver : public DtDriver { public: DtExampleDriver(DtAgentManager& am) : DtDriver(am,"DtExampleDriver") , myAgent(0) , myRotationDegree(0.0) { } 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 myAgent = DtExampleObjectAgent::create(myAgentManager); // Load the model myAgent->loadModel("../data/Lifeforms/Animals/cow.osg"); 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: DtExampleObjectAgent* myAgent; double myRotationDegree; }; int main(int argc,char** argv) { // A DtVrvApplication wraps up all the components required to build a complete // VR-Vantage application (DtDe, DtPluginManager, DtEventLoop, and a // DtVrvApplicationConfiguration). DtVrvApplication application; // This causes the plugin manager to load plugins, cause a Display Configuration // to be realized, creates the default environment, etc. application.initialize(argc, argv); // Create DtExampleDriver. DtExampleDriver* driver = new DtExampleDriver( application.de().agentManager()); // The driver is now owned by the display engine. Do not delete it. application.de().driverManager().addDriver(driver); // Start the driver, creating a driver. application.de().driverManager().startDriver(driver); // Creates an event loop and begins running it, causing the creation of frames. application.run(); }