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

Revision 5102, 8.3 kB (checked in by robert, 8 years ago)

Moved PrecipitationEffect? node into osgParticle.

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