root/OpenSceneGraph/trunk/examples/osgprecipitation/osgprecipitation.cpp @ 13342

Revision 9413, 9.6 kB (checked in by robert, 6 years ago)

Added support for user defined clipping of the precipitation effect, to test use:

osgpreciptation lz.osg --clip 20


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgprecipitation.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgDB/ReadFile>
20#include <osgDB/FileUtils>
21#include <osgUtil/Optimizer>
22#include <osgUtil/CullVisitor>
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
34#include <osgViewer/Viewer>
35
36#include <osg/MatrixTransform>
37#include <osgUtil/TransformCallback>
38#include <osgParticle/PrecipitationEffect>
39
40#include <iostream>
41
42class 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           
53            float value = sin(nv->getFrameStamp()->getSimulationTime());
54            if (value<-0.5)
55            {
56                pe->snow(1.0);
57            }
58            else
59            {
60                pe->rain(0.5);
61            }
62       
63            traverse(node, nv);
64        }
65};
66
67
68int main( int argc, char **argv )
69{
70
71    // use an ArgumentParser object to manage the program arguments.
72    osg::ArgumentParser arguments(&argc,argv);
73   
74    // set up the usage document, in case we need to print out how to use this program.
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");
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");
92   
93
94    // construct the viewer.
95    osgViewer::Viewer viewer;
96
97    // set up the camera manipulators.
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
123    osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect;
124
125    float intensity;
126    while (arguments.read("--snow", intensity)) precipitationEffect->snow(intensity);
127    while (arguments.read("--rain", intensity)) precipitationEffect->rain(intensity);
128
129    float value;
130    while (arguments.read("--particleSize", value)) precipitationEffect->setParticleSize(value);
131
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
136    osg::Vec3 wind;
137    while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) precipitationEffect->setWind(wind);
138   
139    while (arguments.read("--particleSpeed", value)) precipitationEffect->setParticleSpeed(value);
140
141    while (arguments.read("--nearTransition", value )) precipitationEffect->setNearTransition(value);
142    while (arguments.read("--farTransition", value )) precipitationEffect->setFarTransition(value);
143
144    while (arguments.read("--particleDensity", value )) precipitationEffect->setMaximumParticleDensity(value);
145
146    osg::Vec3 cellSize;
147    while (arguments.read("--cellSize", cellSize.x(), cellSize.y(), cellSize.z())) precipitationEffect->setCellSize(cellSize);
148
149    double clipDistance = 0.0;
150    while (arguments.read("--clip",clipDistance)) {}
151
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())) {}
159
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);
163 
164    while (arguments.read("--useFarLineSegments")) { precipitationEffect->setUseFarLineSegments(true); }
165
166   
167    viewer.getCamera()->setClearColor( precipitationEffect->getFog()->getColor() );
168
169
170    // if user request help write it out to cout.
171    if (arguments.read("-h") || arguments.read("--help"))
172    {
173        arguments.getApplicationUsage()->write(std::cout);
174        return 1;
175    }
176   
177    // read the scene from the list of file specified commandline args.
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    }
184   
185    // precipitationEffect->setUpdateCallback(new MyGustCallback);
186   
187    osg::ref_ptr<osg::Group> group = new osg::Group;
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());
196
197        group->addChild(clipNode.get());
198    }
199    else
200    {
201        group->addChild(precipitationEffect.get());
202    }
203   
204    group->addChild(loadedModel.get());
205
206    loadedModel->getOrCreateStateSet()->setAttributeAndModes(precipitationEffect->getFog());
207   
208    // create the light   
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)); // directional light from above
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
220    // set the scene to render
221    viewer.setSceneData(group.get());
222
223    return viewer.run();
224}
Note: See TracBrowser for help on using the browser.