/usr/share/openscenegraph/examples/osgvirtualprogram/CreateSimpleHierachy.cpp is in openscenegraph-examples 3.0.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include <iostream>
#include <osg/Geode>
#include <osg/TexGen>
#include <osg/Texture2D>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include "VirtualProgram.h"
using osgCandidate::VirtualProgram;
////////////////////////////////////////////////////////////////////////////////
osg::Node * CreateSimpleHierarchy( osg::Node * node )
{
if( !node ) return NULL;
float r = node->getBound().radius(); // diameter
osg::Group * root = new osg::Group();
osg::Group * group = new osg::Group();
// Create four matrices for translated instances of the cow
osg::MatrixTransform * transform0 = new osg::MatrixTransform( );
transform0->setMatrix( osg::Matrix::translate( 0,0,r ) );
osg::MatrixTransform * transform1 = new osg::MatrixTransform( );
transform1->setMatrix( osg::Matrix::translate( 0,0,0 ) );
osg::MatrixTransform * transform2 = new osg::MatrixTransform( );
transform2->setMatrix( osg::Matrix::translate( -r,0,-r ) );
osg::MatrixTransform * transform3 = new osg::MatrixTransform( );
transform3->setMatrix( osg::Matrix::translate( r,0,-r ) );
root->addChild( transform0 );
root->addChild( group );
group->addChild( transform1 );
group->addChild( transform2 );
group->addChild( transform3 );
transform0->addChild( node );
transform1->addChild( node );
transform2->addChild( node );
transform3->addChild( node );
// At the scene root apply standard program
if( 1 )
{
osg::Program * program = new osg::Program;
osg::Shader * main = new osg::Shader( osg::Shader::FRAGMENT );
main->setShaderSource(
"uniform sampler2D base; \n"
"void main(void) \n"
"{\n"
" gl_FragColor = gl_Color * texture2DProj( base, gl_TexCoord[0] );\n"
" gl_FragColor *= vec4( 1.0, 1.0, 1.0, 0.5 ); \n"
"}\n"
);
program->addShader( main );
main->setName( "White" );
root->getOrCreateStateSet( )->setAttributeAndModes( program );
}
// Now override root program with default VirtualProgram for three bottom cows
if( 1 )
{
VirtualProgram * virtualProgram = new VirtualProgram( );
// Create main shader which declares and calls ColorFilter function
osg::Shader * main = new osg::Shader( osg::Shader::FRAGMENT );
main->setShaderSource(
"vec4 ColorFilter( in vec4 color ); \n"
"uniform sampler2D base; \n"
"void main(void) \n"
"{ \n"
" gl_FragColor = gl_Color * texture2DProj( base, gl_TexCoord[0] ); \n"
" gl_FragColor = ColorFilter( gl_FragColor ); \n"
"}\n"
);
virtualProgram->setShader( "main", main );
main->setName( "Virtual Main" );
// Create filter shader which implements greem ColorFilter function
osg::Shader * colorFilter = new osg::Shader( osg::Shader::FRAGMENT );
colorFilter->setShaderSource(
"vec4 ColorFilter( in vec4 color ) \n"
"{ \n"
" return color * vec4( 0.0, 1.0, 0.0, 1.0 ); \n"
"}\n"
);
colorFilter->setName( "Virtual Green" );
virtualProgram->setShader( "ColorFilter", colorFilter );
group->getOrCreateStateSet( )->setAttributeAndModes( virtualProgram );
}
// Create "incomplete" VirtualProgram overriding ColorFilter function
// Lower left cow drawn will be red
if( 1 )
{
VirtualProgram * virtualProgram = new VirtualProgram();
osg::Shader * redFilter = new osg::Shader( osg::Shader::FRAGMENT );
redFilter->setShaderSource(
"vec4 ColorFilter( in vec4 color ) \n"
"{ \n"
" return color * vec4( 1, 0, 0, 1 ); \n"
"}\n"
);
virtualProgram->setShader( "ColorFilter", redFilter );
redFilter->setName( "Virtual Red" );
transform2->getOrCreateStateSet( )->setAttribute( virtualProgram );
}
// Create "incomplete" VirtualProgram overriding ColorFilter function
// Lower right cow will be drawn with grid pattern on yellow background
if( 1 )
{
VirtualProgram * virtualProgram = new VirtualProgram();
osg::Shader * gridFilter = new osg::Shader( osg::Shader::FRAGMENT );
gridFilter->setShaderSource(
"vec4 ColorFilter( in vec4 color ) \n"
"{ \n"
" vec2 grid = clamp( mod( gl_FragCoord.xy, 16.0 ), 0.0, 1.0 ); \n"
" return color * vec4( grid, 0.0, 1.0 ); \n"
"}\n"
);
virtualProgram->setShader( "ColorFilter", gridFilter );
gridFilter->setName( "Virtual Grid" );
transform3->getOrCreateStateSet( )->setAttribute( virtualProgram );
}
return root;
}//////////////////////////////////////////
|