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

Revision 5103, 8.8 kB (checked in by robert, 9 years ago)

Added support for wind.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified   
4 * freely and without restriction, both in commericial and non commericial applications,
5 * as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*/
11
12#include <osgDB/ReadFile>
13#include <osgDB/FileUtils>
14#include <osgUtil/Optimizer>
15#include <osgUtil/CullVisitor>
16#include <osgProducer/Viewer>
17
18#include <osg/MatrixTransform>
19#include <osgUtil/TransformCallback>
20#include <osgParticle/PrecipitationEffect>
21
22class MyGustCallback : public osg::NodeCallback
23{
24
25    public:
26
27        MyGustCallback() {}
28
29        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
30        {
31            osgParticle::PrecipitationEffect* pe = dynamic_cast<osgParticle::PrecipitationEffect*>(node);
32           
33            float value = sin(nv->getFrameStamp()->getReferenceTime());
34            if (value<-0.5) pe->getParameters()->wind.set(5.0,0.0,0.0);
35            else pe->getParameters()->wind.set(1.0,0.0,0.0);
36       
37            traverse(node, nv);
38        }
39};
40
41#if 0
42osg::Node* createModel(osg::Node* loadedModel, osgParticle::PrecipitationParameters& parameters)
43{
44    osg::Group* group = new osg::Group;
45
46    osg::BoundingBox bb(0.0, 0.0, 0.0, 100.0, 100.0, 100.0);
47   
48    if (loadedModel)
49    {
50        group->addChild(loadedModel);
51       
52        osg::BoundingSphere bs = loadedModel->getBound();
53
54        bb.set( -500, -500, 0, +500, +500, 10);
55       
56        parameters.boundingBox = bb;
57       
58        osg::StateSet* stateset = loadedModel->getOrCreateStateSet();
59       
60        osg::Fog* fog = new osg::Fog;
61       
62        if (parameters.fogExponent<1.0)
63        {
64            fog->setMode(osg::Fog::LINEAR);
65        }
66        else if (parameters.fogExponent<2.0)
67        {
68            fog->setMode(osg::Fog::EXP);
69        }
70        else
71        {
72            fog->setMode(osg::Fog::EXP2);
73        }
74       
75        fog->setDensity(parameters.fogDensity);
76        fog->setStart(0.0f);
77        fog->setEnd(parameters.fogEnd);
78        fog->setColor(parameters.fogColour);
79        stateset->setAttributeAndModes(fog, osg::StateAttribute::ON);
80       
81        osg::LightSource* lightSource = new osg::LightSource;
82        group->addChild(lightSource);
83
84        osg::Light* light = lightSource->getLight();
85        light->setLightNum(0);
86        light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); // directional light from above
87        light->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f));
88        light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
89        light->setSpecular(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
90
91               
92    }
93   
94    group->addChild(createCellRainEffect(parameters));
95
96    return group;   
97}
98#endif
99
100int main( int argc, char **argv )
101{
102
103    // use an ArgumentParser object to manage the program arguments.
104    osg::ArgumentParser arguments(&argc,argv);
105   
106    // set up the usage document, in case we need to print out how to use this program.
107    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
108    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds..");
109    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
110    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
111    arguments.getApplicationUsage()->addCommandLineOption("","");
112    arguments.getApplicationUsage()->addCommandLineOption("","");
113    arguments.getApplicationUsage()->addCommandLineOption("","");
114    arguments.getApplicationUsage()->addCommandLineOption("","");
115    arguments.getApplicationUsage()->addCommandLineOption("","");
116    arguments.getApplicationUsage()->addCommandLineOption("","");
117    arguments.getApplicationUsage()->addCommandLineOption("","");
118    arguments.getApplicationUsage()->addCommandLineOption("","");
119    arguments.getApplicationUsage()->addCommandLineOption("","");
120    arguments.getApplicationUsage()->addCommandLineOption("","");
121   
122
123    // construct the viewer.
124    osgProducer::Viewer viewer(arguments);
125
126    // set up the value with sensible default event handlers.
127    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
128
129    // get details on keyboard and mouse bindings used by the viewer.
130    viewer.getUsage(*arguments.getApplicationUsage());
131
132    osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect;
133    osgParticle::PrecipitationParameters& parameters = *precipitationEffect->getParameters();
134
135    float intensity;
136    while (arguments.read("--snow", intensity)) parameters.snow(intensity);
137    while (arguments.read("--rain", intensity)) parameters.rain(intensity);
138
139    while (arguments.read("--particleSize", parameters.particleSize)) {}
140    while (arguments.read("--particleColor", parameters.particleColour.r(), parameters.particleColour.g(), parameters.particleColour.b(), parameters.particleColour.a())) {}
141    while (arguments.read("--particleColour", parameters.particleColour.r(), parameters.particleColour.g(), parameters.particleColour.b(), parameters.particleColour.a())) {}
142
143    osg::Vec3 wind;
144    while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) parameters.wind = wind;
145   
146    while (arguments.read("--particleVelocity", parameters.particleVelocity)) {}
147
148    while (arguments.read("--nearTransition", parameters.nearTransition )) {}
149    while (arguments.read("--farTransition", parameters.farTransition )) {}
150
151    while (arguments.read("--particleDensity", parameters.particleDensity )) {}
152
153    while (arguments.read("--cellSizeX", parameters.cellSizeX )) {}
154    while (arguments.read("--cellSizeY", parameters.cellSizeY )) {}
155    while (arguments.read("--cellSizeZ", parameters.cellSizeZ )) {}
156
157    while (arguments.read("--boundingBox", parameters.boundingBox.xMin(),
158                                           parameters.boundingBox.yMin(),
159                                           parameters.boundingBox.zMin(),
160                                           parameters.boundingBox.xMax(),
161                                           parameters.boundingBox.yMax(),
162                                           parameters.boundingBox.zMax())) {}
163
164    while (arguments.read("--fogDensity", parameters.fogDensity )) {}
165    while (arguments.read("--fogExponent", parameters.fogExponent )) {}
166    while (arguments.read("--fogEnd", parameters.fogEnd )) {}
167    while (arguments.read("--fogColor", parameters.fogColour.r(), parameters.fogColour.g(), parameters.fogColour.b(), parameters.fogColour.a())) {}
168    while (arguments.read("--fogColour", parameters.fogColour.r(), parameters.fogColour.g(), parameters.fogColour.b(), parameters.fogColour.a())) {}
169 
170    while (arguments.read("--useFarLineSegments")) { parameters.useFarLineSegments = true; }
171
172   
173
174
175    viewer.setClearColor(parameters.clearColour);
176
177    // now force the effect to update all its internal state.
178    precipitationEffect->update();
179
180
181    // if user request help write it out to cout.
182    if (arguments.read("-h") || arguments.read("--help"))
183    {
184        arguments.getApplicationUsage()->write(std::cout);
185        return 1;
186    }
187
188    // any option left unread are converted into errors to write out later.
189    arguments.reportRemainingOptionsAsUnrecognized();
190
191    // report any errors if they have occured when parsing the program aguments.
192    if (arguments.errors())
193    {
194        arguments.writeErrorMessages(std::cout);
195        return 1;
196    }
197   
198    // read the scene from the list of file specified commandline args.
199    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
200    if (!loadedModel)
201    {
202        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
203        return 1;
204    }
205   
206    // precipitationEffect->setUpdateCallback(new MyGustCallback);
207
208    osg::ref_ptr<osg::Group> group = new osg::Group;
209    group->addChild(precipitationEffect.get());
210    group->addChild(loadedModel.get());
211   
212    // set the scene to render
213    viewer.setSceneData(group.get());
214
215    // create the windows and run the threads.
216    viewer.realize();
217
218    while( !viewer.done() )
219    {
220        // wait for all cull and draw threads to complete.
221        viewer.sync();
222
223        // update the scene by traversing it with the the update visitor which will
224        // call all node update callbacks and animations.
225        viewer.update();
226         
227        // fire off the cull and draw traversals of the scene.
228        viewer.frame();
229       
230    }
231   
232    // wait for all cull and draw threads to complete before exit.
233    viewer.sync();
234
235    return 0;
236}
Note: See TracBrowser for help on using the browser.