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

Revision 12529, 4.4 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

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