This example shows how to create custom shaders using Virtual Programs.
It contains a simple example fragment shader function and a simple example vertex fragment shader funtion. The fragment shader uses a uniform to modify the color of the fragment. The vertex shader uses a uniform to modify the scale of the model.
This example is a plug-in. You can run it by running ./bin64/exampleCustomShaders_stealth.bat (on Windows) or ./bin64/exampleCustomShaders_stealth.sh (on Linux). This example is indented to be run without a terrain. When VR-Vantage starts close the startup dialog. Press 'r', 'R', 'g', 'G', 'b', and 'B' to change the color of the model. Press 'x', 'X', 'y', 'Y', 'z' and 'Z' to change scale of the model. Disabling HDR from the toolbar will help see the shader effect. For more information about running examples, please see Running Applications and Examples.
#include <osg/Uniform>
#include <osg/StateSet>
#include <osgEarth/VirtualProgram>
{
: myDe(de)
, myModelStateSet(ss)
{
myColorToAdd = ss->getOrCreateUniform("colorToAdd", osg::Uniform::FLOAT_VEC3);
myColorToAdd->set(osg::Vec3(0.0f,0.0f,0.0f));
myScale = ss->getOrCreateUniform("scale", osg::Uniform::FLOAT_VEC3);
myScale->set(osg::Vec3(1.0f,1.0f,1.0f));
myVirtualProgram = osgEarth::VirtualProgram::getOrCreate(ss);
myVirtualProgram->setFunction("customLightingFunction", R"(
uniform vec3 colorToAdd;
void customLightingFunction(inout vec4 color)
{
color.rgb += colorToAdd;
}
)",
osgEarth::VirtualProgram::LOCATION_FRAGMENT_LIGHTING,
NULL,
100.0
);
myVirtualProgram->setFunction("customVertexFunction", R"(
uniform vec3 scale;
void customVertexFunction(inout vec4 vertexModel)
{
vertexModel.xyz *= scale;
}
)",
osgEarth::VirtualProgram::LOCATION_VERTEX_MODEL,
NULL,
100.0
);
}
DtExampleShaderManager::~DtExampleShaderManager()
{
}
void DtExampleShaderManager::changeColorOverride(int color, float deltaChange)
{
osg::Vec3 curVal;
myColorToAdd->get(curVal);
switch(color)
{
case 0:
curVal.x() += deltaChange;
curVal.x() = curVal.x() < -1.0 ? -1.0 : curVal.x();
curVal.x() = curVal.x() > 1.0 ? 1.0 : curVal.x();
break;
case 1:
curVal.y() += deltaChange;
curVal.y() = curVal.y() < -1.0 ? -1.0 : curVal.y();
curVal.y() = curVal.y() > 1.0 ? 1.0 : curVal.y();
break;
case 2:
curVal.z() += deltaChange;
curVal.z() = curVal.z() < -1.0 ? -1.0 : curVal.z();
curVal.z() = curVal.z() > 1.0 ? 1.0 : curVal.z();
break;
default:
break;
}
myColorToAdd->set(curVal);
}
void DtExampleShaderManager::changeScale(int axis, float deltaChange)
{
osg::Vec3 curVal;
myScale->get(curVal);
switch(axis)
{
case 0:
curVal.x() += deltaChange;
curVal.x() = curVal.x() < -5.0 ? -5.0 : curVal.x();
curVal.x() = curVal.x() > 5.0 ? 5.0 : curVal.x();
break;
case 1:
curVal.y() += deltaChange;
curVal.y() = curVal.y() < -5.0 ? -5.0 : curVal.y();
curVal.y() = curVal.y() > 5.0 ? 5.0 : curVal.y();
break;
case 2:
curVal.z() += deltaChange;
curVal.z() = curVal.z() < -5.0 ? -5.0 : curVal.z();
curVal.z() = curVal.z() > 5.0 ? 5.0 : curVal.z();
break;
default:
break;
}
myScale->set(curVal);
}
}
Contains makVrv:DtAgentManager class.
Definition of the Example Shader Manager which manages the shaders for the model created by exampleCu...
Contains makVrv::DtSceneObject class.
DtExampleShaderManager(DtDe &de, osg::StateSet *ss)
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
#ifndef DtExampleShaderManager_H_
#define DtExampleShaderManager_H_
#include <osg/ref_ptr>
{
class StateSet;
class Uniform;
}
{
class VirtualProgram;
}
{
class DtExampleShaderManager
{
public:
protected:
osg::ref_ptr<osg::Uniform>
myScale;
};
}
#endif
Contains makVrv::DtDe class.
Contains makVrv::DtUniqueID type.
virtual ~DtExampleShaderManager()
DTOR.
virtual void changeScale(int axis, float deltaChange)
virtual void changeColorOverride(int color, float deltaChange)
osg::ref_ptr< osgEarth::VirtualProgram > myVirtualProgram
Definition DtExampleShaderManager.h:64
osg::ref_ptr< osg::Uniform > myScale
Definition DtExampleShaderManager.h:61
osg::ref_ptr< osg::StateSet > myModelStateSet
Definition DtExampleShaderManager.h:57
osg::ref_ptr< osg::Uniform > myColorToAdd
Definition DtExampleShaderManager.h:60
DtDe & myDe
Definition DtExampleShaderManager.h:54
Definition DtProceduralSoundscapeQuery.h:26
Definition DtOverlayPolygonModelInstance.h:28
#include <osgEarth/VirtualProgram>
#include <boost/bind/bind.hpp>
static const std::string dataFile =
"$(SHARED_DATA_DIR)/ModelData/Vehicles/FixedWing/DC-10_RED_V7.0/DB_DC10_RED_V7.0.medf";
DtModelInstance* loadModelInstanceToTheSceneGraph(DtDe* de);
void createShaderManager(DtDe* de, osg::StateSet *modelStateSet);
static vrvSignalsLib::connection postInitializeConnection;
{
{
&loadModelInstanceToTheSceneGraph, &de));
}
}
{
return true;
}
{
postInitializeConnection.disconnect();
md.setParameter("filename", dataFile);
{
delete mi;
return NULL;
}
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)));
createShaderManager(de, mi->
rootNode()->getOrCreateStateSet());
return mi;
}
void createShaderManager(
DtDe* de, osg::StateSet *modelStateSet)
{
if ( obsFrame )
{
}
}
#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::DtKeyMap and related classes.
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 bool isInMasterMode() const
Returns true if the DtDe is in master mode.
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.
DtInputDriver & inputDriver()
Return the DtInputDriver associated with the master scene.
Definition DtExampleShaderManager.h:34
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_X
Definition DtKeyState.h:79
@ KEY_B
Definition DtKeyState.h:57
@ KEY_G
Definition DtKeyState.h:62
@ KEY_Z
Definition DtKeyState.h:81
@ KEY_Y
Definition DtKeyState.h:80
@ KEY_R
Definition DtKeyState.h:73
@ SHIFT_MODIFIER
Definition DtKeyState.h:140
@ NO_MODIFIER
Definition DtKeyState.h:137
A model definition Interaction any number of named parameters, and also can be used to store cached d...
Definition DtModelDefinition.h:45
The base class for all model instances.
Definition DtModelInstance.h:44
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 ...
DT_DLL_EXAMPLEACCESSORY void init(makVrv::DtDe &de)
DT_DE_PLUGIN_EXPORT_MACRO bool initDeModule(makVrv::DtDe *de)