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

Revision 9692, 5.1 kB (checked in by robert, 6 years ago)

From Gary Quinn, spelling fixes

  • 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/* OpenSceneGraph example, osggeodemo.
3*
4*  Permission is hereby granted, free of charge, to any person obtaining a copy
5*  of this software and associated documentation files (the "Software"), to deal
6*  in the Software without restriction, including without limitation the rights
7*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8*  copies of the Software, and to permit persons to whom the Software is
9*  furnished to do so, subject to the following conditions:
10*
11*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
17*  THE SOFTWARE.
18*/
19
20#include <stdio.h>
21#include <osgViewer/Viewer>
22
23#include <osg/Node>
24#include <osg/Notify>
25
26#include <osgDB/Registry>
27#include <osgDB/ReadFile>
28
29#include <osgGA/TrackballManipulator>
30#include <osgGA/FlightManipulator>
31#include <osgGA/DriveManipulator>
32#include <osgGA/AnimationPathManipulator>
33
34#include <osgDB/WriteFile>
35#include <osgDB/FileUtils>
36#include <osgDB/FileNameUtils>
37
38#include <osgUtil/Optimizer>
39
40#include <iostream>
41
42// currently not a satisfactory solution, but this is early days for the
43// geo loader and having direct links with it.
44#include "../../src/osgPlugins/geo/osgGeoAnimation.h"
45
46
47//== event trapper gets events
48
49class geodemoEventHandler : public osgGA::GUIEventHandler
50{
51public:
52   
53    geodemoEventHandler( )     { mouse_x=mouse_y=0;}
54    virtual ~geodemoEventHandler( ) {}
55
56    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
57    {
58        mouse_x=ea.getX();
59        mouse_y=ea.getY();
60        return false;
61       
62    }
63
64    inline float getMouseX(void) {return mouse_x;};
65    inline float getMouseY(void) {return mouse_y;};
66   
67private:
68    float    mouse_x, mouse_y;
69};
70
71static geodemoEventHandler *ghand=NULL;
72inline double DEG2RAD(const double val) { return val*0.0174532925199432957692369076848861;}
73inline double RAD2DEG(const double val) { return val*57.2957795130823208767981548141052;}
74
75double dodynamics(const double /*time*/, const double val, const std::string name)
76{ // Each local variable named 'name' declared in the geo modeller is passed into here.
77    // its current value is val; returns new value.  Time - elapsed time
78    static double heading,speed; // these are only required for my 'dynamics'
79    if (name == "xpos") {
80        return (val+speed*sin(heading));
81        //    std::cout << " nx " << (*itr->getValue()) ;
82    } else if (name == "ypos") {
83        return (val+speed*cos(heading));
84        //    std::cout << " ny " << (*itr->getValue()) ;
85    } else if (name == "sped") {
86        speed=(0.00025*(ghand->getMouseY()-300)); // (*itr->getValue());
87        return (speed);
88    } else if (name == "heading") {
89        heading-= 0.01*DEG2RAD(ghand->getMouseX()-400); // =DEG2RAD(*itr->getValue());
90        return (RAD2DEG(heading));
91    } else if (name == "conerot") {
92        return ((ghand->getMouseX()-400));
93    } else if (name == "planrot") {
94        return ((ghand->getMouseY()-300)/200.0);
95    } else if (name == "secint" || name == "minutehand"|| name == "hourhand") {
96    //    std::cout << " updating " << name << " " << val << std::endl;
97    }
98    return val;
99}
100
101int main( int argc, char **argv )
102{
103    // use an ArgumentParser object to manage the program arguments.
104    osg::ArgumentParser arguments(&argc,argv);
105
106    // construct the viewer.
107    osgViewer::Viewer viewer;
108
109    // load the nodes from the commandline arguments.
110    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
111    if (!rootnode)
112    {
113        osg::notify(osg::NOTICE)<<"Please specify a geo model filename on the command line."<<std::endl;
114        return 1;
115    }
116   
117    // run optimization over the scene graph
118    osgUtil::Optimizer optimzer;
119    optimzer.optimize(rootnode);
120     
121    // add a viewport to the viewer and attach the scene graph.
122    viewer.setSceneData( rootnode );
123
124    geoHeader *gh = dynamic_cast<geoHeader *>(rootnode);
125    if (gh)
126    { // it is a geo file, so set function to update its animation variables.
127        ghand=new geodemoEventHandler();
128        gh->setUserUpdate(dodynamics);
129        viewer.addEventHandler(ghand);
130    }
131    else
132    { // maybe a group with geo models below.
133        osg::Group *gpall=dynamic_cast<osg::Group *>(rootnode);
134        if (gpall)
135        {
136            int nchild=gpall->getNumChildren();
137            for (int i=0; i<nchild; i++)
138            {
139                osg::Node *nod=gpall->getChild(i);
140                gh = dynamic_cast<geoHeader *>(nod);
141                if (gh)
142                {
143                    ghand=new geodemoEventHandler();
144                    gh->setUserUpdate(dodynamics);
145                    viewer.addEventHandler(ghand);
146                }
147            }
148        }
149    }
150
151    return viewer.run();
152}
Note: See TracBrowser for help on using the browser.