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

Revision 5099, 8.5 kB (checked in by robert, 8 years ago)

Added new precipitation nodes.

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