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

Revision 1815, 6.1 kB (checked in by robert, 11 years ago)

Added the usage report to the examples for when no arguments are passed
to the examples.

  • 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 int getMouseX(void) {return mouse_x;};
50    inline int getMouseY(void) {return mouse_y;};
51   
52private:
53    int    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()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
93    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
94    arguments.getApplicationUsage()->addCommandLineOption("--fov <float>","Set the Field of View");
95   
96    float camera_fov=-1;
97    while (arguments.read("--fov",camera_fov)) {}
98
99    // construct the viewer.
100    osgProducer::Viewer viewer(arguments);
101
102    // set up the value with sensible default event handlers.
103    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
104
105    // get details on keyboard and mouse bindings used by the viewer.
106    viewer.getUsage(*arguments.getApplicationUsage());
107
108    // if user request help write it out to cout.
109    if (arguments.read("-h") || arguments.read("--help"))
110    {
111        arguments.getApplicationUsage()->write(std::cout);
112        return 1;
113    }
114
115    // any option left unread are converted into errors to write out later.
116    arguments.reportRemainingOptionsAsUnrecognized();
117
118    // report any errors if they have occured when parsing the program aguments.
119    if (arguments.errors())
120    {
121        arguments.writeErrorMessages(std::cout);
122        return 1;
123    }
124   
125    if (arguments.argc()<=1)
126    {
127        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
128        return 1;
129    }
130
131
132    // load the nodes from the commandline arguments.
133    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
134    if (!rootnode)
135    {
136        return 1;
137    }
138   
139    // run optimization over the scene graph
140    osgUtil::Optimizer optimzer;
141    optimzer.optimize(rootnode);
142     
143    // add a viewport to the viewer and attach the scene graph.
144    viewer.setSceneData( rootnode );
145
146    geoHeader *gh = dynamic_cast<geoHeader *>(rootnode);
147    if (gh)
148    { // it is a geo file, so set function to update its animation variables.
149        ghand=new geodemoEventHandler();
150        gh->setUserUpdate(dodynamics);
151        viewer.getEventHandlerList().push_front(ghand);
152    }
153    else
154    { // maybe a group with geo models below.
155        osg::Group *gpall=dynamic_cast<osg::Group *>(rootnode);
156        if (gpall)
157        {
158            int nchild=gpall->getNumChildren();
159            for (int i=0; i<nchild; i++)
160            {
161                osg::Node *nod=gpall->getChild(i);
162                gh = dynamic_cast<geoHeader *>(nod);
163                if (gh)
164                {
165                    ghand=new geodemoEventHandler();
166                    gh->setUserUpdate(dodynamics);
167                    viewer.getEventHandlerList().push_front(ghand);
168                }
169            }
170        }
171    }
172
173    // create the windows and run the threads.
174    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
175
176    while( !viewer.done() )
177    {
178        // wait for all cull and draw threads to complete.
179        viewer.sync();
180
181        // update the scene by traversing it with the the update visitor which will
182        // call all node update callbacks and animations.
183        viewer.update();
184         
185        // fire off the cull and draw traversals of the scene.
186        viewer.frame();
187       
188    }
189   
190    // wait for all cull and draw threads to complete before exit.
191    viewer.sync();
192
193    return 0;
194}
Note: See TracBrowser for help on using the browser.