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

Revision 5108, 9.1 kB (checked in by robert, 9 years ago)

Moved PrecipitationParameters? directly into PrecipitationEffect?.

  • 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->setWind(osg::Vec3(5.0f,0.0f,0.0f));
35            else pe->setWind(osg::Vec3(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
134    float intensity;
135    while (arguments.read("--snow", intensity)) precipitationEffect->snow(intensity);
136    while (arguments.read("--rain", intensity)) precipitationEffect->rain(intensity);
137
138    float value;
139    while (arguments.read("--particleSize", value)) precipitationEffect->setParticleSize(value);
140
141    osg::Vec4 color;
142    while (arguments.read("--particleColor", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color);
143    while (arguments.read("--particleColour", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color);
144
145    osg::Vec3 wind;
146    while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) precipitationEffect->setWind(wind);
147   
148    while (arguments.read("--particleSpeed", value)) precipitationEffect->setParticleSpeed(value);
149
150    while (arguments.read("--nearTransition", value )) precipitationEffect->setNearTransition(value);
151    while (arguments.read("--farTransition", value )) precipitationEffect->setFarTransition(value);
152
153    while (arguments.read("--particleDensity", value )) precipitationEffect->setMaximumParticleDensity(value);
154
155    osg::Vec3 cellSize;
156    while (arguments.read("--cellSize", cellSize.x(), cellSize.y(), cellSize.z())) precipitationEffect->setCellSize(cellSize);
157
158    osg::BoundingBox bb;
159    while (arguments.read("--boundingBox", bb.xMin(),
160                                           bb.yMin(),
161                                           bb.zMin(),
162                                           bb.xMax(),
163                                           bb.yMax(),
164                                           bb.zMax())) {}
165
166    while (arguments.read("--fogDensity", value )) precipitationEffect->getFog()->setDensity(value);
167    while (arguments.read("--fogColor", color.r(), color.g(), color.b(), color.a() ))  precipitationEffect->getFog()->setColor(color);
168    while (arguments.read("--fogColour", color.r(), color.g(), color.b(), color.a() ))  precipitationEffect->getFog()->setColor(color);
169 
170//  while (arguments.read("--useFarLineSegments")) { parameters.useFarLineSegments = true; }
171
172   
173    viewer.setClearColor( precipitationEffect->getFog()->getColor() );
174
175    // now force the effect to update all its internal state.
176    precipitationEffect->update();
177
178
179    // if user request help write it out to cout.
180    if (arguments.read("-h") || arguments.read("--help"))
181    {
182        arguments.getApplicationUsage()->write(std::cout);
183        return 1;
184    }
185
186    // any option left unread are converted into errors to write out later.
187    arguments.reportRemainingOptionsAsUnrecognized();
188
189    // report any errors if they have occured when parsing the program aguments.
190    if (arguments.errors())
191    {
192        arguments.writeErrorMessages(std::cout);
193        return 1;
194    }
195   
196    // read the scene from the list of file specified commandline args.
197    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
198    if (!loadedModel)
199    {
200        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
201        return 1;
202    }
203   
204    // precipitationEffect->setUpdateCallback(new MyGustCallback);
205   
206
207    osg::ref_ptr<osg::Group> group = new osg::Group;
208    group->addChild(precipitationEffect.get());
209
210    group->addChild(loadedModel.get());
211    loadedModel->getOrCreateStateSet()->setAttributeAndModes(precipitationEffect->getFog());
212   
213    // create the light   
214    osg::LightSource* lightSource = new osg::LightSource;
215    group->addChild(lightSource);
216
217    osg::Light* light = lightSource->getLight();
218    light->setLightNum(0);
219    light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); // directional light from above
220    light->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f));
221    light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
222    light->setSpecular(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
223
224
225
226
227    // set the scene to render
228    viewer.setSceneData(group.get());
229
230    // create the windows and run the threads.
231    viewer.realize();
232
233    while( !viewer.done() )
234    {
235        // wait for all cull and draw threads to complete.
236        viewer.sync();
237
238        // update the scene by traversing it with the the update visitor which will
239        // call all node update callbacks and animations.
240        viewer.update();
241         
242        // fire off the cull and draw traversals of the scene.
243        viewer.frame();
244       
245    }
246   
247    // wait for all cull and draw threads to complete before exit.
248    viewer.sync();
249
250    return 0;
251}
Note: See TracBrowser for help on using the browser.