root/OpenSceneGraph/trunk/examples/osggeometryshaders/osggeometryshaders.cpp @ 13574

Revision 13574, 5.6 kB (checked in by robert, 92 minutes ago)

From Jannik Heller, "I noticed the rotation in the OrbitManipulator? depends on the framerate. To reproduce this issue, start the osganimate example, rotate the model with the left mouse button, then let go of the mouse button while still moving. You will notice that with V-Sync enabled, the model rotates slower.

The OrbitManipulator? calculates a scale to counteract the framerate dependency, but it turns out this scale wasn't used for the rotation yet."

  • Property svn:eol-style set to native
RevLine 
[7776]1/* OpenSceneGraph example, osgshaders2
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19/* file:        examples/osgshaders2/osgshaders2.cpp
20 * author:      Mike Weiblen 2008-01-03
21 * copyright:   (C) 2008 Zebra Imaging
22 * license:     OpenSceneGraph Public License (OSGPL)
23 *
24 * A demo of GLSL geometry shaders using OSG
25 * Tested on Dell Precision M4300 w/ NVIDIA Quadro FX 360M
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// play with these #defines to see their effect
42#define ENABLE_GLSL
43#define ENABLE_GEOMETRY_SHADER
44
45///////////////////////////////////////////////////////////////////////////
46
47#ifdef ENABLE_GLSL
48
49class SineAnimation: public osg::Uniform::Callback
50{
51public:
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
63private:
64    const float _rate;
65    const float _scale;
66    const float _offset;
67};
68
69///////////////////////////////////////////////////////////////////////////
70
71static 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
83static const char* geomSource = {
84"#version 120\n"
85"#extension GL_EXT_geometry_shader4 : enable\n"
86"uniform float u_anim1;\n"
[8871]87"varying in vec4 v_color[];\n"
88"varying out vec4 v_color_out;\n"
[7776]89"void main(void)\n"
90"{\n"
91"    vec4 v = gl_PositionIn[0];\n"
[8871]92"    v_color_out = v + v_color[0];\n"
[7776]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
105static const char* fragSource = {
106"#version 120\n"
107"#extension GL_EXT_geometry_shader4 : enable\n"
108"uniform float u_anim1;\n"
[8871]109"varying vec4 v_color_out;\n"
[7776]110"void main(void)\n"
111"{\n"
[8871]112"    gl_FragColor = v_color_out;\n"
[7776]113"}\n"
114};
115
116osg::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
138class SomePoints : public osg::Geometry
139{
140public:
141    SomePoints()
142    {
143        osg::Vec4Array* cAry = new osg::Vec4Array;
[13574]144        setColorArray( cAry, osg::Array::BIND_OVERALL );
[7776]145        cAry->push_back( osg::Vec4(1,1,1,1) );
146
147        osg::Vec3Array* vAry = new osg::Vec3Array;
148        setVertexArray( vAry );
149        vAry->push_back( osg::Vec3(0,0,0) );
150        vAry->push_back( osg::Vec3(0,1,0) );
151        vAry->push_back( osg::Vec3(1,0,0) );
152        vAry->push_back( osg::Vec3(1,1,0) );
153        vAry->push_back( osg::Vec3(0,0,1) );
154        vAry->push_back( osg::Vec3(0,1,1) );
155        vAry->push_back( osg::Vec3(1,0,1) );
156        vAry->push_back( osg::Vec3(1,1,1) );
157
158        addPrimitiveSet( new osg::DrawArrays( GL_POINTS, 0, vAry->size() ) );
159
160        osg::StateSet* sset = getOrCreateStateSet();
161        sset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
162
163        // if things go wrong, fall back to big points
164        osg::Point* p = new osg::Point;
165        p->setSize(6);
166        sset->setAttribute( p );
167
168#ifdef ENABLE_GLSL
169        sset->setAttribute( createShader() );
170
171        // a generic cyclic animation value
172        osg::Uniform* u_anim1( new osg::Uniform( "u_anim1", 0.0f ) );
173        u_anim1->setUpdateCallback( new SineAnimation( 4, 0.5, 0.5 ) );
174        sset->addUniform( u_anim1 );
175#endif
176    }
177};
178
179///////////////////////////////////////////////////////////////////////////
180
[9473]181int main( int , char** )
[7776]182{
183    osg::Geode* root( new osg::Geode );
184    root->addDrawable( new SomePoints );
185
186    osgViewer::Viewer viewer;
187    viewer.setSceneData( root );
188    return viewer.run();
189}
190
191// vim: set sw=4 ts=8 et ic ai:
Note: See TracBrowser for help on using the browser.