VR-Forces 4.8 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
exampleDistributedDraw

Table of Contents

Overview

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.

Expected Result

exampleDistributedDraw2.png
Distributed Draw Result

Example details

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). In Vantage, load MakLand terrain than from the "Observer Views" panel click on "import and append views" and select "exampleDrawObjectsMaklandView.osrx" then click on "remoteGraphicsMaklandView". If you also started "exampleDistributedDraw_slave", connect the remote display engine by first enabling from the menu "view->Display Engine Configuration Editor Panel". In the "Display Engine Configuration Editor Panel" click on "Connect To Display Engine", set the host name (leave it to localhost if you are running both on the same PC), click connect. Press the "F" key to create a sphere and see it display on both window.

For more information about running examples, please see Running Applications and Examples.

Example Source Files


DtExampleModel.cxx

// /******************************************************************************
// ** Copyright (c) 2019 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#ifndef DtDistributedDrawDriver_H_
#define DtDistributedDrawDriver_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.
{
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();
DtExampleDrawObjectAgent* myDrawAgent;
double mySphereX, mySphereY, mySphereZ;
bool mySphereInScene;
};
#endif

DtExampleModel.h

// /******************************************************************************
// ** Copyright (c) 2019 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#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
};
}
#endif

DtExampleModelInstance.cxx

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) 2019 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#include <osg/Group>
#include <osg/Node>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
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;
// Setting up the diffuse and ambient material properties.
// We are setting up these properties to enable shading on the sphere.
osg::Material* mat = new osg::Material();
mat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.40, 0.55, 1.0));
mat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.5, 0.8, 0.35, 1.0));
// Add material properties that are already setup to the sphere.
geode->getOrCreateStateSet()->setAttributeAndModes(mat);
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)
, mySphereX(0.0)
, mySphereY(0.0)
, mySphereZ(0.0)
{
// 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.
}
const std::string& DtDistributedDrawDriver::className() const
{
// 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
}

[<< Examples] [Home] [Top of Page]


Document ID: Generated on Thu Aug 27 10:56:05 EDT 2020 from SVN revision 217100
Copyright © 2005-2020 MAK Technologies. All Rights Reserved (www.mak.com)