VR-Vantage
Loading...
Searching...
No Matches
examplePostProcessorPlugin

Overview

This example shows how to create a custom post processor for VR Vantage.

Expected Result

Post Processor Result

Example Details

The example creates two derived classes. ExamplePostProcessor derives from DtPostProcessor and implements the constructor and update function for the new custom post processor. The class ExamplePostProcessorCreator is derived from DtPostProcessorCreator and is utilized in the DtPostProcessorFactory.

The plugin's init function sets the order of the post processor (placed before "camera_fx" runs) and registers the post processor effect with a user defined ObserverSetting enumeration value.

The update function demonstrates how to retrieve the depth buffer from the render to texture camera and link it to the shader state for the post processor. It also shows how to send variables such as time to the shader.

The shader for the post procesor is in examples\examplePostProcessor\example_ps.glsl. It draws the depth buffer in the lower left corner of the screen and applies a since wave distortion on the render to texture camera color buffer.

Please see the comments in the code as well since it describes the post processing system.

Building the Example

To build the plugin, 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/examplePostProcessor_stealth.bat(on Windows) or ./bin64/examplePostProcessor_stealth.sh (on Linux). Load Ground_DB II terrain. For more information about running examples, please see Running Applications and Examples.

This example currently disables the reverse Z buffer (–disableReverseZBuffer) in order to allow the rendered depth buffer to work correctly.

Example Source Files


examplePostProcessorPlugin.cxx

/******************************************************************************
** Copyright (c) 2024 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#include <vrvCore/DtDe.h>
#include <boost/bind/bind.hpp>
using namespace makVrv;
//Create a setting id that will be different than all the VR-Vantage setting id's
static const int observerSettingId = DtObserverSettingsManager::USER_START + 1;
static vrvSignalsLib::connection postInitializeConnection;
class ExamplePostProcessor : public DtPostProcessor
{
public:
ExamplePostProcessor(DtDe& de)
: DtPostProcessor(de, "example", false)
{
// The false in DtPostProcessor constructor indicates that it is not
// activated in 2D orthographic views
// Fragment shader has access to a texture of the camera's color buffer and
// can get at the depth buffer as a texture as well
setFragmentShaderFile("../examples/examplePostProcessor/example_ps.glsl");
// A post processor has a type (i.e. name) as well as one or more effects
// Usually the type() is used to get the DtPostProcessor* from the
// DtPostProcessManagerInterface
// The effects are stored as (string, bool) pairs as and can be turned
// on or off with enableEffect or disableEffect.
// The string supplied should match the effect name in registerEffect.
// In the update function you can check if an effect is enabled with
// bool effectActive = effectEnabled("myeffect") and do some extra code
// However, usually a post processor has just one effect which may
// not even be checked in the update function (as in this example).
addEffect("example_effect");
}
virtual bool update(DtChannel* channel)
{
osg::Camera* cam = cameraForChannel(channel);
if (cam)
{
// Shader parameters are set via the osg::StateSet
// and then referenced in the shader
osg::StateSet* ss = cam->getOrCreateStateSet();
osg::Uniform* timeUfrm = ss->getOrCreateUniform("vrv_time", osg::Uniform::FLOAT);
timeUfrm->set((float)myDe.simulationTime());
// Get the depth as a texture and feed it to the shader
// uniform sampler2D depthTex
osg::Texture* depthTexture = 0L;
DtOsgChannel* osgChannel = dynamic_cast<DtOsgChannel*>(channel);
if(osgChannel != NULL)
{
depthTexture = osgChannel->getDepthTexture();
}
if(depthTexture != 0L)
{
// Make the texture available in the shader at texture unit 1
cam->getOrCreateStateSet()->
getOrCreateUniform("vrv_depthTex", osg::Uniform::INT)->set(1);
cam->getOrCreateStateSet()->
setTextureAttributeAndModes(1, depthTexture);
return true;
}
}
return false;
// Note: If you have multiple post processors that all utilize alpha
// run vrvIG or vrvStealth with "--alphaBits 8" to allow alpha channel
// in the RTTCamera
}
};
// For DtPostProcessFactory
class ExamplePostProcessorCreator : public DtPostProcessorCreator
{
public:
DtPostProcessor* create(DtDe& de, const std::string&)
{
return new ExamplePostProcessor(de);
}
};
void slot_postInit(DtDe& de)
{
postInitializeConnection.disconnect();
// vrvStealth uses "Stealth" for 3D
// vrvIG would use "Out-the-Window"
// default the settings to enabled
int stealthMode = DtObserverModeManager::instance(de).lookupObserverModeKey("Stealth");
DtObserverModeManager::instance(de).setObserverSetting(stealthMode, observerSettingId, true);
DtScene& sceneDriver = de.scene();
std::string groundDbPath = de.dePathConfiguration().sharedDataPath();
groundDbPath += "/TerrainData/TerrainConfiguration/Ground_DB II.mtf";
if( sceneDriver.terrainFileName() != groundDbPath )
{
sceneDriver.loadTerrain( groundDbPath );
}
}
void initializeToolbar(DtDe& de)
{
= DtObserverSettingsManager::instance(de).displayNameProvider();
//setup the text in the menu and the tooltip for the toolbar button
QString qTemp = QObject::tr("Custom Effect");
nameProvider.setDisplayName( observerSettingId, DtUnicode::fromQString<QString>( qTemp ) );
qTemp = QObject::tr("Show/Hide Custom Effect");
nameProvider.setToolTip( observerSettingId, DtUnicode::fromQString<QString>( qTemp ) );
}
void init(DtDe& de)
{
// Ensure that init only gets called once
// Registers the built-in post processors
DtPostProcessorFactory::instance(de).addCreator("example", new ExamplePostProcessorCreator);
// You can see the final order of the post processors with
//DtPostProcessorFactory::instance(de).printAllPostProcessors();
// Sensor post processor should run after this example so insert before sensors if we can
if (!DtPostProcessorFactory::instance(de).addPostProcessorBefore("example", "camera_fx"))
{
DtPostProcessorFactory::instance(de).addPostProcessorAtEnd("example");
}
// Associate a post processor effect with an observer setting
// When slot_observerSettingChanged is called, it will check the ObserverSetting passed
// in and add or remove the post processor effects associated with that ObserverSetting
DtPostProcessorFactory::instance(de).registerEffect("example", "example_effect",
//initialize default state is when tooltips and display names can be set
de.signal_initializeDefaultState.connect(boost::bind(&initializeToolbar, std::ref(de)));
//post initialize will be used to set the observer setting to enabled by default
postInitializeConnection = de.signal_postInitialize.connect(boost::bind(&slot_postInit, std::ref(de)));
//register the menu item for the new effect
de, "CustomEffectItem", observerSettingId));
// Register the new visual type to control the custom effect post processor
// assign it a generic Star Icon for now.
DtVisualTypeManager::instance(de).registerVisualType(
DtVisualTypeManager::VisualType("custom_effect", observerSettingId,
(de.dePathConfiguration().iconsPath() + std::string("/UtilityStarWhite.svg")),
if ( igApp )
{
//Add a menu item and toolbar button for the new effect
makVrv::DtMenuPath::mainMenu("DtObserverMenu").item("CustomEffectItem"));
igApp->masterModeToolbarConfiguration().addToolbarPath(DtToolbarPath::toolbar("DtObserverSettingsToolbar").item("CustomEffectItem"));
}
}
{
// Setup the plugin
init(*de);
return true;
}
Contains makVrv::DtDe class.
#define DT_DE_INIT_ONCE(de)
This macro is called by modules in their init() function. This macro ensures that the module is only ...
Definition DtDe.h:30
Contains vrvCore::DtObserverMode class.
Contains the DtObserverModeManager class declaration.
Contains makVrv::DtObserverSettingsDisplayNameProvider class.
Contains classes used as menu items on the observer menu.
Contains makVrv::DtObserverSettingsManager class.
Contains makVrv::DtOsgChannel class.
Contains makVrv::DtPostProcessManagerInterface class.
Contains makVrv::DtQtMenuAssembler class.
Contains makVrv::DtScene class.
Contains makVrv::DtVrvApplication implementation.
The abstract Channel Class.
Definition DtChannel.h:36
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer,...
Definition DtDe.h:77
boost::signalslib::signal< void()> signal_initializeDefaultState
Definition DtDe.h:529
virtual const DtDePathConfiguration & dePathConfiguration() const
Return the DtDePathConfiguration.
virtual DtScene & scene()
The DtScene accessor.
boost::signalslib::signal< void()> signal_postInitialize
Definition DtDe.h:535
virtual std::string iconsPath() const
Accessors to resolved paths.
virtual std::string sharedDataPath() const
virtual void setToolTip(int intKey, const DtUnicode &displayName)
Change the tool tip associated with a key (if it exists) or add a new tool tip to key mapping.
virtual void setDisplayName(int intKey, const DtUnicode &displayName)
Change the dislay name associated with a key (if it exists) or add a new display name to key mapping.
void addMenuPath(const DtMenuPath &, const MenuSortFlags &sortFlagsIfMenu=(MenuSortFlags)(SortAllAlphabetically|SortWithinSubmenus|SortBetweenSeparators))
Adds the menu path (as well as its entire family).
static MenuPathBuilder mainMenu(const std::string &name)
static DtObserverModeManager & instance(DtDe &de)
Get an instance used to emit, or to connect.
Definition DtObserverSettingsItems.h:300
DtObserverSettingsDisplayNameProvider is an interface for a class to provide display names for observ...
Definition DtObserverSettingsDisplayNameProvider.h:24
ObserverSetting
Definition DtObserverSettingsManager.h:42
@ USER_START
Definition DtObserverSettingsManager.h:86
static DtObserverSettingsManager & instance(DtDe &de)
Get an instance used to emit, or to connect.
This class provides the OSG-specific implementation of a DtChannel.
Definition DtOsgChannel.h:59
virtual osg::ref_ptr< osg::Texture > getDepthTexture() const
Returns the depth texture for this channel.
Definition DtPostProcessor.h:235
static DtPostProcessorFactory & instance(DtDe &de)
Get an instance to the DtPostProcessorFactory.
This class provides the OSG-specific implementation of a post processor. A post processor has a type ...
Definition DtPostProcessor.h:67
static DtQtMenuAssembler & instance(DtDe &de)
Get an instance of the assembler.
An object that represents everything that is visible in the scene.
Definition DtScene.h:45
const std::string & terrainFileName() const
Get the loaded terrain filename.
bool loadTerrain(const DtUnicode &filePath)
Load the terrain from a file.
void addToolbarPath(const DtToolbarPath &)
Adds the toolbar path (as well as its entire family).
static ToolbarPathBuilder toolbar(const std::string &name)
static DtUnicode fromQString(QStringType qstr)
Template function for setting from Qt 4 style string.
Definition DtUnicode.h:107
@ ModelSetAll
Definition DtVisualTypeManager.h:48
static DtVisualTypeManager & instance(DtDe &de)
Static accessor. Get the instance of the manager rooted in the DtDe.
The Root IG Application object. To run the IG simply create one, initialize it and call run.
Definition DtVrvApplication.h:58
DtMenuConfiguration & masterModeMenuConfiguration()
Get the menu configuration which may be altered by plugins.
static DtVrvApplication * findFromDe(DtDe &)
DtToolbarConfiguration & masterModeToolbarConfiguration()
Get the toolbar configuration which may be altered by plugins.
DT_DLL_EXAMPLEACCESSORY void init(makVrv::DtDe &de)
DT_DE_PLUGIN_EXPORT_MACRO bool initDeModule(makVrv::DtDe *de)
DT_DLL_VRVCOREQT void init(DtDe &de)
DT_DLL_VRVPOSTPROCESSOR void init(DtDe &anIG)
Initialize the VRV Post Processor library.
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
Information about a visual type.
Definition DtVisualTypeManager.h:53

examplePostProcessorPlugin.h

/******************************************************************************
** Copyright (c) 2019 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#pragma once
namespace makVrv { class DtDe; }
// Get proper local export symbol
// Setup proper plugin export symbol
#define DT_DE_PLUGIN_EXPORT_MACRO DT_DLL_EXAMPLEPOSTPROCESSOR
// Declare & export the plugin initialization function (bool initDeModule(DtDe* de))
// Work function for the plugin initialization
Contains DLL export macros.
#define DT_DLL_EXAMPLEPOSTPROCESSOR
Definition examplePostProcessor.h:18