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

This DtDistributedDraw driver owns a DtExampleDrawObject, and is responsible for keeping track of the draw object's state and creating or destroying it when the driver is started or stopped.

DtExampleDrawObject is the implementation of the distributed draw object. When a DtExampleDrawObjectAgent is created on the master, an exampleDrawObject is created locally on each display engine.

Building the Example

VR-Vantage includes pre-built versions of the example plug-in. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.

Running the Example

This example is a plug-in. You can run it by running ./bin64/exampleDistributedDraw_stealth.bat and ./bin64/exampleDistributedDraw_slave.bat (on Windows) or ./bin64/exampleDistributedDraw_stealth.sh and ./bin64/exampleDistributedDraw_slave.sh (on Linux). For more information about running examples, please see Running Applications and Examples.

Press the "F" key to create a sphere.

Example Source Files


DtExampleModel.cxx

// /******************************************************************************
// ** Copyright (c) 2008 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
// /******************************************************************************
// ** $RCSfile: DtDistributedDrawDriver.h,v $ $Revision: 1.1 $ $State: Exp $
// ******************************************************************************/
#ifndef DtDistributedDrawDriver_H_
#define DtDistributedDrawDriver_H_
using namespace makVrv;
// This driver owns a DtExampleDrawObject, and is responsible for keeping track of
// the draw object's state & creating or destroying it when the driver is started or stopped.
{
public:
const std::string& instanceName);
virtual const std::string& className() const;
virtual void drawRandomSphere();
protected:
virtual bool onStart();
virtual bool onStop();
virtual void slot_displayEngineAdded( const DtDeRecord& igRecord );
virtual bool onTick();
virtual void updateSphere();
double mySphereX, mySphereY, mySphereZ;
bool mySphereInScene;
};
#endif

DtExampleModel.h

// /******************************************************************************
// ** Copyright (c) 2008 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
// /******************************************************************************
// ** $RCSfile: DtExampleDrawObject.h,v $ $Revision: 1.1 $ $State: Exp $
// ******************************************************************************/
#ifndef DtExampleDrawObject_H_
#define DtExampleDrawObject_H_
#include <vrvCore/DtDe.h>
#include <osg/Node>
#include <string>
namespace makVrv
{
class DtExampleDrawObject
{
public:
void drawSphere(double x, double y, double z, double r);
protected:
DtDe& myDe;
// Keep track of the node drawSphere creates so that it will be cleaned up properly
osg::ref_ptr<osg::Node> myNode;
};
}
#endif

DtExampleModelInstance.cxx

// /******************************************************************************
// ** Copyright (c) 2008 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
// /******************************************************************************
// ** $RCSfile: exampleDistributedDraw.cxx,v $ $Revision: 1.2 $ $State: Exp $
// ******************************************************************************/
#include <vrvCore/DtDe.h>
#include <boost/bind.hpp>
#include <boost/signal.hpp>
// Use the VR-Vantage namespace. All classes in VR-Vantage are in this namespace.
using namespace makVrv;
int main(int argc,char** argv)
{
// Create the Application.
DtVrvApplication application(config);
application.installCommandLineProcessor(&cmdLineProcessor);
// Parse Command Line.
application.initialize(argc,argv);
// Create DtExampleDriver.
application.de().agentManager(), "drawDriver1");
// The driver is now owned by the display engine. Do not delete it.
application.de().driverManager().addDriver(driver);
// Start the driver, creating the agent.
application.de().driverManager().startDriver(driver);
// Bind the 'F' key to the driver's draw a random sphere function
DtInputDriver& inputDriver = application.de().driverManager().inputDriver();
DtKeyMapManager& keyMapManager = DtKeyMapManager::instance(application.de());
keyMapManager.addKeyFunction( "Draw Sphere", boost::bind(
DtKeyMap* obsFrame = keyMapManager.findKeyMap( "Observer Frame" );
if ( obsFrame )
{
inputDriver.addKeyBinding( obsFrame, DtKeyState::KEY_F, "Draw Sphere" );
}
// Creates an event loop and begins running it, causing the creation of frames.
application.run();
}

DtExampleModelInstance.h

/******************************************************************************
** Copyright (c) 2009 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
/******************************************************************************
** $RCSfile: exampleDistributedDrawPlugin.cxx,v $ $Revision: 1.1 $ $State: Exp $
******************************************************************************/
#include <vrvCore/DtDe.h>
#include <boost/bind.hpp>
using namespace makVrv;
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.
&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
&loadDriver, de));
// Create the driver that will manage the DtExampleDrawObjectAgents
de->agentManager(), "drawDriver1");
// 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);
// Add a key binding that calls the example driver's draw function:
// The input driver responds to input events by calling key functions. To
// bind a key to an arbitrary function, there are two steps:
// 1. Create a KeyFunction that calls the desired function and add it
// to the input driver
// 2. Add a key binding from some key to the KeyFunction defined in step 1
// to the keymap
// Create the KeyFunction
DtInputDriver& inputDriver = de->driverManager().inputDriver();
keyMapManager.addKeyFunction( "Draw Sphere", boost::bind(
// Bind the above key function to the 'f' key in the default
// ("Observer Frame") key map
DtKeyMap* obsFrame = keyMapManager.findKeyMap( "Observer Frame" );
if ( obsFrame )
{
inputDriver.addKeyBinding( obsFrame, DtKeyState::KEY_F, "Draw Sphere" );
}
}

DtExampleModelManager.cxx

// /******************************************************************************
// ** Copyright (c) 2008 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
// /******************************************************************************
// ** $RCSfile: DtExampleDrawObject.cxx,v $ $Revision: 1.1 $ $State: Exp $
// ******************************************************************************/
#include <osg/Group>
#include <osg/Node>
#include <osg/Geode>
#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>
namespace makVrv
{
: myDe(de)
, myNode()
{
}
DtExampleDrawObject::~DtExampleDrawObject()
{
// Clear out myNode from the scene
if (myNode.valid())
{
removeChildFromAllRoots();
}
}
void DtExampleDrawObject::removeChildFromAllRoots()
{
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)
{
// Remove the object from the scene graph.
((DtOsgRenderer&)myDe.renderer()).removeNodeFromRoot( myNode.get(),
DtOsgRenderer::visualizerTypeRoot( iter->first, 0 ) );
}
++iter;
}
myNode = 0;
}
void DtExampleDrawObject::drawSphere(double x, double y, double z, double r)
{
// Clear out myNode from the scene if it was there already
if (myNode.valid())
{
removeChildFromAllRoots();
myNode = 0;
}
// Create a sphere with specified radius at the specified location
osg::Geode* geode = new osg::Geode;
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(x,y,z), r)));
myNode = geode;
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 object to the root of the scene graph.
((DtOsgRenderer&)myDe.renderer()).addNodeToRoot( myNode.get(),
DtOsgRenderer::visualizerTypeRoot( iter->first, 0 ) );
}
++iter;
}
}
}

DtExampleModelManager.h

// /******************************************************************************
// ** Copyright (c) 2008 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
// /******************************************************************************
// ** $RCSfile: DtDistributedDrawDriver.cxx,v $ $Revision: 1.1 $ $State: Exp $
// ******************************************************************************/
#include <stdlib.h>
DtDistributedDrawDriver::DtDistributedDrawDriver( DtAgentManager& agentManager, const std::string& instanceName )
: DtDriver(agentManager, instanceName)
, myDrawAgent(0)
, mySphereInScene(false)
{
// Tell the driver to start automatically when added to the DE
setAutoStart(true);
// This driver can't be started or stopped from the GUI; myUserControllableFlag sets this property
myUserControllableFlag = false;
}
{
// Don't need to delete myAgent, since the driver manager guarantees
// that onStop will be called before the driver is deleted.
}
{
// name must be static, since it's returned by reference
static std::string name("DtDistributedDrawDriver");
return name;
}
{
// Create the distributed draw agent, which will create a DtExampleDrawObject
// on each connected display engine.
myDrawAgent = DtExampleDrawObjectAgent::create(myAgentManager);
// Restore the state of the driver's agent if necessary
{
}
return true;
}
{
// Shutdown cleanly:
{
delete myDrawAgent;
}
return true;
}
{
// Does nothing
return true;
}
void DtDistributedDrawDriver::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();
}
}
{
mySphereX = rand() % 1000;
mySphereY = rand() % 1000;
mySphereZ = rand() % 1000;
}
{
// This call will queue the drawSphere function, and during the object update phase
// drawSphere will be called on the DtExampleDrawObject instances.
// Make a note that there's a sphere in the scene so that the driver
// can restore its state correctly if stopped and restarted
}


Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)