| 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 | #include <osg/Notify> |
|---|
| 30 | #include <osg/ref_ptr> |
|---|
| 31 | #include <osg/Geode> |
|---|
| 32 | #include <osg/Geometry> |
|---|
| 33 | #include <osg/Point> |
|---|
| 34 | #include <osg/Vec3> |
|---|
| 35 | #include <osg/Vec4> |
|---|
| 36 | #include <osg/Program> |
|---|
| 37 | #include <osg/Shader> |
|---|
| 38 | #include <osg/Uniform> |
|---|
| 39 | #include <osgViewer/Viewer> |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #define ENABLE_GLSL |
|---|
| 43 | #define ENABLE_GEOMETRY_SHADER |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | #ifdef ENABLE_GLSL |
|---|
| 48 | |
|---|
| 49 | class SineAnimation: public osg::Uniform::Callback |
|---|
| 50 | { |
|---|
| 51 | public: |
|---|
| 52 | SineAnimation( float rate = 1.0f, float scale = 1.0f, float offset = 0.0f ) : |
|---|
| 53 | _rate(rate), _scale(scale), _offset(offset) |
|---|
| 54 | {} |
|---|
| 55 | |
|---|
| 56 | void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv ) |
|---|
| 57 | { |
|---|
| 58 | float angle = _rate * nv->getFrameStamp()->getSimulationTime(); |
|---|
| 59 | float value = sinf( angle ) * _scale + _offset; |
|---|
| 60 | uniform->set( value ); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | private: |
|---|
| 64 | const float _rate; |
|---|
| 65 | const float _scale; |
|---|
| 66 | const float _offset; |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | static const char* vertSource = { |
|---|
| 72 | "#version 120\n" |
|---|
| 73 | "#extension GL_EXT_geometry_shader4 : enable\n" |
|---|
| 74 | "uniform float u_anim1;\n" |
|---|
| 75 | "varying vec4 v_color;\n" |
|---|
| 76 | "void main(void)\n" |
|---|
| 77 | "{\n" |
|---|
| 78 | " v_color = gl_Vertex;\n" |
|---|
| 79 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
|---|
| 80 | "}\n" |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | static const char* geomSource = { |
|---|
| 84 | "#version 120\n" |
|---|
| 85 | "#extension GL_EXT_geometry_shader4 : enable\n" |
|---|
| 86 | "uniform float u_anim1;\n" |
|---|
| 87 | "varying in vec4 v_color[];\n" |
|---|
| 88 | "varying out vec4 v_color_out;\n" |
|---|
| 89 | "void main(void)\n" |
|---|
| 90 | "{\n" |
|---|
| 91 | " vec4 v = gl_PositionIn[0];\n" |
|---|
| 92 | " v_color_out = v + v_color[0];\n" |
|---|
| 93 | "\n" |
|---|
| 94 | " gl_Position = v + vec4(u_anim1,0.,0.,0.); EmitVertex();\n" |
|---|
| 95 | " gl_Position = v - vec4(u_anim1,0.,0.,0.); EmitVertex();\n" |
|---|
| 96 | " EndPrimitive();\n" |
|---|
| 97 | "\n" |
|---|
| 98 | " gl_Position = v + vec4(0.,1.0-u_anim1,0.,0.); EmitVertex();\n" |
|---|
| 99 | " gl_Position = v - vec4(0.,1.0-u_anim1,0.,0.); EmitVertex();\n" |
|---|
| 100 | " EndPrimitive();\n" |
|---|
| 101 | "}\n" |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | static const char* fragSource = { |
|---|
| 106 | "#version 120\n" |
|---|
| 107 | "#extension GL_EXT_geometry_shader4 : enable\n" |
|---|
| 108 | "uniform float u_anim1;\n" |
|---|
| 109 | "varying vec4 v_color_out;\n" |
|---|
| 110 | "void main(void)\n" |
|---|
| 111 | "{\n" |
|---|
| 112 | " gl_FragColor = v_color_out;\n" |
|---|
| 113 | "}\n" |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | osg::Program* createShader() |
|---|
| 117 | { |
|---|
| 118 | osg::Program* pgm = new osg::Program; |
|---|
| 119 | pgm->setName( "osgshader2 demo" ); |
|---|
| 120 | |
|---|
| 121 | pgm->addShader( new osg::Shader( osg::Shader::VERTEX, vertSource ) ); |
|---|
| 122 | pgm->addShader( new osg::Shader( osg::Shader::FRAGMENT, fragSource ) ); |
|---|
| 123 | |
|---|
| 124 | #ifdef ENABLE_GEOMETRY_SHADER |
|---|
| 125 | pgm->addShader( new osg::Shader( osg::Shader::GEOMETRY, geomSource ) ); |
|---|
| 126 | pgm->setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, 4 ); |
|---|
| 127 | pgm->setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, GL_POINTS ); |
|---|
| 128 | pgm->setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_LINE_STRIP ); |
|---|
| 129 | #endif |
|---|
| 130 | |
|---|
| 131 | return pgm; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | #endif |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | class SomePoints : public osg::Geometry |
|---|
| 139 | { |
|---|
| 140 | public: |
|---|
| 141 | SomePoints() |
|---|
| 142 | { |
|---|
| 143 | osg::Vec4Array* cAry = new osg::Vec4Array; |
|---|
| 144 | setColorArray( cAry ); |
|---|
| 145 | setColorBinding( osg::Geometry::BIND_OVERALL ); |
|---|
| 146 | cAry->push_back( osg::Vec4(1,1,1,1) ); |
|---|
| 147 | |
|---|
| 148 | osg::Vec3Array* vAry = new osg::Vec3Array; |
|---|
| 149 | setVertexArray( vAry ); |
|---|
| 150 | vAry->push_back( osg::Vec3(0,0,0) ); |
|---|
| 151 | vAry->push_back( osg::Vec3(0,1,0) ); |
|---|
| 152 | vAry->push_back( osg::Vec3(1,0,0) ); |
|---|
| 153 | vAry->push_back( osg::Vec3(1,1,0) ); |
|---|
| 154 | vAry->push_back( osg::Vec3(0,0,1) ); |
|---|
| 155 | vAry->push_back( osg::Vec3(0,1,1) ); |
|---|
| 156 | vAry->push_back( osg::Vec3(1,0,1) ); |
|---|
| 157 | vAry->push_back( osg::Vec3(1,1,1) ); |
|---|
| 158 | |
|---|
| 159 | addPrimitiveSet( new osg::DrawArrays( GL_POINTS, 0, vAry->size() ) ); |
|---|
| 160 | |
|---|
| 161 | osg::StateSet* sset = getOrCreateStateSet(); |
|---|
| 162 | sset->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | osg::Point* p = new osg::Point; |
|---|
| 166 | p->setSize(6); |
|---|
| 167 | sset->setAttribute( p ); |
|---|
| 168 | |
|---|
| 169 | #ifdef ENABLE_GLSL |
|---|
| 170 | sset->setAttribute( createShader() ); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | osg::Uniform* u_anim1( new osg::Uniform( "u_anim1", 0.0f ) ); |
|---|
| 174 | u_anim1->setUpdateCallback( new SineAnimation( 4, 0.5, 0.5 ) ); |
|---|
| 175 | sset->addUniform( u_anim1 ); |
|---|
| 176 | #endif |
|---|
| 177 | } |
|---|
| 178 | }; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | int main( int argc, char *argv[] ) |
|---|
| 183 | { |
|---|
| 184 | osg::Geode* root( new osg::Geode ); |
|---|
| 185 | root->addDrawable( new SomePoints ); |
|---|
| 186 | |
|---|
| 187 | osgViewer::Viewer viewer; |
|---|
| 188 | viewer.setSceneData( root ); |
|---|
| 189 | return viewer.run(); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|