| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | #include <osgUtil/Optimizer> |
|---|
| 14 | #include <osgViewer/Viewer> |
|---|
| 15 | #include <iostream> |
|---|
| 16 | |
|---|
| 17 | class MotionBlurOperation: public osg::Operation |
|---|
| 18 | { |
|---|
| 19 | public: |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 47 | double s = powf(0.2, dt / persistence_); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | glAccum(GL_MULT, s); |
|---|
| 51 | glAccum(GL_ACCUM, 1 - s); |
|---|
| 52 | glAccum(GL_RETURN, 1.0f); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | private: |
|---|
| 56 | bool cleared_; |
|---|
| 57 | double t0_; |
|---|
| 58 | double persistence_; |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | int main( int argc, char **argv ) |
|---|
| 63 | { |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 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 | |
|---|
| 77 | osgViewer::Viewer viewer; |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 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 | |
|---|
| 90 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | if (!loadedModel) |
|---|
| 94 | { |
|---|
| 95 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 96 | return 1; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | osg::DisplaySettings::instance()->setMinimumNumAccumBits(8,8,8,8); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | viewer.setSceneData(loadedModel.get()); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | viewer.realize(); |
|---|
| 108 | |
|---|
| 109 | osgViewer::Viewer::Windows windows; |
|---|
| 110 | viewer.getWindows(windows); |
|---|
| 111 | for(osgViewer::Viewer::Windows::iterator itr = windows.begin(); |
|---|
| 112 | itr != windows.end(); |
|---|
| 113 | ++itr) |
|---|
| 114 | { |
|---|
| 115 | (*itr)->add(new MotionBlurOperation(persistence)); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | return viewer.run(); |
|---|
| 119 | } |
|---|