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

Overview

Shows how to create a custom effect texture type on loaded model

Expected Result

Effect Texure Plugin Result

Example details

VR-Vantage has a concept of Effect Textures. These effect textures allow you to have additional textures on a model without needing to edit the actual model file. This is how normal maps, emissivity maps, or any other secondary textures are configured in VR-Vantage. See section 24.7 of the VR-Vantage Users Guide for an explaination of the effect textures that are currently supported.

This example shows how to create a custom effect texture type and configure it to be applied to any model that has specially named textures. This custom effect texture replaces the base texture, the example also modifies this texture programmatically in the fragment shader by inverting the colours.

The class DtExampleEffectTextureController is the main class to look at for this example. The constructor sets up the vertex and fragment shaders that will be used when an EET texture is loaded. The generateShaders() function sets up the final version of the shader text and loads the shaders as virtual program functions (see exampleCustomShader for an explaination about how virtual programs work). getEffectTypeCode() specifies which texture Postfix should be used to determine if this effect texture controller should be used.

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/exampleEffectTexturePlugin_stealth.bat (on Windows) or ./bin64/exampleEffectTexturePlugin_stealth.sh (on Linux). Please add your shared data dir to the script in order to allow it to work correctly. This example is intended to be run without a terrain. Use your mouse wheel to get closer to the helicopter. Press 't' to change the effect on the helicopter. For more information about running examples, please see Running Applications and Examples. Close the open Terrain dialog to see the Helicopter that is loaded by the example.

The script that runs the example will also create the EET texture used by the example. On windows you may have to run the script in admin mode.

When running the example push the T key to turn the effect on or off. Notice the paint on the helicopter switch from black to camo when the effect texture is toggled.

Example Source Files


DtExampleEffectTextureController.cxx

/******************************************************************************
** Copyright (c) 2025 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#include <vrvCore/DtDe.h>
#include <matrix/vlMath.h>
#include <osg/StateSet>
#include <osg/Texture>
#include <osg/Texture2DArray>
#include <osgEarth/VirtualProgram>
#include <osgEarth/StringUtils>
using namespace makArchives;
namespace makVrv
{
const std::string ExampleMapFragFunctionName = "example_effect_texture_frag";
const std::string ExampleMapVertFunctionName = "example_effect_texture_vert";
// ../../.. because the shader loader looks for virtual program shaders in install_dir/data/shaders/components
const std::string ExampleMapFragFilePath = "../../../examples/exampleEffectTexturePlugin/exampleEffect.frag.glsl";
const std::string ExampleMapVertFilePath = "../../../examples/exampleEffectTexturePlugin/exampleEffect.vert.glsl";
// ../.. because the shader loader looks for indirect shaders in install_dir/data/shaders/
const std::string ExampleMapIndirectFragFilePath = "/../../examples/exampleEffectTexturePlugin/exampleEffect.frag.glsl";
const std::string ExampleMapEffectUnitName = "example_effect_texture_tex";
const std::string ExampleMapPostfix = "EET";
static osg::ref_ptr< osg::Uniform > effectEnabledUniform = nullptr;
{
if(effectEnabledUniform != NULL)
{
bool curVal = false;
effectEnabledUniform->get(curVal);
effectEnabledUniform->set(!curVal);
}
}
: DtOsgEffectTextureController(de, ETT_CUSTOM_MAP, ExampleMapPostfix )
{
if(!effectEnabledUniform)
{
effectEnabledUniform = new osg::Uniform("vrv_exampleEffectTextureEnabled", false);
}
DtEffectTextureRegistry& registry = DtEffectTextureRegistry::instance(de);
registry.registerEffectTextureType(ETT_CUSTOM_MAP, "example map", "ETT_CUSTOM_MAP", ExampleMapPostfix, false);
// This needs to be registered after IndirectLightingInitFragmentColoring so that the texture buffers have been filled.
registry.registerIndirectShaderEffectTexturetype(ETT_CUSTOM_MAP,
DtIndirectEffectTextureShader(ExampleMapFragFunctionName, ExampleMapIndirectFragFilePath,
DtVirtualProgramExecutionPriority::IndirectLightingInitFragmentColoring+1, "example_effect_texture_tex64", "example_effect_texture_coord" ));
// effect only applies to geometry so skipping light registration.
registry.registerShaderEffectTarget(ETT_CUSTOM_MAP, DtEffectTextureRegistry::TARGET_GEOMETRY);
}
DtExampleEffectTextureController::~DtExampleEffectTextureController()
{
//nop
}
bool DtExampleEffectTextureController::installShaders(int baseUnit, int effectUnit, const osg::Texture* tex, osg::StateSet* stateSet, const std::string& baseTextureName) const
{
DtVirtualProgramParams params;
params.vertShader = ExampleMapVertFilePath;
params.fragShader = ExampleMapFragFilePath;
params.vertFunction = ExampleMapVertFunctionName;
params.fragFunction = ExampleMapFragFunctionName;
params.vertFuncLoc = osgEarth::VirtualProgram::LOCATION_VERTEX_VIEW;
params.fragFuncLoc = osgEarth::VirtualProgram::LOCATION_FRAGMENT_LIGHTING;
params.vertExecPrio = DtVirtualProgramExecutionPriority::ReflectivityMapVertexView;
// run before regular lighting
params.fragExecPrio = 1.75;
params.unitWhoseCoordsToReplace = baseUnit;
params.effectUnitName = ExampleMapEffectUnitName;
osgEarth::VirtualProgram* vp = installVirtualProgram(baseUnit, effectUnit, tex, stateSet, baseTextureName, params);
stateSet->addUniform(effectEnabledUniform);
return vp != NULL;
}
DtOsgEffectTextureController* DtExampleEffectTextureControllerCreator::create(DtDe& de)
{
return new DtExampleEffectTextureController(de);
}
DtExampleIndirectDefaultDrawComponent::DtExampleIndirectDefaultDrawComponent( DtDe& de )
: DtIndirectDefaultDrawComponent(de)
{
}
void DtExampleIndirectDefaultDrawComponent::initialize( const DtIndirectEffectTexturesTemplate& effectTexturesState, unsigned int numTextCoordArrays )
{
DtIndirectDefaultDrawComponent::initialize( effectTexturesState, numTextCoordArrays );
if( myStateSet )
{
if( !effectEnabledUniform )
{
effectEnabledUniform = new osg::Uniform( "vrv_exampleEffectTextureEnabled", false );
}
myStateSet->addUniform( effectEnabledUniform );
}
}
}
Contains makVrv::DtDe class.
Contains makVrv::DtEffectControllerFactory class.
Contains makVrv::DtEffectTextureRegistry declaration.
Contains custom effect texture class declaration.
Contains makVrv::DtOsgEffectTextureController declaration.
Contains makVrv::DtOsgShaderUtils class.
Contains makVrv::DtVirtualProgramDepthOnlyCallback class.
Contains VirtualProgram execution priority constants.
Contains the DtVirtualProgramParams class.
Definition DtAddPropsRuleRecord.h:20
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
@ ETT_CUSTOM_MAP
Definition DtEffectTextureType.h:43

DtExampleEffectTextureController.h

/******************************************************************************
** Copyright (c) 2025 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#pragma once
#include <vrvCore/DtDe.h>
#include <osg/StateSet>
#include <osg/Texture>
using namespace makArchives;
namespace makVrv
{
class DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN DtExampleEffectTextureController : public DtOsgEffectTextureController
{
public:
DtExampleEffectTextureController(DtDe& de);
virtual ~DtExampleEffectTextureController();
virtual bool installShaders(int baseUnit, int effectUnit, const osg::Texture* tex, osg::StateSet* stateSet, const std::string& baseTextureName) const;
static void toggleEffectEnabled();
protected:
static const std::string myVertSource;
static const std::string myFragSource;
};
class DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN DtExampleEffectTextureControllerCreator : public DtEffectControllerCreator
{
public:
virtual DtOsgEffectTextureController* create(DtDe& de);
};
class DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN DtExampleIndirectDefaultDrawComponent : public DtIndirectDefaultDrawComponent
{
public:
DtExampleIndirectDefaultDrawComponent( DtDe& de );
virtual void initialize( const DtIndirectEffectTexturesTemplate& effectTexturesState, unsigned int numTextCoordArrays ) override;
};
using DtExampleIndirectDefaultDrawComponentCreator = DtVirtualBaseClassCustomCreator<DtExampleIndirectDefaultDrawComponent, DtIndirectDefaultDrawComponentCreator>;
}
Contains the DtIndirectDefaultDrawComponent class declaration.
Contains DLL export macros.
#define DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN
Definition exampleEffectTexturePlugin.h:19
DtVirtualBaseClassCustomCreator< DtExampleIndirectDefaultDrawComponent, DtIndirectDefaultDrawComponentCreator > DtExampleIndirectDefaultDrawComponentCreator
Definition DtExampleEffectTextureController.h:59

exampleEffectTexturePlugin.h

/******************************************************************************
** Copyright (c) 2019 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#ifndef EXAMPLEEFFECTTEXTUREPlugin_H_
#define EXAMPLEEFFECTTEXTUREPlugin_H_
#ifdef _WIN32
#ifdef EXAMPLEEFFECTTEXTUREPLUGIN_EXPORTS
#define DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN __declspec ( dllexport )
#else
#define DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN __declspec ( dllimport )
#endif
#else
#define DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN
#endif
#endif

exampleEffectTexturePluginInit.cxx

/******************************************************************************
** Copyright (c) 2025 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#include <vrvCore/DtDe.h>
#include <osg/PositionAttitudeTransform>
// The name of the geometry file to load. The custom effect texture for this model is
// created by the exampleEffectTexturePlugin run script and is called DB_UH60L_1024_DESERT_CAMO_EET_rgb.meif
static const std::string dataFile =
"/ModelData/Vehicles/RotaryWing/uh-60l.br.flt/uh-60l.br_FBX.medf";
using namespace makVrv;
static vrvSignalsLib::connection postInitializeConnection;
// This is a helper function to load a geometry file and add it to the scene-
// graph. A model-definition is first given to a model-instance telling it
// which geometry file is to be loaded. Then the realize() function is called
// on the model-instance to load the file from disk. Last the root node from
// the model-instance is added to the scene-graph.
{
// Create a model definition describing the model to load.
// The DtModelDefinition class is used to describe several attributes about
// the model. For this example we just specify the name of the geometry
// file to load.
DtModelDefinition md("theModel");
std::string sharedDataPath = de->dePathConfiguration().sharedDataPath();
md.setParameter("filename", sharedDataPath+dataFile);
// Create a model instance and load a file from the model definition.
// In this example we use a DtOsgSimpleModelInstance as the type of model-
// instance that will be added to the scene. Since we are creating this
// object directly without the use of a factory, we're required to call
// the realize() function to actually load the model.
if (! mi->realize(md))
{
delete mi;
return;
}
// Create a transform to move the model away from the origin.
// The default observer starts at 0,0,0. Move the transform, 100 units
// away and rotate it to look nice.
osg::ref_ptr<osg::PositionAttitudeTransform> pat =
new osg::PositionAttitudeTransform();
pat->setPosition(osg::Vec3(0,100,0));
pat->setAttitude(osg::Quat(
osg::DegreesToRadians(-10.0f), osg::Vec3(1.0f, 0.0f, 0.0f),
osg::DegreesToRadians(-30.0f), osg::Vec3(0.0f, 1.0f, 0.0f),
osg::DegreesToRadians(150.0f), osg::Vec3(0.0f, 0.0f, 1.0f)));
// Add the root-node of the model to the transform.
pat->addChild(mi->rootNode());
// Add the transform to the prop-root of the renderer.
DtOsgRenderer& renderer = (DtOsgRenderer&)de->renderer();
renderer.addNodeToRoot(pat, renderer.propRoot());
//Connect the example effect texture enable uniform to the T key
DtInputDriver& inputDriver =
keyMapManager.addKeyFunction( "Toggle Example Effect Texture", boost::bind(
DtKeyMap* obsFrame = keyMapManager.findKeyMap( "Observer Frame" );
if ( obsFrame )
{
inputDriver.addKeyBinding( obsFrame, DtKeyState::KEY_T,
"Toggle Example Effect Texture" );
}
// We're done with the signal, so disconnect from it
postInitializeConnection.disconnect();
}
{
return true;
}
using namespace makArchives;
namespace makVrv
{
{
void init(DtDe& de)
{
DtEffectControllerFactory& ecf = DtEffectControllerFactory::instance(de);
ecf.registerCreator("DtExampleEffectTextureController", new DtExampleEffectTextureControllerCreator());
// Add a model to the scene graph after the de has initialized
postInitializeConnection = de.signal_postInitialize.connect(boost::bind(
}
}
}
#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 makVrv:DtMasterScene class.
Contains makVrv::DtInputDriver class.
Contains makVrv::DtKeyMapManager class.
Interaction makVrv::DtModelDefinition class.
Contains makVrv::DtModelInstance class.
Contains makVrv::DtOsgEmptySceneLogo class.
Contains makVrv::DtOsgRenderer class.
Contains makVrv::DtOsgSimpleModelInstance class.
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer,...
Definition DtDe.h:77
virtual const DtDePathConfiguration & dePathConfiguration() const
Return the DtDePathConfiguration.
virtual DtDriverManager & driverManager()
The DtMasterScene accessor. The function will throw before the initialize function is called.
boost::signalslib::signal< void()> signal_postInitialize
Definition DtDe.h:535
virtual DtRenderer & renderer() const
Get the renderer.
virtual std::string sharedDataPath() const
DtInputDriver & inputDriver()
Return the DtInputDriver associated with the master scene.
virtual void registerCreator(const std::string &name, DtEffectControllerCreator *creator)
Register a new channel keyword object creator.
static DtEffectControllerFactory & instance(DtDe &de)
Get an instance to the DtEffectControllerFactory.
This class's main responsibility is to handle keyboard and mouse events from OSG (Qt events are route...
Definition DtInputDriver.h:46
void addKeyBinding(DtKeyMap *map, DtKeyState::KeyType key, const std::string &functionName, DtKeyState::KeyModifier modifiers=DtKeyState::NO_MODIFIER, bool replace=false, DtKeyEvent::KeyEventType eventType=DtKeyEvent::KEY_DOWN)
bind a key down or key up event (possibly modified) to a function name
a Key Map contains a map of keys to FunctionMapEntrys, as well as a list of functions to execute when...
Definition DtKeyMap.h:313
The manager for owning key maps.
Definition DtKeyMapManager.h:32
@ Rendering
Definition DtKeyMapManager.h:43
static DtKeyMapManager & instance(DtDe &de)
Get an instance of the key map.
virtual void addKeyFunction(const std::string &functionName, const KeyFunction &function, FunctionCategory functionCategory, bool categorize=true)
bind a function to string name that can be used in a key map (or binary function set)
virtual DtKeyMap * findKeyMap(const std::string &mapName)
look up a key map
@ KEY_T
Definition DtKeyState.h:75
A model definition Interaction any number of named parameters, and also can be used to store cached d...
Definition DtModelDefinition.h:45
static void setEnabled(bool enabled)
This global method will enable or disable the use of an empty scene overlay for all uses of this clas...
virtual osg::Node * rootNode()
Get the model instance's root node.
The DtOsgRenderer is the component responsible for rendering 3D images onto the screen via OpenSceneG...
Definition DtOsgRenderer.h:93
virtual void addNodeToRoot(osg::Node *node, const DtSceneRoot &sr, bool createOnAccess=true)
Add a node to the specified SceneRoot.
static DtSceneRoot propRoot()
A simple OSG model instance.
Definition DtOsgSimpleModelInstance.h:29
virtual bool realize(const DtModelDefinition &definition)
Attempt to realize a definition. If the model instance can't realize the definition it should return ...
void slot_postInitialize(makVrv::DtDe &de)
DT_DE_PLUGIN_EXPORT_MACRO bool initDeModule(makVrv::DtDe *de)
Definition exampleEffectTexturePluginInit.h:22
DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN void init(DtDe &anIG)
DT_DLL_VRVOSG void init(DtDe &anIG, bool initOpenGL=true)

exampleEffectTexturePluginInit.h

/******************************************************************************
** Copyright (c) 2019 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
#pragma once
// Get proper local export symbol
// Setup proper plugin export symbol
#define DT_DE_PLUGIN_EXPORT_MACRO DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN
// Export plugging function bool initDeModule(DtDe* de);
namespace makVrv
{
class DtDe;
namespace vrvExampleEffectTexturePlugin
{
}
}