| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| 21 | #include <osgUtil/Optimizer> |
|---|
| 22 | #include <osgViewer/Viewer> |
|---|
| 23 | #include <iostream> |
|---|
| 24 | |
|---|
| 25 | class MotionBlurOperation: public osg::Operation |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 55 | double s = powf(0.2, dt / persistence_); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | glAccum(GL_MULT, s); |
|---|
| 59 | glAccum(GL_ACCUM, 1 - s); |
|---|
| 60 | glAccum(GL_RETURN, 1.0f); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | private: |
|---|
| 64 | bool cleared_; |
|---|
| 65 | double t0_; |
|---|
| 66 | double persistence_; |
|---|
| 67 | }; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | int main( int argc, char **argv ) |
|---|
| 71 | { |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 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 | |
|---|
| 85 | osgViewer::Viewer viewer; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 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 | |
|---|
| 98 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osgt"); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | if (!loadedModel) |
|---|
| 105 | { |
|---|
| 106 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 107 | return 1; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | osg::DisplaySettings::instance()->setMinimumNumAccumBits(8,8,8,8); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | viewer.setSceneData(loadedModel.get()); |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 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 | } |
|---|