root/OpenSceneGraph/trunk/examples/osgmotionblur/osgmotionblur.cpp @ 5328

Revision 5328, 5.6 kB (checked in by robert, 8 years ago)

Updated copyright years.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified   
4 * freely and without restriction, both in commericial and non commericial applications,
5 * as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*/
11
12#include <osgDB/ReadFile>
13#include <osgUtil/Optimizer>
14#include <osgProducer/Viewer>
15
16class MotionBlurDrawCallback: public osgProducer::OsgSceneHandler::Callback
17{
18public:
19    MotionBlurDrawCallback(double persistence)
20    :    cleared_(false),
21        persistence_(persistence)
22    {
23    }
24
25    virtual void operator()(osgProducer::OsgSceneHandler &handler, Producer::Camera &camera)
26    {
27        double t = handler.getSceneView()->getFrameStamp()->getReferenceTime();
28
29        if (!cleared_)
30        {
31            // clear the accumulation buffer
32            glClearColor(0, 0, 0, 0);
33            glClear(GL_ACCUM_BUFFER_BIT);
34            cleared_ = true;
35            t0_ = t;
36        }
37
38        double dt = fabs(t - t0_);
39        t0_ = t;
40
41        // call the scene handler's draw function
42        handler.drawImplementation(camera);       
43
44        // compute the blur factor
45        double s = powf(0.2, dt / persistence_);
46
47        // scale, accumulate and return
48        glAccum(GL_MULT, s);
49        glAccum(GL_ACCUM, 1 - s);
50        glAccum(GL_RETURN, 1.0f);
51    }
52
53private:
54    bool cleared_;
55    double t0_;
56    double persistence_;
57};
58
59
60int main( int argc, char **argv )
61{
62
63    // use an ArgumentParser object to manage the program arguments.
64    osg::ArgumentParser arguments(&argc,argv);
65   
66    // set up the usage document, in case we need to print out how to use this program.
67    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
68    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an OpenSceneGraph example that shows how to use the accumulation buffer to achieve a simple motion blur effect.");
69    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
70    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
71    arguments.getApplicationUsage()->addCommandLineOption("-P or --persistence","Set the motion blur persistence time");
72   
73
74    // construct the viewer.
75    osgProducer::Viewer viewer(arguments);
76
77    // set up the value with sensible default event handlers.
78    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
79
80    // get details on keyboard and mouse bindings used by the viewer.
81    viewer.getUsage(*arguments.getApplicationUsage());
82
83    // if user request help write it out to cout.
84    if (arguments.read("-h") || arguments.read("--help"))
85    {
86        arguments.getApplicationUsage()->write(std::cout);
87        return 1;
88    }
89
90    double persistence = 0.25;
91    arguments.read("-P", persistence) || arguments.read("--persistence", persistence);
92
93    // report any errors if they have occured when parsing the program aguments.
94    if (arguments.errors())
95    {
96        arguments.writeErrorMessages(std::cout);
97        return 1;
98    }
99   
100    if (arguments.argc()<=1)
101    {
102        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
103        return 1;
104    }
105
106    osg::Timer_t start_tick = osg::Timer::instance()->tick();
107
108    // read the scene from the list of file specified commandline args.
109    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
110
111    // if no model has been successfully loaded report failure.
112    if (!loadedModel)
113    {
114        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
115        return 1;
116    }
117
118    // any option left unread are converted into errors to write out later.
119    arguments.reportRemainingOptionsAsUnrecognized();
120
121    // report any errors if they have occured when parsing the program aguments.
122    if (arguments.errors())
123    {
124        arguments.writeErrorMessages(std::cout);
125    }
126
127    osg::Timer_t end_tick = osg::Timer::instance()->tick();
128
129    std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
130
131    // set the display settings we can to request, OsgCameraGroup will read this.
132    osg::DisplaySettings::instance()->setMinimumNumAccumBits(8,8,8,8);
133
134    // optimize the scene graph, remove rendundent nodes and state etc.
135    osgUtil::Optimizer optimizer;
136    optimizer.optimize(loadedModel.get());
137
138    // pass the loaded scene graph to the viewer.
139    viewer.setSceneData(loadedModel.get());
140
141    // create the windows and run the threads.
142    viewer.realize();
143
144    // set our motion blur callback as the draw callback for each scene handler
145    osgProducer::Viewer::SceneHandlerList &shl = viewer.getSceneHandlerList();
146    for (osgProducer::Viewer::SceneHandlerList::iterator i=shl.begin(); i!=shl.end(); ++i)
147    {
148        (*i)->setDrawCallback(new MotionBlurDrawCallback(persistence));
149    }
150
151    while( !viewer.done() )
152    {
153        // wait for all cull and draw threads to complete.
154        viewer.sync();
155
156        // update the scene by traversing it with the the update visitor which will
157        // call all node update callbacks and animations.
158        viewer.update();
159         
160        // fire off the cull and draw traversals of the scene.
161        viewer.frame();
162       
163    }
164   
165    // wait for all cull and draw threads to complete before exit.
166    viewer.sync();
167
168    return 0;
169}
Note: See TracBrowser for help on using the browser.