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

Revision 6941, 7.6 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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