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

Revision 6051, 3.7 kB (checked in by robert, 8 years ago)

Added osg::FrameStamp::set/getSimulationTime().

Added setting of osg_SimulationTime and osg_DeltaSimulationTime to the uniforms set by SceneView?

Added frame(double simulationTime) and advance(double simulationTime) parameters to
osgViewer::SimpleViewer?, Vewer and CompositeViewer?.

Updated various examples and Nodes to use SimulationTime? where appropriate.

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