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

Revision 5381, 7.6 kB (checked in by robert, 8 years ago)

Added viewer.cleanup_frame() to all examples.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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)
35            {
36                pe->snow(1.0);
37            }
38            else
39            {
40                pe->rain(0.5);
41            }
42       
43            traverse(node, nv);
44        }
45};
46
47
48int main( int argc, char **argv )
49{
50
51    // use an ArgumentParser object to manage the program arguments.
52    osg::ArgumentParser arguments(&argc,argv);
53   
54    // set up the usage document, in case we need to print out how to use this program.
55    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
56    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds..");
57    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
58    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
59    arguments.getApplicationUsage()->addCommandLineOption("","");
60    arguments.getApplicationUsage()->addCommandLineOption("","");
61    arguments.getApplicationUsage()->addCommandLineOption("","");
62    arguments.getApplicationUsage()->addCommandLineOption("","");
63    arguments.getApplicationUsage()->addCommandLineOption("","");
64    arguments.getApplicationUsage()->addCommandLineOption("","");
65    arguments.getApplicationUsage()->addCommandLineOption("","");
66    arguments.getApplicationUsage()->addCommandLineOption("","");
67    arguments.getApplicationUsage()->addCommandLineOption("","");
68    arguments.getApplicationUsage()->addCommandLineOption("","");
69   
70
71    // construct the viewer.
72    osgProducer::Viewer viewer(arguments);
73
74    // set up the value with sensible default event handlers.
75    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
76
77    // get details on keyboard and mouse bindings used by the viewer.
78    viewer.getUsage(*arguments.getApplicationUsage());
79
80    osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect;
81
82    float intensity;
83    while (arguments.read("--snow", intensity)) precipitationEffect->snow(intensity);
84    while (arguments.read("--rain", intensity)) precipitationEffect->rain(intensity);
85
86    float value;
87    while (arguments.read("--particleSize", value)) precipitationEffect->setParticleSize(value);
88
89    osg::Vec4 color;
90    while (arguments.read("--particleColor", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color);
91    while (arguments.read("--particleColour", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color);
92
93    osg::Vec3 wind;
94    while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) precipitationEffect->setWind(wind);
95   
96    while (arguments.read("--particleSpeed", value)) precipitationEffect->setParticleSpeed(value);
97
98    while (arguments.read("--nearTransition", value )) precipitationEffect->setNearTransition(value);
99    while (arguments.read("--farTransition", value )) precipitationEffect->setFarTransition(value);
100
101    while (arguments.read("--particleDensity", value )) precipitationEffect->setMaximumParticleDensity(value);
102
103    osg::Vec3 cellSize;
104    while (arguments.read("--cellSize", cellSize.x(), cellSize.y(), cellSize.z())) precipitationEffect->setCellSize(cellSize);
105
106    osg::BoundingBox bb;
107    while (arguments.read("--boundingBox", bb.xMin(),
108                                           bb.yMin(),
109                                           bb.zMin(),
110                                           bb.xMax(),
111                                           bb.yMax(),
112                                           bb.zMax())) {}
113
114    while (arguments.read("--fogDensity", value )) precipitationEffect->getFog()->setDensity(value);
115    while (arguments.read("--fogColor", color.r(), color.g(), color.b(), color.a() ))  precipitationEffect->getFog()->setColor(color);
116    while (arguments.read("--fogColour", color.r(), color.g(), color.b(), color.a() ))  precipitationEffect->getFog()->setColor(color);
117 
118    while (arguments.read("--useFarLineSegments")) { precipitationEffect->setUseFarLineSegments(true); }
119
120   
121    viewer.setClearColor( precipitationEffect->getFog()->getColor() );
122
123
124    // if user request help write it out to cout.
125    if (arguments.read("-h") || arguments.read("--help"))
126    {
127        arguments.getApplicationUsage()->write(std::cout);
128        return 1;
129    }
130
131    // any option left unread are converted into errors to write out later.
132    arguments.reportRemainingOptionsAsUnrecognized();
133
134    // report any errors if they have occured when parsing the program aguments.
135    if (arguments.errors())
136    {
137        arguments.writeErrorMessages(std::cout);
138        return 1;
139    }
140   
141    // read the scene from the list of file specified commandline args.
142    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
143    if (!loadedModel)
144    {
145        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
146        return 1;
147    }
148   
149    // precipitationEffect->setUpdateCallback(new MyGustCallback);
150   
151
152    osg::ref_ptr<osg::Group> group = new osg::Group;
153    group->addChild(precipitationEffect.get());
154
155    group->addChild(loadedModel.get());
156    loadedModel->getOrCreateStateSet()->setAttributeAndModes(precipitationEffect->getFog());
157   
158    // create the light   
159    osg::LightSource* lightSource = new osg::LightSource;
160    group->addChild(lightSource);
161
162    osg::Light* light = lightSource->getLight();
163    light->setLightNum(0);
164    light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); // directional light from above
165    light->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f));
166    light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
167    light->setSpecular(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
168
169
170    // set the scene to render
171    viewer.setSceneData(group.get());
172
173    // create the windows and run the threads.
174    viewer.realize();
175
176    while( !viewer.done() )
177    {
178        // wait for all cull and draw threads to complete.
179        viewer.sync();
180
181        // update the scene by traversing it with the the update visitor which will
182        // call all node update callbacks and animations.
183        viewer.update();
184         
185        // fire off the cull and draw traversals of the scene.
186        viewer.frame();
187       
188    }
189   
190    // wait for all cull and draw threads to complete.
191    viewer.sync();
192
193    // run a clean up frame to delete all OpenGL objects.
194    viewer.cleanup_frame();
195
196    // wait for all the clean up frame to complete.
197    viewer.sync();
198
199    return 0;
200}
Note: See TracBrowser for help on using the browser.