root/OpenSceneGraph/trunk/examples/osgprerender/osgprerender.cpp @ 1727

Revision 1727, 13.4 kB (checked in by robert, 11 years ago)

Added support for setting up VisualChoose? with destination alpha, stencil and
quad buffers attributes from DisplaySettings? values.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/GLExtensions>
2#include <osg/Node>
3#include <osg/Geometry>
4#include <osg/Notify>
5#include <osg/MatrixTransform>
6#include <osg/Texture2D>
7#include <osg/Stencil>
8#include <osg/ColorMask>
9#include <osg/Depth>
10#include <osg/Billboard>
11#include <osg/Material>
12
13#include <osgGA/TrackballManipulator>
14#include <osgGA/FlightManipulator>
15#include <osgGA/DriveManipulator>
16
17#include <osgUtil/TransformCallback>
18#include <osgUtil/RenderToTextureStage>
19#include <osgUtil/SmoothingVisitor>
20
21#include <osgDB/Registry>
22#include <osgDB/ReadFile>
23
24#include <osgProducer/Viewer>
25
26class MyUpdateCallback : public osg::NodeCallback
27{
28    public:
29   
30        MyUpdateCallback(osg::Node* subgraph):
31            _subgraph(subgraph) {}
32
33        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
34        {
35            // traverse the subgraph to update any nodes.
36            if (_subgraph.valid()) _subgraph->accept(*nv);
37       
38            // must traverse the Node's subgraph           
39            traverse(node,nv);
40        }
41       
42        osg::ref_ptr<osg::Node>     _subgraph;
43};
44
45class MyCullCallback : public osg::NodeCallback
46{
47    public:
48   
49        MyCullCallback(osg::Node* subgraph,osg::Texture2D* texture):
50            _subgraph(subgraph),
51            _texture(texture),
52            _localState(new osg::StateSet) {}
53
54        MyCullCallback(osg::Node* subgraph,osg::Image* image):
55            _subgraph(subgraph),
56            _image(image),
57            _localState(new osg::StateSet) {}
58       
59        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
60        {
61
62            osgUtil::CullVisitor* cullVisitor = dynamic_cast<osgUtil::CullVisitor*>(nv);
63            if (cullVisitor && (_texture.valid()|| _image.valid()) && _subgraph.valid())
64            {           
65                doPreRender(*node,*cullVisitor);
66
67                // must traverse the subgraph           
68                traverse(node,nv);
69
70            }
71            else
72            {
73                // must traverse the subgraph           
74                traverse(node,nv);
75            }
76        }
77       
78        void doPreRender(osg::Node& node, osgUtil::CullVisitor& cv);
79       
80        osg::ref_ptr<osg::Node>      _subgraph;
81        osg::ref_ptr<osg::Texture2D> _texture;
82        osg::ref_ptr<osg::Image>     _image;
83        osg::ref_ptr<osg::StateSet>  _localState;
84   
85};
86
87void MyCullCallback::doPreRender(osg::Node&, osgUtil::CullVisitor& cv)
88{   
89    const osg::BoundingSphere& bs = _subgraph->getBound();
90    if (!bs.valid())
91    {
92        osg::notify(osg::WARN) << "bb invalid"<<_subgraph.get()<<std::endl;
93        return;
94    }
95   
96
97    // create the render to texture stage.
98    osg::ref_ptr<osgUtil::RenderToTextureStage> rtts = new osgUtil::RenderToTextureStage;
99
100    // set up lighting.
101    // currently ignore lights in the scene graph itself..
102    // will do later.
103    osgUtil::RenderStage* previous_stage = cv.getCurrentRenderBin()->_stage;
104
105    // set up the background color and clear mask.
106    rtts->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
107    rtts->setClearMask(previous_stage->getClearMask());
108
109    // set up to charge the same RenderStageLighting is the parent previous stage.
110    rtts->setRenderStageLighting(previous_stage->getRenderStageLighting());
111
112
113    // record the render bin, to be restored after creation
114    // of the render to text
115    osgUtil::RenderBin* previousRenderBin = cv.getCurrentRenderBin();
116
117    // set the current renderbin to be the newly created stage.
118    cv.setCurrentRenderBin(rtts.get());
119
120
121    float znear = 1.0f*bs.radius();
122    float zfar  = 3.0f*bs.radius();
123       
124    // 2:1 aspect ratio as per flag geomtry below.
125    float top   = 0.25f*znear;
126    float right = 0.5f*znear;
127
128    znear *= 0.9f;
129    zfar *= 1.1f;
130
131    // set up projection.
132    osg::RefMatrix* projection = new osg::RefMatrix;
133    projection->makeFrustum(-right,right,-top,top,znear,zfar);
134
135    cv.pushProjectionMatrix(projection);
136
137    osg::RefMatrix* matrix = new osg::RefMatrix;
138    matrix->makeLookAt(bs.center()+osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
139
140    cv.pushModelViewMatrix(matrix);
141
142    cv.pushStateSet(_localState.get());
143
144    {
145
146        // traverse the subgraph
147        _subgraph->accept(cv);
148
149    }
150
151    cv.popStateSet();
152
153    // restore the previous model view matrix.
154    cv.popModelViewMatrix();
155
156    // restore the previous model view matrix.
157    cv.popProjectionMatrix();
158
159    // restore the previous renderbin.
160    cv.setCurrentRenderBin(previousRenderBin);
161
162    if (rtts->_renderGraphList.size()==0 && rtts->_bins.size()==0)
163    {
164        // getting to this point means that all the subgraph has been
165        // culled by small feature culling or is beyond LOD ranges.
166        return;
167    }
168
169
170
171    int height = 256;
172    int width  = 512;
173
174
175    const osg::Viewport& viewport = *cv.getViewport();
176
177    // offset the impostor viewport from the center of the main window
178    // viewport as often the edges of the viewport might be obscured by
179    // other windows, which can cause image/reading writing problems.
180    int center_x = viewport.x()+viewport.width()/2;
181    int center_y = viewport.y()+viewport.height()/2;
182
183    osg::Viewport* new_viewport = new osg::Viewport;
184    new_viewport->setViewport(center_x-width/2,center_y-height/2,width,height);
185    rtts->setViewport(new_viewport);
186   
187    _localState->setAttribute(new_viewport);
188
189    // and the render to texture stage to the current stages
190    // dependancy list.
191    cv.getCurrentRenderBin()->_stage->addToDependencyList(rtts.get());
192
193    // if one exist attach texture to the RenderToTextureStage.
194    if (_texture.valid()) rtts->setTexture(_texture.get());
195
196    // if one exist attach image to the RenderToTextureStage.
197    if (_image.valid()) rtts->setImage(_image.get());
198
199}
200
201// call back which cretes a deformation field to oscilate the model.
202class MyGeometryCallback :
203    public osg::Drawable::UpdateCallback,
204    public osg::Drawable::AttributeFunctor
205{
206    public:
207   
208        MyGeometryCallback(const osg::Vec3& o,
209                           const osg::Vec3& x,const osg::Vec3& y,const osg::Vec3& z,
210                           double period,double xphase,double amplitude):
211            _firstCall(true),
212            _startTime(0.0),
213            _time(0.0),
214            _period(period),
215            _xphase(xphase),
216            _amplitude(amplitude),
217            _origin(o),
218            _xAxis(x),
219            _yAxis(y),
220            _zAxis(z) {}
221   
222        virtual void update(osg::NodeVisitor* nv,osg::Drawable* drawable)
223        {
224            const osg::FrameStamp* fs = nv->getFrameStamp();
225            double referenceTime = fs->getReferenceTime();
226            if (_firstCall)
227            {
228                _firstCall = false;
229                _startTime = referenceTime;
230            }
231           
232            _time = referenceTime-_startTime;
233           
234            drawable->accept(*this);
235            drawable->dirtyBound();
236           
237            osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(drawable);
238            if (geometry)
239            {
240                osgUtil::SmoothingVisitor::smooth(*geometry);
241            }
242           
243        }
244       
245        virtual void apply(osg::Drawable::AttributeType type,unsigned int count,osg::Vec3* begin)
246        {
247            if (type == osg::Drawable::VERTICES)
248            {
249                const float TwoPI=2.0f*osg::PI;
250                const float phase = -_time/_period;
251               
252                osg::Vec3* end = begin+count;
253                for (osg::Vec3* itr=begin;itr<end;++itr)
254                {
255                    osg::Vec3 dv(*itr-_origin);
256                    osg::Vec3 local(dv*_xAxis,dv*_yAxis,dv*_zAxis);
257                   
258                    local.z() = local.x()*_amplitude*
259                                sinf(TwoPI*(phase+local.x()*_xphase));
260                   
261                    (*itr) = _origin +
262                             _xAxis*local.x()+
263                             _yAxis*local.y()+
264                             _zAxis*local.z();
265                }
266            }
267        }
268
269        bool    _firstCall;
270
271        double  _startTime;
272        double  _time;
273       
274        double  _period;
275        double  _xphase;
276        float   _amplitude;
277
278        osg::Vec3   _origin;
279        osg::Vec3   _xAxis;
280        osg::Vec3   _yAxis;
281        osg::Vec3   _zAxis;
282       
283};
284
285osg::Node* createPreRenderSubGraph(osg::Node* subgraph)
286{
287    if (!subgraph) return 0;
288 
289    // create the quad to visualize.
290    osg::Geometry* polyGeom = new osg::Geometry();
291
292    polyGeom->setSupportsDisplayList(false);
293
294    osg::Vec3 origin(0.0f,0.0f,0.0f);
295    osg::Vec3 xAxis(1.0f,0.0f,0.0f);
296    osg::Vec3 yAxis(0.0f,0.0f,1.0f);
297    osg::Vec3 zAxis(0.0f,-1.0f,0.0f);
298    float height = 100.0f;
299    float width = 200.0f;
300    int noSteps = 20;
301   
302    osg::Vec3Array* vertices = new osg::Vec3Array;
303    osg::Vec3 bottom = origin;
304    osg::Vec3 top = origin; top.z()+= height;
305    osg::Vec3 dv = xAxis*(width/((float)(noSteps-1)));
306   
307    osg::Vec2Array* texcoords = new osg::Vec2Array;
308    osg::Vec2 bottom_texcoord(0.0f,0.0f);
309    osg::Vec2 top_texcoord(0.0f,1.0f);
310    osg::Vec2 dv_texcoord(1.0f/(float)(noSteps-1),0.0f);
311
312    for(int i=0;i<noSteps;++i)
313    {
314        vertices->push_back(top);
315        vertices->push_back(bottom);
316        top+=dv;
317        bottom+=dv;
318
319        texcoords->push_back(top_texcoord);
320        texcoords->push_back(bottom_texcoord);
321        top_texcoord+=dv_texcoord;
322        bottom_texcoord+=dv_texcoord;
323    }
324   
325
326    // pass the created vertex array to the points geometry object.
327    polyGeom->setVertexArray(vertices);
328   
329    polyGeom->setTexCoordArray(0,texcoords);
330   
331
332    osg::Vec4Array* colors = new osg::Vec4Array;
333    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
334    polyGeom->setColorArray(colors);
335    polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
336
337    polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,vertices->size()));
338
339    // new we need to add the texture to the Drawable, we do so by creating a
340    // StateSet to contain the Texture StateAttribute.
341    osg::StateSet* stateset = new osg::StateSet;
342
343    osg::Texture2D* texture = new osg::Texture2D;
344    texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
345    texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
346    stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
347
348    polyGeom->setStateSet(stateset);
349
350    polyGeom->setUpdateCallback(new MyGeometryCallback(origin,xAxis,yAxis,zAxis,1.0,1.0/width,0.2f));
351
352    osg::Geode* geode = new osg::Geode();
353    geode->addDrawable(polyGeom);
354
355    osg::Group* parent = new osg::Group;
356   
357    parent->setUpdateCallback(new MyUpdateCallback(subgraph));
358   
359    parent->setCullCallback(new MyCullCallback(subgraph,texture));
360 
361    parent->addChild(geode);
362   
363    return parent;
364}
365
366int main( int argc, char **argv )
367{
368    // use an ArgumentParser object to manage the program arguments.
369    osg::ArgumentParser arguments(&argc,argv);
370
371    // set up the usage document, in case we need to print out how to use this program.
372    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
373    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
374   
375    // construct the viewer.
376    osgProducer::Viewer viewer(arguments);
377
378    // set up the value with sensible default event handlers.
379    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
380
381    // get details on keyboard and mouse bindings used by the viewer.
382    viewer.getUsage(*arguments.getApplicationUsage());
383
384    // if user request help write it out to cout.
385    if (arguments.read("-h") || arguments.read("--help"))
386    {
387        arguments.getApplicationUsage()->write(std::cout);
388        return 1;
389    }
390
391    // any option left unread are converted into errors to write out later.
392    arguments.reportRemainingOptionsAsUnrecognized();
393
394    // report any errors if they have occured when parsing the program aguments.
395    if (arguments.errors())
396    {
397        arguments.writeErrorMessages(std::cout);
398        return 1;
399    }
400
401   
402    // load the nodes from the commandline arguments.
403    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
404    if (!loadedModel)
405    {
406//        write_usage(osg::notify(osg::NOTICE),argv[0]);
407        return 1;
408    }
409   
410    // create a transform to spin the model.
411    osg::MatrixTransform* loadedModelTransform = new osg::MatrixTransform;
412    loadedModelTransform->addChild(loadedModel);
413
414    osg::NodeCallback* nc = new osgUtil::TransformCallback(loadedModelTransform->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f));
415    loadedModelTransform->setUpdateCallback(nc);
416
417    osg::Group* rootNode = new osg::Group();
418//    rootNode->addChild(loadedModelTransform);
419    rootNode->addChild(createPreRenderSubGraph(loadedModelTransform));
420
421
422    // add model to the viewer.
423    viewer.setSceneData( rootNode );
424
425
426    // create the windows and run the threads.
427    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
428
429    while( !viewer.done() )
430    {
431        // wait for all cull and draw threads to complete.
432        viewer.sync();
433
434        // update the scene by traversing it with the the update visitor which will
435        // call all node update callbacks and animations.
436        viewer.update();
437         
438        // fire off the cull and draw traversals of the scene.
439        viewer.frame();
440       
441    }
442
443    return 0;
444}
Note: See TracBrowser for help on using the browser.