Overview
This example shows how to create custom shaders using Virtual Programs.
Expected Result
Custom Shader Result
Example details
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.
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/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.
Example Source Files
DtExampleModel.cxx
#include <osg/Uniform>
#include <osg/StateSet>
#include <osgEarth/VirtualProgram>
namespace makVrv
{
: 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);
}
}
DtExampleModel.h
#ifndef DtExampleShaderManager_H_
#define DtExampleShaderManager_H_
#include <osg/ref_ptr>
namespace osg
{
class StateSet;
class Uniform;
}
namespace osgEarth
{
class VirtualProgram;
}
namespace makVrv
{
class DtExampleShaderManager
{
public:
protected:
osg::ref_ptr<osg::Uniform>
myScale;
};
}
#endif
DtExampleModelInstance.cxx
#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);
using namespace makVrv;
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 )
{
}
}
DtExampleModelInstance.h
#pragma once
#ifdef _WIN32
#ifdef EXAMPLECUSTOMSHADERS_EXPORTS
#define DT_DLL_EXAMPLECUSTOMSHADER __declspec ( dllexport )
#else
#define DT_DLL_EXAMPLECUSTOMSHADER __declspec ( dllimport )
#endif
#else
#define DT_DLL_EXAMPLECUSTOMSHADER
#endif
#define DT_DE_PLUGIN_EXPORT_MACRO DT_DLL_EXAMPLECUSTOMSHADER
using namespace makVrv;
[<< Examples] [Home] [Top of Page]