VR-Forces 4.0.4 Class Documentation
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 ./bin/exampleDistributedDraw_stealth.bat (on Windows) or ./bin/exampleDistributedDraw_stealth.sh (on Linux). For more information about running examples, please see Running Applications and Examples.

Example Source Files


DtDistributedDrawDriver.h

// /****************************************************************************** 
// ** Copyright (c) 2008 MAK Technologies, Inc. 
// ** All rights reserved. 
// ******************************************************************************/ 
// /****************************************************************************** 
// ** $RCSfile: DtDistributedDrawDriver.h,v $ $Revision: 1.1 $ $State: Exp $ 
// ******************************************************************************/ 


#ifndef DtDistributedDrawDriver_H_
#define DtDistributedDrawDriver_H_

#include <vrvCore/DtDriver.h>
#include <vrvCore/DtAgentManager.h>
#include "DtExampleDrawObjectAgent.hpp"

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.
class DtDistributedDrawDriver : public DtDriver
{
public:
   DtDistributedDrawDriver(DtAgentManager& agentManager,
      const std::string& instanceName);

   virtual ~DtDistributedDrawDriver();

   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();
  
   DtExampleDrawObjectAgent* myDrawAgent;

   double mySphereX, mySphereY, mySphereZ;

   bool mySphereInScene;

};

#endif

DtExampleDrawObject.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 <vrvCore/DtUniqueID.h>

#include <osg/Node>

#include <string>

namespace makVrv
{
   class DtExampleDrawObject
   {

   public:

      DtExampleDrawObject(DtDe& de, DtUniqueID id);

      virtual ~DtExampleDrawObject();

      void drawSphere(double x, double y, double z, double r);

      void removeChildFromAllRoots();

   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

exampleDistributedDraw.cxx

// /****************************************************************************** 
// ** Copyright (c) 2008 MAK Technologies, Inc. 
// ** All rights reserved. 
// ******************************************************************************/ 
// /****************************************************************************** 
// ** $RCSfile: exampleDistributedDraw.cxx,v $ $Revision: 1.2 $ $State: Exp $ 
// ******************************************************************************/ 
#include <vrvCore/DtVrvApplication.h>
#include <vrvCore/DtDe.h>
#include <vrvCore/DtKeyMapManager.h>
#include <vrvCore/DtKeyMap.h>
#include <vrvCore/DtDriverManager.h>
#include <vrvCore/DtInputDriver.h>
#include <vrvCore/DtVantageIgConfiguration.h>
#include <vrvCore/DtVantageIgCommandLineProcessor.h>
#include <vrvCore/DtAgentManager.h>
#include <vrvCore/DtAgentUpdateResolverInterface.h>
#include <vrvCore/DtSceneObject.h>
#include <vrvCore/DtDriver.h>

#include <boost/bind.hpp>
#include <boost/signal.hpp>

#include "DtDistributedDrawDriver.h"

// 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. 
   DtVantageIgConfiguration config;
   DtVrvApplication application(config);
   DtVantageIgCommandLineProcessor cmdLineProcessor;
   application.installCommandLineProcessor(&cmdLineProcessor);

   // Parse Command Line.
   application.initialize(argc,argv);

   // Create DtExampleDriver.
   DtDistributedDrawDriver* driver = new DtDistributedDrawDriver(
      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( 
      &DtDistributedDrawDriver::drawRandomSphere, driver), DtKeyMapManager::Rendering);

   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();
}

exampleDistributedDrawPlugin.cxx

/******************************************************************************
** Copyright (c) 2009 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
/******************************************************************************
** $RCSfile: exampleDistributedDrawPlugin.cxx,v $ $Revision: 1.1 $ $State: Exp $
******************************************************************************/


#include <vrvCore/DtDe.h>
#include <vrvCore/DtKeyMapManager.h>
#include <vrvCore/DtKeyMap.h>
#include <vrvCore/DtDriverManager.h>
#include <vrvCore/DtInputDriver.h>
#include <vrvCore/DtAgentManager.h>

#include <boost/bind.hpp>

#include "DtDistributedDrawDriver.h"
#include "exampleDistributedDrawPlugin.h"

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)
   DT_DE_INIT_ONCE(de);

   // 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.
      de.signal_postInitialize.connect(boost::bind(
         &loadDriver, &de));
   }
}

bool initDeModule(makVrv::DtDe* 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
   de->signal_postInitialize.disconnect(boost::bind(
      &loadDriver, de));

   // Create the driver that will manage the DtExampleDrawObjectAgents
   DtDistributedDrawDriver* driver = new DtDistributedDrawDriver(
      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();
   DtKeyMapManager& keyMapManager = DtKeyMapManager::instance(*de);
   keyMapManager.addKeyFunction( "Draw Sphere", boost::bind( 
      &DtDistributedDrawDriver::drawRandomSphere, driver), DtKeyMapManager::Rendering);

   // 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" );
   }
}

DtExampleDrawObject.cxx

// /****************************************************************************** 
// ** Copyright (c) 2008 MAK Technologies, Inc. 
// ** All rights reserved. 
// ******************************************************************************/ 
// /****************************************************************************** 
// ** $RCSfile: DtExampleDrawObject.cxx,v $ $Revision: 1.1 $ $State: Exp $ 
// ******************************************************************************/ 


#include "DtExampleDrawObject.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 <osg/Geode>
#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>

#include <vrvOsg/DtOsgRenderer.h>

namespace makVrv
{

   DtExampleDrawObject::DtExampleDrawObject( DtDe& de, DtUniqueID id )
   : 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;
      }
   }

}

DtDistributedDrawDriver.cxx

// /****************************************************************************** 
// ** Copyright (c) 2008 MAK Technologies, Inc. 
// ** All rights reserved. 
// ******************************************************************************/ 
// /****************************************************************************** 
// ** $RCSfile: DtDistributedDrawDriver.cxx,v $ $Revision: 1.1 $ $State: Exp $ 
// ******************************************************************************/ 


#include "DtDistributedDrawDriver.h"
#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;
}

DtDistributedDrawDriver::~DtDistributedDrawDriver()
{
   // Don't need to delete myAgent, since the driver manager guarantees
   // that onStop will be called before the driver is deleted.
}

const std::string& DtDistributedDrawDriver::className() const
{
   // name must be static, since it's returned by reference
   static std::string name("DtDistributedDrawDriver");
   return name;
}

bool DtDistributedDrawDriver::onStart()
{
   // 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
   if (mySphereInScene)
   {      
      updateSphere();
   }

   return true;
}

bool DtDistributedDrawDriver::onStop()
{
   // Shutdown cleanly:
   if (myDrawAgent)
   {
      delete myDrawAgent;
      myDrawAgent = 0;
   }

   return true;
}

bool DtDistributedDrawDriver::onTick()
{
   // 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();
   }
}

void DtDistributedDrawDriver::drawRandomSphere()
{
   mySphereX = rand() % 1000;
   mySphereY = rand() % 1000;
   mySphereZ = rand() % 1000;

   updateSphere();
}

void DtDistributedDrawDriver::updateSphere()
{
   // This call will queue the drawSphere function, and during the object update phase
   // drawSphere will be called on the DtExampleDrawObject instances.
   myDrawAgent->drawSphere(mySphereX, mySphereY, mySphereZ, 100);

   // Make a note that there's a sphere in the scene so that the driver
   // can restore its state correctly if stopped and restarted
   mySphereInScene = true;
}

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)