| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| [5076] | 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osgDB/ReadFile> |
|---|
| 20 | #include <osgDB/FileUtils> |
|---|
| 21 | #include <osgUtil/Optimizer> |
|---|
| [5078] | 22 | #include <osgUtil/CullVisitor> |
|---|
| [9413] | 23 | #include <osg/ClipNode> |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | #include <osgGA/TrackballManipulator> |
|---|
| 27 | #include <osgGA/FlightManipulator> |
|---|
| 28 | #include <osgGA/DriveManipulator> |
|---|
| 29 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 30 | #include <osgGA/StateSetManipulator> |
|---|
| 31 | #include <osgGA/AnimationPathManipulator> |
|---|
| 32 | #include <osgGA/TerrainManipulator> |
|---|
| 33 | |
|---|
| [5954] | 34 | #include <osgViewer/Viewer> |
|---|
| [5076] | 35 | |
|---|
| [5103] | 36 | #include <osg/MatrixTransform> |
|---|
| 37 | #include <osgUtil/TransformCallback> |
|---|
| [5102] | 38 | #include <osgParticle/PrecipitationEffect> |
|---|
| [5076] | 39 | |
|---|
| [5954] | 40 | #include <iostream> |
|---|
| 41 | |
|---|
| [5103] | 42 | class MyGustCallback : public osg::NodeCallback |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | public: |
|---|
| 46 | |
|---|
| 47 | MyGustCallback() {} |
|---|
| 48 | |
|---|
| 49 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 50 | { |
|---|
| 51 | osgParticle::PrecipitationEffect* pe = dynamic_cast<osgParticle::PrecipitationEffect*>(node); |
|---|
| 52 | |
|---|
| [6051] | 53 | float value = sin(nv->getFrameStamp()->getSimulationTime()); |
|---|
| [5111] | 54 | if (value<-0.5) |
|---|
| 55 | { |
|---|
| 56 | pe->snow(1.0); |
|---|
| 57 | } |
|---|
| 58 | else |
|---|
| 59 | { |
|---|
| 60 | pe->rain(0.5); |
|---|
| 61 | } |
|---|
| [5103] | 62 | |
|---|
| 63 | traverse(node, nv); |
|---|
| 64 | } |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| [5076] | 67 | |
|---|
| 68 | int main( int argc, char **argv ) |
|---|
| 69 | { |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 76 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds.."); |
|---|
| 77 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 78 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| [5954] | 79 | arguments.getApplicationUsage()->addCommandLineOption("--snow <density>","Set the snow with a density between 0 and 1.0"); |
|---|
| 80 | arguments.getApplicationUsage()->addCommandLineOption("--rain <density>",""); |
|---|
| 81 | arguments.getApplicationUsage()->addCommandLineOption("--particleSize <size>",""); |
|---|
| 82 | arguments.getApplicationUsage()->addCommandLineOption("--particleColour <red> <green> <blue> <alpha>",""); |
|---|
| 83 | arguments.getApplicationUsage()->addCommandLineOption("--wind <x> <y> <z>","Set the wind speed in model coordinates"); |
|---|
| 84 | arguments.getApplicationUsage()->addCommandLineOption("--particleSpeed <float>","Set the particle speed"); |
|---|
| 85 | arguments.getApplicationUsage()->addCommandLineOption("--nearTransition <distance>","Set the near transistion distance"); |
|---|
| 86 | arguments.getApplicationUsage()->addCommandLineOption("--farTransition <distance>","Set the far transistion distance"); |
|---|
| 87 | arguments.getApplicationUsage()->addCommandLineOption("--particleDensity <density>","Set the particle density"); |
|---|
| 88 | arguments.getApplicationUsage()->addCommandLineOption("--cellSize <x> <y> <z>","Set the cell size in model coordinates"); |
|---|
| 89 | arguments.getApplicationUsage()->addCommandLineOption("--fogDensity <density>","Set the fog density"); |
|---|
| 90 | arguments.getApplicationUsage()->addCommandLineOption("--fogColour <red> <green> <blue> <alpha>","Set fog colour."); |
|---|
| 91 | arguments.getApplicationUsage()->addCommandLineOption("-useFarLineSegments","Switch on the use of line segments"); |
|---|
| [5076] | 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| [5954] | 95 | osgViewer::Viewer viewer; |
|---|
| [5076] | 96 | |
|---|
| [9413] | 97 | |
|---|
| 98 | { |
|---|
| 99 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 100 | |
|---|
| 101 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 102 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 103 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 104 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 105 | |
|---|
| 106 | std::string pathfile; |
|---|
| 107 | char keyForAnimationPath = '5'; |
|---|
| 108 | while (arguments.read("-p",pathfile)) |
|---|
| 109 | { |
|---|
| 110 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 111 | if (apm || !apm->valid()) |
|---|
| 112 | { |
|---|
| 113 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 114 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 115 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 116 | ++keyForAnimationPath; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| [5099] | 123 | osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect; |
|---|
| [5076] | 124 | |
|---|
| [5087] | 125 | float intensity; |
|---|
| [5108] | 126 | while (arguments.read("--snow", intensity)) precipitationEffect->snow(intensity); |
|---|
| 127 | while (arguments.read("--rain", intensity)) precipitationEffect->rain(intensity); |
|---|
| [5087] | 128 | |
|---|
| [5108] | 129 | float value; |
|---|
| 130 | while (arguments.read("--particleSize", value)) precipitationEffect->setParticleSize(value); |
|---|
| [5087] | 131 | |
|---|
| [5108] | 132 | osg::Vec4 color; |
|---|
| 133 | while (arguments.read("--particleColor", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color); |
|---|
| 134 | while (arguments.read("--particleColour", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color); |
|---|
| 135 | |
|---|
| [5103] | 136 | osg::Vec3 wind; |
|---|
| [5108] | 137 | while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) precipitationEffect->setWind(wind); |
|---|
| [5103] | 138 | |
|---|
| [5108] | 139 | while (arguments.read("--particleSpeed", value)) precipitationEffect->setParticleSpeed(value); |
|---|
| [5087] | 140 | |
|---|
| [5108] | 141 | while (arguments.read("--nearTransition", value )) precipitationEffect->setNearTransition(value); |
|---|
| 142 | while (arguments.read("--farTransition", value )) precipitationEffect->setFarTransition(value); |
|---|
| [5087] | 143 | |
|---|
| [5108] | 144 | while (arguments.read("--particleDensity", value )) precipitationEffect->setMaximumParticleDensity(value); |
|---|
| [5088] | 145 | |
|---|
| [5108] | 146 | osg::Vec3 cellSize; |
|---|
| 147 | while (arguments.read("--cellSize", cellSize.x(), cellSize.y(), cellSize.z())) precipitationEffect->setCellSize(cellSize); |
|---|
| [5088] | 148 | |
|---|
| [9413] | 149 | double clipDistance = 0.0; |
|---|
| 150 | while (arguments.read("--clip",clipDistance)) {} |
|---|
| 151 | |
|---|
| [5108] | 152 | osg::BoundingBox bb; |
|---|
| 153 | while (arguments.read("--boundingBox", bb.xMin(), |
|---|
| 154 | bb.yMin(), |
|---|
| 155 | bb.zMin(), |
|---|
| 156 | bb.xMax(), |
|---|
| 157 | bb.yMax(), |
|---|
| 158 | bb.zMax())) {} |
|---|
| [5088] | 159 | |
|---|
| [5108] | 160 | while (arguments.read("--fogDensity", value )) precipitationEffect->getFog()->setDensity(value); |
|---|
| 161 | while (arguments.read("--fogColor", color.r(), color.g(), color.b(), color.a() )) precipitationEffect->getFog()->setColor(color); |
|---|
| 162 | while (arguments.read("--fogColour", color.r(), color.g(), color.b(), color.a() )) precipitationEffect->getFog()->setColor(color); |
|---|
| [5088] | 163 | |
|---|
| [5112] | 164 | while (arguments.read("--useFarLineSegments")) { precipitationEffect->setUseFarLineSegments(true); } |
|---|
| [5091] | 165 | |
|---|
| [5099] | 166 | |
|---|
| [5954] | 167 | viewer.getCamera()->setClearColor( precipitationEffect->getFog()->getColor() ); |
|---|
| [5099] | 168 | |
|---|
| 169 | |
|---|
| [5076] | 170 | |
|---|
| 171 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 172 | { |
|---|
| 173 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 174 | return 1; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 179 | if (!loadedModel) |
|---|
| 180 | { |
|---|
| 181 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 182 | return 1; |
|---|
| 183 | } |
|---|
| [5099] | 184 | |
|---|
| [5103] | 185 | |
|---|
| [5108] | 186 | |
|---|
| [5099] | 187 | osg::ref_ptr<osg::Group> group = new osg::Group; |
|---|
| [9413] | 188 | |
|---|
| 189 | if (clipDistance!=0.0) |
|---|
| 190 | { |
|---|
| 191 | osg::ref_ptr<osg::ClipNode> clipNode = new osg::ClipNode; |
|---|
| 192 | clipNode->addClipPlane( new osg::ClipPlane( 0 ) ); |
|---|
| 193 | clipNode->getClipPlane(0)->setClipPlane( 0.0, 0.0, -1.0, -clipDistance ); |
|---|
| 194 | clipNode->setReferenceFrame(osg::ClipNode::ABSOLUTE_RF); |
|---|
| 195 | clipNode->addChild(precipitationEffect.get()); |
|---|
| [5108] | 196 | |
|---|
| [9413] | 197 | group->addChild(clipNode.get()); |
|---|
| 198 | } |
|---|
| 199 | else |
|---|
| 200 | { |
|---|
| 201 | group->addChild(precipitationEffect.get()); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| [5099] | 204 | group->addChild(loadedModel.get()); |
|---|
| [9413] | 205 | |
|---|
| [5108] | 206 | loadedModel->getOrCreateStateSet()->setAttributeAndModes(precipitationEffect->getFog()); |
|---|
| [5099] | 207 | |
|---|
| [5108] | 208 | |
|---|
| 209 | osg::LightSource* lightSource = new osg::LightSource; |
|---|
| 210 | group->addChild(lightSource); |
|---|
| 211 | |
|---|
| 212 | osg::Light* light = lightSource->getLight(); |
|---|
| 213 | light->setLightNum(0); |
|---|
| 214 | light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); |
|---|
| 215 | light->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f)); |
|---|
| 216 | light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); |
|---|
| 217 | light->setSpecular(osg::Vec4(0.2f,0.2f,0.2f,1.0f)); |
|---|
| 218 | |
|---|
| 219 | |
|---|
| [5099] | 220 | |
|---|
| 221 | viewer.setSceneData(group.get()); |
|---|
| [5076] | 222 | |
|---|
| [5954] | 223 | return viewer.run(); |
|---|
| [5076] | 224 | } |
|---|