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

Revision 6916, 3.9 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, added fallbacks for when now command line args are provided.

  • 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 <osgViewer/Viewer>
15#include <iostream>
16
17class MotionBlurOperation: public osg::Operation
18{
19public:
20    MotionBlurOperation(double persistence):
21        osg::Operation("MotionBlur",true),
22        cleared_(false),
23        persistence_(persistence)
24    {
25    }
26
27    virtual void operator () (osg::Object* object)
28    {
29        osg::GraphicsContext* gc = dynamic_cast<osg::GraphicsContext*>(object);
30        if (!gc) return;
31   
32        double t = gc->getState()->getFrameStamp()->getSimulationTime();
33
34        if (!cleared_)
35        {
36            // clear the accumulation buffer
37            glClearColor(0, 0, 0, 0);
38            glClear(GL_ACCUM_BUFFER_BIT);
39            cleared_ = true;
40            t0_ = t;
41        }
42
43        double dt = fabs(t - t0_);
44        t0_ = t;
45
46        // compute the blur factor
47        double s = powf(0.2, dt / persistence_);
48
49        // scale, accumulate and return
50        glAccum(GL_MULT, s);
51        glAccum(GL_ACCUM, 1 - s);
52        glAccum(GL_RETURN, 1.0f);
53    }
54
55private:
56    bool cleared_;
57    double t0_;
58    double persistence_;
59};
60
61
62int main( int argc, char **argv )
63{
64
65    // use an ArgumentParser object to manage the program arguments.
66    osg::ArgumentParser arguments(&argc,argv);
67   
68    // set up the usage document, in case we need to print out how to use this program.
69    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
70    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an OpenSceneGraph example that shows how to use the accumulation buffer to achieve a simple motion blur effect.");
71    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
72    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
73    arguments.getApplicationUsage()->addCommandLineOption("-P or --persistence","Set the motion blur persistence time");
74   
75
76    // construct the viewer.
77    osgViewer::Viewer viewer;
78
79    // if user request help write it out to cout.
80    if (arguments.read("-h") || arguments.read("--help"))
81    {
82        arguments.getApplicationUsage()->write(std::cout);
83        return 1;
84    }
85
86    double persistence = 0.25;
87    arguments.read("-P", persistence) || arguments.read("--persistence", persistence);
88
89    // read the scene from the list of file specified commandline args.
90    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
91   
92    // if not loaded assume no arguments passed in, try use default mode instead.
93    if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osg");
94
95    // if no model has been successfully loaded report failure.
96    if (!loadedModel)
97    {
98        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
99        return 1;
100    }
101
102
103    // set the display settings we can to request, OsgCameraGroup will read this.
104    osg::DisplaySettings::instance()->setMinimumNumAccumBits(8,8,8,8);
105
106    // pass the loaded scene graph to the viewer.
107    viewer.setSceneData(loadedModel.get());
108
109    // create the windows and run the threads.
110    viewer.realize();
111
112    osgViewer::Viewer::Windows windows;
113    viewer.getWindows(windows);
114    for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
115        itr != windows.end();
116        ++itr)
117    {
118        (*itr)->add(new MotionBlurOperation(persistence));
119    }
120
121    return viewer.run();
122}
Note: See TracBrowser for help on using the browser.