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 <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",
"uniform vec3 colorToAdd;\
void customLightingFunction(inout vec4 color) \
{\
color.rgb += colorToAdd;\
}\
",
osgEarth::ShaderComp::LOCATION_FRAGMENT_LIGHTING,
NULL,
100.0
);
myVirtualProgram->setFunction("customVertexFunction",
"uniform vec3 scale;\
void customVertexFunction(inout vec4 vertexModel) \
{\
vertexModel.xyz *= scale;\
}\
",
osgEarth::ShaderComp::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);
}
}
#include <boost/bind.hpp>
#include <osgEarth/VirtualProgram>
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;
{
{
&loadModelInstanceToTheSceneGraph, &de));
}
}
{
return true;
}
{
&loadModelInstanceToTheSceneGraph, de));
md.setParameter("filename", dataFile);
{
delete mi;
return NULL;
}
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 )
{
}
}