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
#include <matrix/vlMath.h>
#include <osg/StateSet>
#include <osg/Texture>
#include <osg/Texture2DArray>
#include <osgEarth/VirtualProgram>
#include <osgEarth/StringUtils>
{
const std::string ExampleMapFragFunctionName = "example_effect_texture_frag";
const std::string ExampleMapVertFunctionName = "example_effect_texture_vert";
const std::string ExampleMapFragFilePath = "../../../examples/exampleEffectTexturePlugin/exampleEffect.frag.glsl";
const std::string ExampleMapVertFilePath = "../../../examples/exampleEffectTexturePlugin/exampleEffect.vert.glsl";
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);
registry.registerIndirectShaderEffectTexturetype(ETT_CUSTOM_MAP,
DtIndirectEffectTextureShader(ExampleMapFragFunctionName, ExampleMapIndirectFragFilePath,
DtVirtualProgramExecutionPriority::IndirectLightingInitFragmentColoring+1, "example_effect_texture_tex64", "example_effect_texture_coord" ));
registry.registerShaderEffectTarget(ETT_CUSTOM_MAP, DtEffectTextureRegistry::TARGET_GEOMETRY);
}
DtExampleEffectTextureController::~DtExampleEffectTextureController()
{
}
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;
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.
static void toggleEffectEnabled()
DtExampleEffectTextureController(DtDe &de)
Definition DtAddPropsRuleRecord.h:20
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
@ ETT_CUSTOM_MAP
Definition DtEffectTextureType.h:43
DtExampleEffectTextureController.h
#pragma once
#include <osg/StateSet>
#include <osg/Texture>
{
{
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;
};
{
public:
virtual DtOsgEffectTextureController* create(DtDe& de);
};
{
public:
DtExampleIndirectDefaultDrawComponent( DtDe& de );
virtual void initialize( const DtIndirectEffectTexturesTemplate& effectTexturesState, unsigned int numTextCoordArrays ) override;
};
}
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
#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
#include <osg/PositionAttitudeTransform>
static const std::string dataFile =
"/ModelData/Vehicles/RotaryWing/uh-60l.br.flt/uh-60l.br_FBX.medf";
static vrvSignalsLib::connection postInitializeConnection;
{
md.setParameter("filename", sharedDataPath+dataFile);
{
delete mi;
return;
}
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)));
keyMapManager.
addKeyFunction(
"Toggle Example Effect Texture", boost::bind(
if ( obsFrame )
{
"Toggle Example Effect Texture" );
}
postInitializeConnection.disconnect();
}
{
return true;
}
{
{
{
ecf.
registerCreator(
"DtExampleEffectTextureController",
new DtExampleEffectTextureControllerCreator());
}
}
}
#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::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.
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 ...
static void registerInstance(DtDe &de)
Definition DtVirtualBaseClassCustomCreator.h:59
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
#pragma once
#define DT_DE_PLUGIN_EXPORT_MACRO DT_DLL_EXAMPLEEFFECTTEXTUREPLUGIN
{
class DtDe;
namespace vrvExampleEffectTexturePlugin
{
}
}