2003-03-15 04:35:45 +08:00
# include <osg/Group>
# include <osg/Notify>
# include <osg/Depth>
# include <osg/StateSet>
# include <osg/ClearNode>
# include <osg/Transform>
# include <osgUtil/CullVisitor>
# include <osgDB/Registry>
# include <osgDB/ReadFile>
# include <osgGA/AnimationPathManipulator>
# include <osgProducer/Viewer>
# include "GliderManipulator.h"
extern osg : : Node * makeTerrain ( void ) ;
extern osg : : Node * makeTrees ( void ) ;
extern osg : : Node * makeTank ( void ) ;
extern osg : : Node * makeWindsocks ( void ) ;
extern osg : : Node * makeGliders ( void ) ;
extern osg : : Node * makeGlider ( void ) ;
extern osg : : Node * makeSky ( void ) ;
extern osg : : Node * makeBase ( void ) ;
extern osg : : Node * makeClouds ( void ) ;
2003-12-09 22:07:44 +08:00
class MoveEarthySkyWithEyePointTransform : public osg : : Transform
2003-03-15 04:35:45 +08:00
{
2003-12-09 22:07:44 +08:00
public :
2003-03-15 04:35:45 +08:00
/** Get the transformation matrix which moves from local coords to world coords.*/
2003-12-09 22:07:44 +08:00
virtual bool computeLocalToWorldMatrix ( osg : : Matrix & matrix , osg : : NodeVisitor * nv ) const
2003-03-15 04:35:45 +08:00
{
osgUtil : : CullVisitor * cv = dynamic_cast < osgUtil : : CullVisitor * > ( nv ) ;
if ( cv )
{
osg : : Vec3 eyePointLocal = cv - > getEyeLocal ( ) ;
matrix . preMult ( osg : : Matrix : : translate ( eyePointLocal . x ( ) , eyePointLocal . y ( ) , 0.0f ) ) ;
}
return true ;
}
/** Get the transformation matrix which moves from world coords to local coords.*/
2003-12-09 22:07:44 +08:00
virtual bool computeWorldToLocalMatrix ( osg : : Matrix & matrix , osg : : NodeVisitor * nv ) const
2003-03-15 04:35:45 +08:00
{
2003-12-09 22:07:44 +08:00
std : : cout < < " computing transform " < < std : : endl ;
2003-03-15 04:35:45 +08:00
osgUtil : : CullVisitor * cv = dynamic_cast < osgUtil : : CullVisitor * > ( nv ) ;
if ( cv )
{
osg : : Vec3 eyePointLocal = cv - > getEyeLocal ( ) ;
matrix . postMult ( osg : : Matrix : : translate ( - eyePointLocal . x ( ) , - eyePointLocal . y ( ) , 0.0f ) ) ;
}
return true ;
}
} ;
osg : : Group * createModel ( )
{
// no database loaded so automatically create Ed Levin Park..
osg : : Group * group = new osg : : Group ;
// the base and sky subgraphs go to set the earth sky of the
// model and clear the color and depth buffer for us, by using
// osg::Depth, and setting their bin numbers to less than 0,
// to force them to draw before the rest of the scene.
osg : : ClearNode * clearNode = new osg : : ClearNode ;
clearNode - > setRequiresClear ( false ) ; // we've got base and sky to do it.
// use a transform to make the sky and base around with the eye point.
2003-12-09 22:07:44 +08:00
osg : : Transform * transform = new MoveEarthySkyWithEyePointTransform ;
2003-03-15 04:35:45 +08:00
// transform's value isn't knowm until in the cull traversal so its bounding
// volume is can't be determined, therefore culling will be invalid,
// so switch it off, this cause all our paresnts to switch culling
// off as well. But don't worry culling will be back on once underneath
// this node or any other branch above this transform.
transform - > setCullingActive ( false ) ;
// add the sky and base layer.
transform - > addChild ( makeSky ( ) ) ; // bin number -2 so drawn first.
transform - > addChild ( makeBase ( ) ) ; // bin number -1 so draw second.
// add the transform to the earth sky.
clearNode - > addChild ( transform ) ;
// add to earth sky to the scene.
group - > addChild ( clearNode ) ;
// the rest of the scene drawn after the base and sky above.
group - > addChild ( makeTrees ( ) ) ; // will drop into a transparent, depth sorted bin (1)
group - > addChild ( makeTerrain ( ) ) ; // will drop into default bin - state sorted 0
group - > addChild ( makeTank ( ) ) ; // will drop into default bin - state sorted 0
// add the following in the future...
// makeGliders
// makeClouds
return group ;
}
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.
2003-04-09 19:44:32 +08:00
arguments . getApplicationUsage ( ) - > setDescription ( arguments . getApplicationName ( ) + " is the example which demonstrates how to create a scene programatically, in this case a hang gliding flying site. " ) ;
2003-04-07 05:32:44 +08:00
arguments . getApplicationUsage ( ) - > setCommandLineUsage ( arguments . getApplicationName ( ) + " [options] filename ... " ) ;
2003-03-15 04:35:45 +08:00
arguments . getApplicationUsage ( ) - > addCommandLineOption ( " -h or --help " , " Display this information " ) ;
// construct the viewer.
osgProducer : : Viewer viewer ( arguments ) ;
// set up the value with sensible default event handlers.
2006-08-01 18:27:11 +08:00
viewer . setUpViewer ( osgProducer : : Viewer : : ESCAPE_SETS_DONE ) ;
2003-03-15 04:35:45 +08:00
unsigned int pos = viewer . addCameraManipulator ( new GliderManipulator ( ) ) ;
viewer . selectCameraManipulator ( pos ) ;
// get details on keyboard and mouse bindings used by the viewer.
viewer . getUsage ( * arguments . getApplicationUsage ( ) ) ;
// if user request help write it out to cout.
if ( arguments . read ( " -h " ) | | arguments . read ( " --help " ) )
{
arguments . getApplicationUsage ( ) - > write ( std : : cout ) ;
return 1 ;
}
// any option left unread are converted into errors to write out later.
arguments . reportRemainingOptionsAsUnrecognized ( ) ;
// report any errors if they have occured when parsing the program aguments.
if ( arguments . errors ( ) )
{
arguments . writeErrorMessages ( std : : cout ) ;
return 1 ;
}
2003-04-07 05:32:44 +08:00
2003-03-15 04:35:45 +08:00
// load the nodes from the commandline arguments.
osg : : Node * rootnode = osgDB : : readNodeFiles ( arguments ) ;
if ( ! rootnode ) rootnode = createModel ( ) ;
viewer . setSceneData ( rootnode ) ;
2006-08-01 18:27:11 +08:00
2003-03-15 04:35:45 +08:00
// create the windows and run the threads.
2003-04-08 23:18:45 +08:00
viewer . realize ( ) ;
2003-03-15 04:35:45 +08:00
while ( ! viewer . done ( ) )
{
// wait for all cull and draw threads to complete.
viewer . sync ( ) ;
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
viewer . update ( ) ;
// fire off the cull and draw traversals of the scene.
viewer . frame ( ) ;
}
2003-03-25 18:05:09 +08:00
// wait for all cull and draw threads to complete before exit.
viewer . sync ( ) ;
2003-03-15 04:35:45 +08:00
2005-05-08 04:47:09 +08:00
// run a clean up frame to delete all OpenGL objects.
viewer . cleanup_frame ( ) ;
// wait for all the clean up frame to complete.
viewer . sync ( ) ;
2003-03-15 04:35:45 +08:00
return 0 ;
}