2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2006-04-05 23:13:17 +08:00
*
* This application is open source and may be redistributed and / or modified
* freely and without restriction , both in commericial and non commericial applications ,
* as long as this copyright notice is maintained .
*
* This application is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
*/
# include <osgDB/ReadFile>
# include <osgDB/FileUtils>
# include <osgUtil/Optimizer>
2006-04-08 02:24:52 +08:00
# include <osgUtil/CullVisitor>
2007-01-10 21:52:22 +08:00
# include <osgViewer/Viewer>
2006-04-05 23:13:17 +08:00
2006-04-24 14:36:26 +08:00
# include <osg/MatrixTransform>
# include <osgUtil/TransformCallback>
2006-04-22 23:08:07 +08:00
# include <osgParticle/PrecipitationEffect>
2006-04-14 03:05:26 +08:00
2007-01-10 21:52:22 +08:00
# include <iostream>
2006-04-24 14:36:26 +08:00
class MyGustCallback : public osg : : NodeCallback
{
public :
MyGustCallback ( ) { }
virtual void operator ( ) ( osg : : Node * node , osg : : NodeVisitor * nv )
{
osgParticle : : PrecipitationEffect * pe = dynamic_cast < osgParticle : : PrecipitationEffect * > ( node ) ;
float value = sin ( nv - > getFrameStamp ( ) - > getReferenceTime ( ) ) ;
2006-04-25 20:39:00 +08:00
if ( value < - 0.5 )
{
pe - > snow ( 1.0 ) ;
}
else
{
pe - > rain ( 0.5 ) ;
}
2006-04-24 14:36:26 +08:00
traverse ( node , nv ) ;
}
} ;
2006-04-05 23:13:17 +08:00
int main ( int argc , char * * argv )
{
// use an ArgumentParser object to manage the program arguments.
osg : : ArgumentParser arguments ( & argc , argv ) ;
// set up the usage document, in case we need to print out how to use this program.
arguments . getApplicationUsage ( ) - > setApplicationName ( arguments . getApplicationName ( ) ) ;
arguments . getApplicationUsage ( ) - > setDescription ( arguments . getApplicationName ( ) + " example provides an interactive viewer for visualising point clouds.. " ) ;
arguments . getApplicationUsage ( ) - > setCommandLineUsage ( arguments . getApplicationName ( ) + " [options] filename ... " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " -h or --help " , " Display this information " ) ;
2007-01-10 21:52:22 +08:00
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --snow <density> " , " Set the snow with a density between 0 and 1.0 " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --rain <density> " , " " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --particleSize <size> " , " " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --particleColour <red> <green> <blue> <alpha> " , " " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --wind <x> <y> <z> " , " Set the wind speed in model coordinates " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --particleSpeed <float> " , " Set the particle speed " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --nearTransition <distance> " , " Set the near transistion distance " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --farTransition <distance> " , " Set the far transistion distance " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --particleDensity <density> " , " Set the particle density " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --cellSize <x> <y> <z> " , " Set the cell size in model coordinates " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --fogDensity <density> " , " Set the fog density " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " --fogColour <red> <green> <blue> <alpha> " , " Set fog colour. " ) ;
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " -useFarLineSegments " , " Switch on the use of line segments " ) ;
2006-04-05 23:13:17 +08:00
// construct the viewer.
2007-01-10 21:52:22 +08:00
osgViewer : : Viewer viewer ;
2006-04-05 23:13:17 +08:00
2006-04-22 03:39:05 +08:00
osg : : ref_ptr < osgParticle : : PrecipitationEffect > precipitationEffect = new osgParticle : : PrecipitationEffect ;
2006-04-14 03:05:26 +08:00
float intensity ;
2006-04-25 00:21:10 +08:00
while ( arguments . read ( " --snow " , intensity ) ) precipitationEffect - > snow ( intensity ) ;
while ( arguments . read ( " --rain " , intensity ) ) precipitationEffect - > rain ( intensity ) ;
2006-04-14 03:05:26 +08:00
2006-04-25 00:21:10 +08:00
float value ;
while ( arguments . read ( " --particleSize " , value ) ) precipitationEffect - > setParticleSize ( value ) ;
osg : : Vec4 color ;
while ( arguments . read ( " --particleColor " , color . r ( ) , color . g ( ) , color . b ( ) , color . a ( ) ) ) precipitationEffect - > setParticleColor ( color ) ;
while ( arguments . read ( " --particleColour " , color . r ( ) , color . g ( ) , color . b ( ) , color . a ( ) ) ) precipitationEffect - > setParticleColor ( color ) ;
2006-04-14 03:05:26 +08:00
2006-04-24 14:36:26 +08:00
osg : : Vec3 wind ;
2006-04-25 00:21:10 +08:00
while ( arguments . read ( " --wind " , wind . x ( ) , wind . y ( ) , wind . z ( ) ) ) precipitationEffect - > setWind ( wind ) ;
2006-04-24 14:36:26 +08:00
2006-04-25 00:21:10 +08:00
while ( arguments . read ( " --particleSpeed " , value ) ) precipitationEffect - > setParticleSpeed ( value ) ;
2006-04-14 03:05:26 +08:00
2006-04-25 00:21:10 +08:00
while ( arguments . read ( " --nearTransition " , value ) ) precipitationEffect - > setNearTransition ( value ) ;
while ( arguments . read ( " --farTransition " , value ) ) precipitationEffect - > setFarTransition ( value ) ;
2006-04-14 04:21:55 +08:00
2006-04-25 00:21:10 +08:00
while ( arguments . read ( " --particleDensity " , value ) ) precipitationEffect - > setMaximumParticleDensity ( value ) ;
2006-04-14 04:21:55 +08:00
2006-04-25 00:21:10 +08:00
osg : : Vec3 cellSize ;
while ( arguments . read ( " --cellSize " , cellSize . x ( ) , cellSize . y ( ) , cellSize . z ( ) ) ) precipitationEffect - > setCellSize ( cellSize ) ;
2006-04-14 04:21:55 +08:00
2006-04-25 00:21:10 +08:00
osg : : BoundingBox bb ;
while ( arguments . read ( " --boundingBox " , bb . xMin ( ) ,
bb . yMin ( ) ,
bb . zMin ( ) ,
bb . xMax ( ) ,
bb . yMax ( ) ,
bb . zMax ( ) ) ) { }
2006-04-05 23:13:17 +08:00
2006-04-25 00:21:10 +08:00
while ( arguments . read ( " --fogDensity " , value ) ) precipitationEffect - > getFog ( ) - > setDensity ( value ) ;
while ( arguments . read ( " --fogColor " , color . r ( ) , color . g ( ) , color . b ( ) , color . a ( ) ) ) precipitationEffect - > getFog ( ) - > setColor ( color ) ;
while ( arguments . read ( " --fogColour " , color . r ( ) , color . g ( ) , color . b ( ) , color . a ( ) ) ) precipitationEffect - > getFog ( ) - > setColor ( color ) ;
2006-04-14 04:21:55 +08:00
2006-04-25 20:56:33 +08:00
while ( arguments . read ( " --useFarLineSegments " ) ) { precipitationEffect - > setUseFarLineSegments ( true ) ; }
2006-04-14 22:24:12 +08:00
2006-04-22 03:39:05 +08:00
2007-01-10 21:52:22 +08:00
viewer . getCamera ( ) - > setClearColor ( precipitationEffect - > getFog ( ) - > getColor ( ) ) ;
2006-04-14 22:24:12 +08:00
2006-04-22 03:39:05 +08:00
2006-04-05 23:13:17 +08:00
// if user request help write it out to cout.
if ( arguments . read ( " -h " ) | | arguments . read ( " --help " ) )
{
arguments . getApplicationUsage ( ) - > write ( std : : cout ) ;
return 1 ;
}
// read the scene from the list of file specified commandline args.
osg : : ref_ptr < osg : : Node > loadedModel = osgDB : : readNodeFiles ( arguments ) ;
if ( ! loadedModel )
{
std : : cout < < arguments . getApplicationName ( ) < < " : No data loaded " < < std : : endl ;
return 1 ;
}
2006-04-22 03:39:05 +08:00
2006-04-24 14:36:26 +08:00
// precipitationEffect->setUpdateCallback(new MyGustCallback);
2006-04-25 00:21:10 +08:00
2006-04-24 14:36:26 +08:00
2006-04-22 03:39:05 +08:00
osg : : ref_ptr < osg : : Group > group = new osg : : Group ;
group - > addChild ( precipitationEffect . get ( ) ) ;
2006-04-25 00:21:10 +08:00
2006-04-22 03:39:05 +08:00
group - > addChild ( loadedModel . get ( ) ) ;
2006-04-25 00:21:10 +08:00
loadedModel - > getOrCreateStateSet ( ) - > setAttributeAndModes ( precipitationEffect - > getFog ( ) ) ;
2006-04-22 03:39:05 +08:00
2006-04-25 00:21:10 +08:00
// create the light
osg : : LightSource * lightSource = new osg : : LightSource ;
group - > addChild ( lightSource ) ;
osg : : Light * light = lightSource - > getLight ( ) ;
light - > setLightNum ( 0 ) ;
light - > setPosition ( osg : : Vec4 ( 0.0f , 0.0f , 1.0f , 0.0f ) ) ; // directional light from above
light - > setAmbient ( osg : : Vec4 ( 0.8f , 0.8f , 0.8f , 1.0f ) ) ;
light - > setDiffuse ( osg : : Vec4 ( 0.2f , 0.2f , 0.2f , 1.0f ) ) ;
light - > setSpecular ( osg : : Vec4 ( 0.2f , 0.2f , 0.2f , 1.0f ) ) ;
2006-04-22 03:39:05 +08:00
// set the scene to render
viewer . setSceneData ( group . get ( ) ) ;
2006-04-05 23:13:17 +08:00
2007-01-10 21:52:22 +08:00
return viewer . run ( ) ;
2006-04-05 23:13:17 +08:00
}