| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | #include <osgDB/FileUtils> |
|---|
| 14 | #include <osgUtil/Optimizer> |
|---|
| 15 | #include <osgUtil/CullVisitor> |
|---|
| 16 | #include <osgViewer/Viewer> |
|---|
| 17 | |
|---|
| 18 | #include <osg/MatrixTransform> |
|---|
| 19 | #include <osgUtil/TransformCallback> |
|---|
| 20 | #include <osgParticle/PrecipitationEffect> |
|---|
| 21 | |
|---|
| 22 | #include <iostream> |
|---|
| 23 | |
|---|
| 24 | class MyGustCallback : public osg::NodeCallback |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | public: |
|---|
| 28 | |
|---|
| 29 | MyGustCallback() {} |
|---|
| 30 | |
|---|
| 31 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 32 | { |
|---|
| 33 | osgParticle::PrecipitationEffect* pe = dynamic_cast<osgParticle::PrecipitationEffect*>(node); |
|---|
| 34 | |
|---|
| 35 | float value = sin(nv->getFrameStamp()->getReferenceTime()); |
|---|
| 36 | if (value<-0.5) |
|---|
| 37 | { |
|---|
| 38 | pe->snow(1.0); |
|---|
| 39 | } |
|---|
| 40 | else |
|---|
| 41 | { |
|---|
| 42 | pe->rain(0.5); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | traverse(node, nv); |
|---|
| 46 | } |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | int main( int argc, char **argv ) |
|---|
| 51 | { |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 58 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds.."); |
|---|
| 59 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 60 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 61 | arguments.getApplicationUsage()->addCommandLineOption("--snow <density>","Set the snow with a density between 0 and 1.0"); |
|---|
| 62 | arguments.getApplicationUsage()->addCommandLineOption("--rain <density>",""); |
|---|
| 63 | arguments.getApplicationUsage()->addCommandLineOption("--particleSize <size>",""); |
|---|
| 64 | arguments.getApplicationUsage()->addCommandLineOption("--particleColour <red> <green> <blue> <alpha>",""); |
|---|
| 65 | arguments.getApplicationUsage()->addCommandLineOption("--wind <x> <y> <z>","Set the wind speed in model coordinates"); |
|---|
| 66 | arguments.getApplicationUsage()->addCommandLineOption("--particleSpeed <float>","Set the particle speed"); |
|---|
| 67 | arguments.getApplicationUsage()->addCommandLineOption("--nearTransition <distance>","Set the near transistion distance"); |
|---|
| 68 | arguments.getApplicationUsage()->addCommandLineOption("--farTransition <distance>","Set the far transistion distance"); |
|---|
| 69 | arguments.getApplicationUsage()->addCommandLineOption("--particleDensity <density>","Set the particle density"); |
|---|
| 70 | arguments.getApplicationUsage()->addCommandLineOption("--cellSize <x> <y> <z>","Set the cell size in model coordinates"); |
|---|
| 71 | arguments.getApplicationUsage()->addCommandLineOption("--fogDensity <density>","Set the fog density"); |
|---|
| 72 | arguments.getApplicationUsage()->addCommandLineOption("--fogColour <red> <green> <blue> <alpha>","Set fog colour."); |
|---|
| 73 | arguments.getApplicationUsage()->addCommandLineOption("-useFarLineSegments","Switch on the use of line segments"); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | osgViewer::Viewer viewer; |
|---|
| 78 | |
|---|
| 79 | osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect; |
|---|
| 80 | |
|---|
| 81 | float intensity; |
|---|
| 82 | while (arguments.read("--snow", intensity)) precipitationEffect->snow(intensity); |
|---|
| 83 | while (arguments.read("--rain", intensity)) precipitationEffect->rain(intensity); |
|---|
| 84 | |
|---|
| 85 | float value; |
|---|
| 86 | while (arguments.read("--particleSize", value)) precipitationEffect->setParticleSize(value); |
|---|
| 87 | |
|---|
| 88 | osg::Vec4 color; |
|---|
| 89 | while (arguments.read("--particleColor", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color); |
|---|
| 90 | while (arguments.read("--particleColour", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color); |
|---|
| 91 | |
|---|
| 92 | osg::Vec3 wind; |
|---|
| 93 | while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) precipitationEffect->setWind(wind); |
|---|
| 94 | |
|---|
| 95 | while (arguments.read("--particleSpeed", value)) precipitationEffect->setParticleSpeed(value); |
|---|
| 96 | |
|---|
| 97 | while (arguments.read("--nearTransition", value )) precipitationEffect->setNearTransition(value); |
|---|
| 98 | while (arguments.read("--farTransition", value )) precipitationEffect->setFarTransition(value); |
|---|
| 99 | |
|---|
| 100 | while (arguments.read("--particleDensity", value )) precipitationEffect->setMaximumParticleDensity(value); |
|---|
| 101 | |
|---|
| 102 | osg::Vec3 cellSize; |
|---|
| 103 | while (arguments.read("--cellSize", cellSize.x(), cellSize.y(), cellSize.z())) precipitationEffect->setCellSize(cellSize); |
|---|
| 104 | |
|---|
| 105 | osg::BoundingBox bb; |
|---|
| 106 | while (arguments.read("--boundingBox", bb.xMin(), |
|---|
| 107 | bb.yMin(), |
|---|
| 108 | bb.zMin(), |
|---|
| 109 | bb.xMax(), |
|---|
| 110 | bb.yMax(), |
|---|
| 111 | bb.zMax())) {} |
|---|
| 112 | |
|---|
| 113 | while (arguments.read("--fogDensity", value )) precipitationEffect->getFog()->setDensity(value); |
|---|
| 114 | while (arguments.read("--fogColor", color.r(), color.g(), color.b(), color.a() )) precipitationEffect->getFog()->setColor(color); |
|---|
| 115 | while (arguments.read("--fogColour", color.r(), color.g(), color.b(), color.a() )) precipitationEffect->getFog()->setColor(color); |
|---|
| 116 | |
|---|
| 117 | while (arguments.read("--useFarLineSegments")) { precipitationEffect->setUseFarLineSegments(true); } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | viewer.getCamera()->setClearColor( precipitationEffect->getFog()->getColor() ); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 125 | { |
|---|
| 126 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 127 | return 1; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 132 | if (!loadedModel) |
|---|
| 133 | { |
|---|
| 134 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 135 | return 1; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | osg::ref_ptr<osg::Group> group = new osg::Group; |
|---|
| 142 | group->addChild(precipitationEffect.get()); |
|---|
| 143 | |
|---|
| 144 | group->addChild(loadedModel.get()); |
|---|
| 145 | loadedModel->getOrCreateStateSet()->setAttributeAndModes(precipitationEffect->getFog()); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | osg::LightSource* lightSource = new osg::LightSource; |
|---|
| 149 | group->addChild(lightSource); |
|---|
| 150 | |
|---|
| 151 | osg::Light* light = lightSource->getLight(); |
|---|
| 152 | light->setLightNum(0); |
|---|
| 153 | light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); |
|---|
| 154 | light->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f)); |
|---|
| 155 | light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); |
|---|
| 156 | light->setSpecular(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | viewer.setSceneData(group.get()); |
|---|
| 161 | |
|---|
| 162 | return viewer.run(); |
|---|
| 163 | } |
|---|