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

Revision 1747, 5.9 kB (checked in by robert, 12 years ago)

Added a viewer.sync to the end of all the demos.

Removed the Win32 remapping of keycodes from the osgProducer::EventAdapter?.

  • 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.getProgramName()+" [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
126    // load the nodes from the commandline arguments.
127    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
128    if (!rootnode)
129    {
130        return 1;
131    }
132   
133    // run optimization over the scene graph
134    osgUtil::Optimizer optimzer;
135    optimzer.optimize(rootnode);
136     
137    // add a viewport to the viewer and attach the scene graph.
138    viewer.setSceneData( rootnode );
139
140    geoHeader *gh = dynamic_cast<geoHeader *>(rootnode);
141    if (gh)
142    { // it is a geo file, so set function to update its animation variables.
143        ghand=new geodemoEventHandler();
144        gh->setUserUpdate(dodynamics);
145        viewer.getEventHandlerList().push_front(ghand);
146    }
147    else
148    { // maybe a group with geo models below.
149        osg::Group *gpall=dynamic_cast<osg::Group *>(rootnode);
150        if (gpall)
151        {
152            int nchild=gpall->getNumChildren();
153            for (int i=0; i<nchild; i++)
154            {
155                osg::Node *nod=gpall->getChild(i);
156                gh = dynamic_cast<geoHeader *>(nod);
157                if (gh)
158                {
159                    ghand=new geodemoEventHandler();
160                    gh->setUserUpdate(dodynamics);
161                    viewer.getEventHandlerList().push_front(ghand);
162                }
163            }
164        }
165    }
166
167    // create the windows and run the threads.
168    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
169
170    while( !viewer.done() )
171    {
172        // wait for all cull and draw threads to complete.
173        viewer.sync();
174
175        // update the scene by traversing it with the the update visitor which will
176        // call all node update callbacks and animations.
177        viewer.update();
178         
179        // fire off the cull and draw traversals of the scene.
180        viewer.frame();
181       
182    }
183   
184    // wait for all cull and draw threads to complete before exit.
185    viewer.sync();
186
187    return 0;
188}
Note: See TracBrowser for help on using the browser.