root/OpenSceneGraph/trunk/examples/osggeodemo/osggeodemo.cpp @ 1844

Revision 1844, 6.2 kB (checked in by robert, 12 years ago)

Added desciptions to all the demos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// Geo demo written by Geoff Michel, November 2002.
2
3#include <stdio.h>
4#include <osgProducer/Viewer>
5
6#include <osg/Node>
7#include <osg/Notify>
8
9#include <osgDB/Registry>
10#include <osgDB/ReadFile>
11
12#include <osgGA/TrackballManipulator>
13#include <osgGA/FlightManipulator>
14#include <osgGA/DriveManipulator>
15#include <osgGA/AnimationPathManipulator>
16
17#include <osgDB/WriteFile>
18#include <osgDB/FileUtils>
19#include <osgDB/FileNameUtils>
20
21#include <osgUtil/Optimizer>
22
23// currently not a satisfactory solution, but this is early days for the
24// geo loader and having direct links with it.
25#include "../../src/osgPlugins/geo/osgGeoAnimation.h"
26
27
28//== event trapper gets events
29
30class geodemoEventHandler : public osgGA::GUIEventHandler
31{
32public:
33   
34    geodemoEventHandler( )     { mouse_x=mouse_y=0;}
35    virtual ~geodemoEventHandler( ) {}
36
37    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
38    {
39        mouse_x=ea.getX();
40        mouse_y=ea.getY();
41        return false;
42       
43    }
44   
45    virtual void accept(osgGA::GUIEventHandlerVisitor& v)
46    {
47        v.visit(*this);
48    }
49    inline float getMouseX(void) {return mouse_x;};
50    inline float getMouseY(void) {return mouse_y;};
51   
52private:
53    float    mouse_x, mouse_y;
54};
55
56static geodemoEventHandler *ghand=NULL;
57inline double DEG2RAD(const double val) { return val*0.0174532925199432957692369076848861;}
58inline double RAD2DEG(const double val) { return val*57.2957795130823208767981548141052;}
59
60double dodynamics(const double /*time*/, const double val, const std::string name)
61{ // Each local variable named 'name' declared in the geo modeller is passed into here.
62    // its current value is val; returns new value.  Time - elapsed time
63    static double heading,speed; // these are only required for my 'dynamics'
64    if (name == "xpos") {
65        return (val+speed*sin(heading));
66        //    std::cout << " nx " << (*itr->getValue()) ;
67    } else if (name == "ypos") {
68        return (val+speed*cos(heading));
69        //    std::cout << " ny " << (*itr->getValue()) ;
70    } else if (name == "sped") {
71        speed=(0.00025*(ghand->getMouseY()-300)); // (*itr->getValue());
72        return (speed);
73    } else if (name == "heading") {
74        heading-= 0.01*DEG2RAD(ghand->getMouseX()-400); // =DEG2RAD(*itr->getValue());
75        return (RAD2DEG(heading));
76    } else if (name == "conerot") {
77        return ((ghand->getMouseX()-400));
78    } else if (name == "planrot") {
79        return ((ghand->getMouseY()-300)/200.0);
80    } else if (name == "secint" || name == "minutehand"|| name == "hourhand") {
81    //    std::cout << " updating " << name << " " << val << std::endl;
82    }
83    return val;
84}
85
86int main( int argc, char **argv )
87{
88    // use an ArgumentParser object to manage the program arguments.
89    osg::ArgumentParser arguments(&argc,argv);
90
91    // set up the usage document, in case we need to print out how to use this program.
92    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example of how to control animation in Geo files.");
93    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
94    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
95    arguments.getApplicationUsage()->addCommandLineOption("--fov <float>","Set the Field of View");
96   
97    float camera_fov=-1;
98    while (arguments.read("--fov",camera_fov)) {}
99
100    // construct the viewer.
101    osgProducer::Viewer viewer(arguments);
102
103    // set up the value with sensible default event handlers.
104    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
105
106    // get details on keyboard and mouse bindings used by the viewer.
107    viewer.getUsage(*arguments.getApplicationUsage());
108
109    // if user request help write it out to cout.
110    if (arguments.read("-h") || arguments.read("--help"))
111    {
112        arguments.getApplicationUsage()->write(std::cout);
113        return 1;
114    }
115
116    // any option left unread are converted into errors to write out later.
117    arguments.reportRemainingOptionsAsUnrecognized();
118
119    // report any errors if they have occured when parsing the program aguments.
120    if (arguments.errors())
121    {
122        arguments.writeErrorMessages(std::cout);
123        return 1;
124    }
125   
126    if (arguments.argc()<=1)
127    {
128        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
129        return 1;
130    }
131
132
133    // load the nodes from the commandline arguments.
134    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
135    if (!rootnode)
136    {
137        return 1;
138    }
139   
140    // run optimization over the scene graph
141    osgUtil::Optimizer optimzer;
142    optimzer.optimize(rootnode);
143     
144    // add a viewport to the viewer and attach the scene graph.
145    viewer.setSceneData( rootnode );
146
147    geoHeader *gh = dynamic_cast<geoHeader *>(rootnode);
148    if (gh)
149    { // it is a geo file, so set function to update its animation variables.
150        ghand=new geodemoEventHandler();
151        gh->setUserUpdate(dodynamics);
152        viewer.getEventHandlerList().push_front(ghand);
153    }
154    else
155    { // maybe a group with geo models below.
156        osg::Group *gpall=dynamic_cast<osg::Group *>(rootnode);
157        if (gpall)
158        {
159            int nchild=gpall->getNumChildren();
160            for (int i=0; i<nchild; i++)
161            {
162                osg::Node *nod=gpall->getChild(i);
163                gh = dynamic_cast<geoHeader *>(nod);
164                if (gh)
165                {
166                    ghand=new geodemoEventHandler();
167                    gh->setUserUpdate(dodynamics);
168                    viewer.getEventHandlerList().push_front(ghand);
169                }
170            }
171        }
172    }
173
174    // create the windows and run the threads.
175    viewer.realize();
176
177    while( !viewer.done() )
178    {
179        // wait for all cull and draw threads to complete.
180        viewer.sync();
181
182        // update the scene by traversing it with the the update visitor which will
183        // call all node update callbacks and animations.
184        viewer.update();
185         
186        // fire off the cull and draw traversals of the scene.
187        viewer.frame();
188       
189    }
190   
191    // wait for all cull and draw threads to complete before exit.
192    viewer.sync();
193
194    return 0;
195}
Note: See TracBrowser for help on using the browser.