File: osgtransformfeedback.cpp

package info (click to toggle)
openscenegraph 3.6.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 40,100 kB
  • sloc: cpp: 392,065; ansic: 21,495; java: 1,020; yacc: 548; makefile: 430; objc: 406; xml: 155; lex: 151; javascript: 34
file content (292 lines) | stat: -rw-r--r-- 8,661 bytes parent folder | download | duplicates (6)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* OpenSceneGraph example, osgtransformfeedback
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

/* file:        examples/osgtransformfeedback/osgtransformfeedback.cpp
* author:      Julien Valentin 2013-10-01
* copyright:   (C) 2013
* license:     OpenSceneGraph Public License (OSGPL)
*
* A demo of GLSL geometry shaders using OSG transform feedback
*
*/


#include <osg/GL2Extensions>
#include <osg/Notify>
#include <osg/ref_ptr>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Point>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Program>
#include <osg/Shader>
#include <osg/BlendFunc>

#include <osg/Uniform>
#include <osgViewer/Viewer>

#include <osg/BufferIndexBinding>

#include <iostream>

///////////////////////////////////////////////////////////////////////////

class SineAnimation: public osg::UniformCallback
{
public:
    SineAnimation( float rate = 1.0f, float scale = 1.0f, float offset = 0.0f ) :
        _rate(rate), _scale(scale), _offset(offset)
    {}

    void operator()( osg::Uniform* uniform, osg::NodeVisitor* nv )
    {
        float angle = _rate * nv->getFrameStamp()->getSimulationTime();
        float value = sinf( angle ) * _scale + _offset;
        uniform->set( value );
    }

private:
    const float _rate;
    const float _scale;
    const float _offset;
};

///////////////////////////////////////////////////////////////////////////
static const char* RendervertSource =
{
    "#version 120\n"
    "uniform float u_anim1;\n"
    "//in vec4 Vertex;\n"
    "varying vec4 v_color;\n"
    "void main(void)\n"
    "{\n"
    "    v_color = gl_Vertex;\n"
    "    gl_Position = gl_ModelViewProjectionMatrix *(gl_Vertex);\n"
    "}\n"
};
static const char* vertSource =
{
    "#version 120\n"
    "#extension GL_EXT_geometry_shader4 : enable\n"
    "uniform float u_anim1;\n"
    " varying out vec4 v_color;\n"
    "void main(void)\n"
    "{\n"
    "   gl_Position = (gl_Vertex);\n"
    "	v_color = gl_Vertex;\n"
    "}\n"
};



static const char* geomSource =
{
    "#version 120\n"
    "#extension GL_EXT_geometry_shader4 : enable\n"
    "uniform float u_anim1;\n"
    " varying in vec4 v_color[];\n"
    " varying  vec4 out1;\n"
    "void main(void)\n"
    "{\n"
    "    vec4 v =vec4( gl_PositionIn[0].xyz,1);\n"
    " out1 =  v + vec4(u_anim1,0.,0.,0.);//  gl_Position = v + vec4(u_anim1,0.,0.,0.); \n"
    "  EmitVertex();\n"
    "    EndPrimitive();\n"
    "   out1 =  v - vec4(u_anim1,0.,0.,0.); // gl_Position = v - vec4(u_anim1,0.,0.,0.);  \n"
    " EmitVertex();\n"
    "    EndPrimitive();\n"
    "\n"
    "   out1=  v + vec4(0.,1.0-u_anim1,0.,0.);// gl_Position = v + vec4(0.,1.0-u_anim1,0.,0.); \n"
    "  EmitVertex();\n"
    "    EndPrimitive();\n"
    "   out1 =  v - vec4(0.,1.0-u_anim1,0.,0.); //gl_Position = v - vec4(0.,1.0-u_anim1,0.,0.); \n"
    "  EmitVertex();\n"
    "    EndPrimitive();\n"
    "}\n"
};


static const char* fragSource =
{
    "#version 120\n"
    "#extension GL_EXT_geometry_shader4 : enable\n"
    "uniform float u_anim1;\n"
    "varying vec4 v_color_out;\n"
    "void main(void)\n"
    "{\n"
    "    gl_FragColor = vec4(1,0,0,1);//v_color_out;\n"
    "}\n"
};

osg::Program* createGeneratorShader()
{
    osg::Program* pgm = new osg::Program;
    pgm->setName( "osg transformfeedback demo" );
    pgm->addShader( new osg::Shader( osg::Shader::VERTEX,   vertSource ) );
    pgm->setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, 4 );
    pgm->setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, GL_POINTS );
    pgm->setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_POINTS );
    pgm->addShader( new osg::Shader( osg::Shader::GEOMETRY, geomSource ) );
    pgm->addTransformFeedBackVarying(std::string("out1"));
    pgm->setTransformFeedBackMode(GL_INTERLEAVED_ATTRIBS);
    return pgm;
}

osg::Program* createRenderShader()
{
    osg::Program* pgm = new osg::Program;
    pgm->setName( "osg transformfeedback renderer demo" );
    pgm->addShader( new osg::Shader( osg::Shader::VERTEX,   RendervertSource ) );
    pgm->addShader( new osg::Shader( osg::Shader::FRAGMENT, fragSource ) );
    return pgm;
}

//////////////////////////////////////////////////////////////////////////////////////



class SomePointsRenderer;
class SomePointsGenerator:public osg::Geometry
{
public:
    SomePointsGenerator();
    void setRenderer(osg::Geometry*renderer);
    GLuint getNumPrimitivesGenerated()const;

protected:
    osg::Program * _program;
    osg::ref_ptr<osg::VertexBufferObject> genbuffer;//Renderer buffer
    osg::Vec4Array* vAry;

    virtual void drawImplementation( osg::RenderInfo& renderInfo ) const;
};
/////////////////////////////////////////////////////////////////////////////////////

class SomePointsRenderer : public osg::Geometry
{
public:

    SomePointsRenderer(SomePointsGenerator*_generator)
    {

        setUseVertexBufferObjects(true);

        osg::Vec4Array* vAry2 = new osg::Vec4Array;
        vAry2->resize(_generator->getNumPrimitivesGenerated());
        setVertexArray(vAry2);
        addPrimitiveSet( new osg::DrawArrays( GL_LINES, 0,_generator->getNumPrimitivesGenerated()));

        osg::StateSet* sset = getOrCreateStateSet();
        ///hacking rendering order
        /*osg::BlendFunc* bf = new
        osg::BlendFunc(osg::BlendFunc::SRC_ALPHA,
        osg::BlendFunc::ONE_MINUS_SRC_ALPHA );
        sset->setAttributeAndModes(bf);*/

        sset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
        getOrCreateVertexBufferObject();
        sset->setAttribute( createRenderShader() );

    }



};

///////////////////////////////////////////////////////////////////////////
GLuint SomePointsGenerator::getNumPrimitivesGenerated()const
{
    return vAry->size()*4;
}

void SomePointsGenerator::drawImplementation( osg::RenderInfo& renderInfo ) const
{

    //get output buffer
    unsigned int contextID = renderInfo.getState()->getContextID();

    GLuint ubuff= genbuffer->getOrCreateGLBufferObject(contextID)->getGLObjectID();

    osg::GLExtensions* ext = renderInfo.getState()->get<osg::GLExtensions>();

    ext->glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0,ubuff);

    glEnable(GL_RASTERIZER_DISCARD);

    ext->glBeginTransformFeedback(GL_POINTS);


    osg::Geometry::drawImplementation(  renderInfo );


    ext->glEndTransformFeedback();

    glDisable(GL_RASTERIZER_DISCARD);

    ext->glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
}



SomePointsGenerator::SomePointsGenerator():osg::Geometry()
{

    setUseVertexBufferObjects(true);

    osg::StateSet* sset = getOrCreateStateSet();
    sset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    vAry = new osg::Vec4Array;;
    vAry->push_back( osg::Vec4(0,0,0,1) );
    vAry->push_back( osg::Vec4(0,1,0,1) );
    vAry->push_back( osg::Vec4(1,0,0,1) );
    vAry->push_back( osg::Vec4(1,1,0,1 ));
    addPrimitiveSet( new osg::DrawArrays( GL_POINTS, 0, vAry->size() ) );
    setVertexArray( vAry );

    _program=createGeneratorShader() ;
    sset->setAttribute(_program );

    // a generic cyclic animation value
    osg::Uniform* u_anim1( new osg::Uniform( "u_anim1", 0.9f ) );
    u_anim1->setUpdateCallback( new SineAnimation( 4, 0.5, 0.5 ) );
    sset->addUniform( u_anim1 );

}

void SomePointsGenerator::setRenderer(osg::Geometry* renderer)
{
    genbuffer = renderer->getOrCreateVertexBufferObject();
}



///////////////////////////////////////////////////////////////////////////

int main( int , char** )
{
    osg::Geode* root( new osg::Geode );
    SomePointsGenerator * pate = new SomePointsGenerator();
    SomePointsRenderer* pate2 = new SomePointsRenderer(pate);
    pate->setRenderer( pate2);
    root->addDrawable( pate );
    root->addDrawable( pate2 );
    osgViewer::Viewer viewer;
    viewer.setSceneData( root );
    return viewer.run();
}