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

Revision 5954, 7.1 kB (checked in by robert, 7 years ago)

Ported following examples to osgViewer:

osgparticleeffects
osgphotoalbum
osgpick
osgpoints
osgpointsprite
osgprecipitation
osgprerender
osgprerendercubemap
osgreflect
osgscalarbar
osgscribe
osgsequence
osgplanets

  • 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 <osgViewer/Viewer>
17
18#include <osg/MatrixTransform>
19#include <osgUtil/TransformCallback>
20#include <osgParticle/PrecipitationEffect>
21
22#include <iostream>
23
24class MyGustCallback : public osg::NodeCallback
25{
26
27    public:
28
29        MyGustCallback() {}
30
31        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
32        {
33            osgParticle::PrecipitationEffect* pe = dynamic_cast<osgParticle::PrecipitationEffect*>(node);
34           
35            float value = sin(nv->getFrameStamp()->getReferenceTime());
36            if (value<-0.5)
37            {
38                pe->snow(1.0);
39            }
40            else
41            {
42                pe->rain(0.5);
43            }
44       
45            traverse(node, nv);
46        }
47};
48
49
50int main( int argc, char **argv )
51{
52
53    // use an ArgumentParser object to manage the program arguments.
54    osg::ArgumentParser arguments(&argc,argv);
55   
56    // set up the usage document, in case we need to print out how to use this program.
57    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
58    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" example provides an interactive viewer for visualising point clouds..");
59    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
60    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
61    arguments.getApplicationUsage()->addCommandLineOption("--snow <density>","Set the snow with a density between 0 and 1.0");
62    arguments.getApplicationUsage()->addCommandLineOption("--rain <density>","");
63    arguments.getApplicationUsage()->addCommandLineOption("--particleSize <size>","");
64    arguments.getApplicationUsage()->addCommandLineOption("--particleColour <red> <green> <blue> <alpha>","");
65    arguments.getApplicationUsage()->addCommandLineOption("--wind <x> <y> <z>","Set the wind speed in model coordinates");
66    arguments.getApplicationUsage()->addCommandLineOption("--particleSpeed <float>","Set the particle speed");
67    arguments.getApplicationUsage()->addCommandLineOption("--nearTransition <distance>","Set the near transistion distance");
68    arguments.getApplicationUsage()->addCommandLineOption("--farTransition  <distance>","Set the far transistion distance");
69    arguments.getApplicationUsage()->addCommandLineOption("--particleDensity <density>","Set the particle density");
70    arguments.getApplicationUsage()->addCommandLineOption("--cellSize <x> <y> <z>","Set the cell size in model coordinates");
71    arguments.getApplicationUsage()->addCommandLineOption("--fogDensity <density>","Set the fog density");
72    arguments.getApplicationUsage()->addCommandLineOption("--fogColour <red> <green> <blue> <alpha>","Set fog colour.");
73    arguments.getApplicationUsage()->addCommandLineOption("-useFarLineSegments","Switch on the use of line segments");
74   
75
76    // construct the viewer.
77    osgViewer::Viewer viewer;
78
79    osg::ref_ptr<osgParticle::PrecipitationEffect> precipitationEffect = new osgParticle::PrecipitationEffect;
80
81    float intensity;
82    while (arguments.read("--snow", intensity)) precipitationEffect->snow(intensity);
83    while (arguments.read("--rain", intensity)) precipitationEffect->rain(intensity);
84
85    float value;
86    while (arguments.read("--particleSize", value)) precipitationEffect->setParticleSize(value);
87
88    osg::Vec4 color;
89    while (arguments.read("--particleColor", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color);
90    while (arguments.read("--particleColour", color.r(), color.g(), color.b(), color.a())) precipitationEffect->setParticleColor(color);
91
92    osg::Vec3 wind;
93    while (arguments.read("--wind", wind.x(), wind.y(), wind.z())) precipitationEffect->setWind(wind);
94   
95    while (arguments.read("--particleSpeed", value)) precipitationEffect->setParticleSpeed(value);
96
97    while (arguments.read("--nearTransition", value )) precipitationEffect->setNearTransition(value);
98    while (arguments.read("--farTransition", value )) precipitationEffect->setFarTransition(value);
99
100    while (arguments.read("--particleDensity", value )) precipitationEffect->setMaximumParticleDensity(value);
101
102    osg::Vec3 cellSize;
103    while (arguments.read("--cellSize", cellSize.x(), cellSize.y(), cellSize.z())) precipitationEffect->setCellSize(cellSize);
104
105    osg::BoundingBox bb;
106    while (arguments.read("--boundingBox", bb.xMin(),
107                                           bb.yMin(),
108                                           bb.zMin(),
109                                           bb.xMax(),
110                                           bb.yMax(),
111                                           bb.zMax())) {}
112
113    while (arguments.read("--fogDensity", value )) precipitationEffect->getFog()->setDensity(value);
114    while (arguments.read("--fogColor", color.r(), color.g(), color.b(), color.a() ))  precipitationEffect->getFog()->setColor(color);
115    while (arguments.read("--fogColour", color.r(), color.g(), color.b(), color.a() ))  precipitationEffect->getFog()->setColor(color);
116 
117    while (arguments.read("--useFarLineSegments")) { precipitationEffect->setUseFarLineSegments(true); }
118
119   
120    viewer.getCamera()->setClearColor( precipitationEffect->getFog()->getColor() );
121
122
123    // if user request help write it out to cout.
124    if (arguments.read("-h") || arguments.read("--help"))
125    {
126        arguments.getApplicationUsage()->write(std::cout);
127        return 1;
128    }
129   
130    // read the scene from the list of file specified commandline args.
131    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
132    if (!loadedModel)
133    {
134        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
135        return 1;
136    }
137   
138    // precipitationEffect->setUpdateCallback(new MyGustCallback);
139   
140
141    osg::ref_ptr<osg::Group> group = new osg::Group;
142    group->addChild(precipitationEffect.get());
143
144    group->addChild(loadedModel.get());
145    loadedModel->getOrCreateStateSet()->setAttributeAndModes(precipitationEffect->getFog());
146   
147    // create the light   
148    osg::LightSource* lightSource = new osg::LightSource;
149    group->addChild(lightSource);
150
151    osg::Light* light = lightSource->getLight();
152    light->setLightNum(0);
153    light->setPosition(osg::Vec4(0.0f,0.0f,1.0f,0.0f)); // directional light from above
154    light->setAmbient(osg::Vec4(0.8f,0.8f,0.8f,1.0f));
155    light->setDiffuse(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
156    light->setSpecular(osg::Vec4(0.2f,0.2f,0.2f,1.0f));
157
158
159    // set the scene to render
160    viewer.setSceneData(group.get());
161
162    return viewer.run();
163}
Note: See TracBrowser for help on using the browser.