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

Revision 5636, 6.3 kB (checked in by robert, 8 years ago)

Removed deprecated GUIEventHandler method

  • 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    inline float getMouseX(void) {return mouse_x;};
46    inline float getMouseY(void) {return mouse_y;};
47   
48private:
49    float    mouse_x, mouse_y;
50};
51
52static geodemoEventHandler *ghand=NULL;
53inline double DEG2RAD(const double val) { return val*0.0174532925199432957692369076848861;}
54inline double RAD2DEG(const double val) { return val*57.2957795130823208767981548141052;}
55
56double dodynamics(const double /*time*/, const double val, const std::string name)
57{ // Each local variable named 'name' declared in the geo modeller is passed into here.
58    // its current value is val; returns new value.  Time - elapsed time
59    static double heading,speed; // these are only required for my 'dynamics'
60    if (name == "xpos") {
61        return (val+speed*sin(heading));
62        //    std::cout << " nx " << (*itr->getValue()) ;
63    } else if (name == "ypos") {
64        return (val+speed*cos(heading));
65        //    std::cout << " ny " << (*itr->getValue()) ;
66    } else if (name == "sped") {
67        speed=(0.00025*(ghand->getMouseY()-300)); // (*itr->getValue());
68        return (speed);
69    } else if (name == "heading") {
70        heading-= 0.01*DEG2RAD(ghand->getMouseX()-400); // =DEG2RAD(*itr->getValue());
71        return (RAD2DEG(heading));
72    } else if (name == "conerot") {
73        return ((ghand->getMouseX()-400));
74    } else if (name == "planrot") {
75        return ((ghand->getMouseY()-300)/200.0);
76    } else if (name == "secint" || name == "minutehand"|| name == "hourhand") {
77    //    std::cout << " updating " << name << " " << val << std::endl;
78    }
79    return val;
80}
81
82int main( int argc, char **argv )
83{
84    // use an ArgumentParser object to manage the program arguments.
85    osg::ArgumentParser arguments(&argc,argv);
86
87    // set up the usage document, in case we need to print out how to use this program.
88    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example of how to control animation in Geo files.");
89    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
90    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
91    arguments.getApplicationUsage()->addCommandLineOption("--fov <float>","Set the Field of View");
92   
93    float camera_fov=-1;
94    while (arguments.read("--fov",camera_fov)) {}
95
96    // construct the viewer.
97    osgProducer::Viewer viewer(arguments);
98
99    // set up the value with sensible default event handlers.
100    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
101
102    // get details on keyboard and mouse bindings used by the viewer.
103    viewer.getUsage(*arguments.getApplicationUsage());
104
105    // if user request help write it out to cout.
106    if (arguments.read("-h") || arguments.read("--help"))
107    {
108        arguments.getApplicationUsage()->write(std::cout);
109        return 1;
110    }
111
112    // any option left unread are converted into errors to write out later.
113    arguments.reportRemainingOptionsAsUnrecognized();
114
115    // report any errors if they have occured when parsing the program aguments.
116    if (arguments.errors())
117    {
118        arguments.writeErrorMessages(std::cout);
119        return 1;
120    }
121   
122    if (arguments.argc()<=1)
123    {
124        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
125        return 1;
126    }
127
128
129    // load the nodes from the commandline arguments.
130    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
131    if (!rootnode)
132    {
133        return 1;
134    }
135   
136    // run optimization over the scene graph
137    osgUtil::Optimizer optimzer;
138    optimzer.optimize(rootnode);
139     
140    // add a viewport to the viewer and attach the scene graph.
141    viewer.setSceneData( rootnode );
142
143    geoHeader *gh = dynamic_cast<geoHeader *>(rootnode);
144    if (gh)
145    { // it is a geo file, so set function to update its animation variables.
146        ghand=new geodemoEventHandler();
147        gh->setUserUpdate(dodynamics);
148        viewer.getEventHandlerList().push_front(ghand);
149    }
150    else
151    { // maybe a group with geo models below.
152        osg::Group *gpall=dynamic_cast<osg::Group *>(rootnode);
153        if (gpall)
154        {
155            int nchild=gpall->getNumChildren();
156            for (int i=0; i<nchild; i++)
157            {
158                osg::Node *nod=gpall->getChild(i);
159                gh = dynamic_cast<geoHeader *>(nod);
160                if (gh)
161                {
162                    ghand=new geodemoEventHandler();
163                    gh->setUserUpdate(dodynamics);
164                    viewer.getEventHandlerList().push_front(ghand);
165                }
166            }
167        }
168    }
169
170    // create the windows and run the threads.
171    viewer.realize();
172
173    while( !viewer.done() )
174    {
175        // wait for all cull and draw threads to complete.
176        viewer.sync();
177
178        // update the scene by traversing it with the the update visitor which will
179        // call all node update callbacks and animations.
180        viewer.update();
181         
182        // fire off the cull and draw traversals of the scene.
183        viewer.frame();
184       
185    }
186   
187    // wait for all cull and draw threads to complete.
188    viewer.sync();
189
190    // run a clean up frame to delete all OpenGL objects.
191    viewer.cleanup_frame();
192
193    // wait for all the clean up frame to complete.
194    viewer.sync();
195
196    return 0;
197}
Note: See TracBrowser for help on using the browser.