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

Revision 5927, 4.1 kB (checked in by robert, 8 years ago)

Ported following examples to osgViewer:

osggeodemo
osggeometry
osghud
osgimpostor
osgkeyboard
osglauncher
osglight
osglightpoint
osglogicop
osglogo
osgmovie
osgmultiplecameras
osgmultitexture
osgoccluder
osgparametric
osgparticle

  • 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 <osgViewer/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#include <iostream>
24
25// currently not a satisfactory solution, but this is early days for the
26// geo loader and having direct links with it.
27#include "../../src/osgPlugins/geo/osgGeoAnimation.h"
28
29
30//== event trapper gets events
31
32class geodemoEventHandler : public osgGA::GUIEventHandler
33{
34public:
35   
36    geodemoEventHandler( )     { mouse_x=mouse_y=0;}
37    virtual ~geodemoEventHandler( ) {}
38
39    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
40    {
41        mouse_x=ea.getX();
42        mouse_y=ea.getY();
43        return false;
44       
45    }
46
47    inline float getMouseX(void) {return mouse_x;};
48    inline float getMouseY(void) {return mouse_y;};
49   
50private:
51    float    mouse_x, mouse_y;
52};
53
54static geodemoEventHandler *ghand=NULL;
55inline double DEG2RAD(const double val) { return val*0.0174532925199432957692369076848861;}
56inline double RAD2DEG(const double val) { return val*57.2957795130823208767981548141052;}
57
58double dodynamics(const double /*time*/, const double val, const std::string name)
59{ // Each local variable named 'name' declared in the geo modeller is passed into here.
60    // its current value is val; returns new value.  Time - elapsed time
61    static double heading,speed; // these are only required for my 'dynamics'
62    if (name == "xpos") {
63        return (val+speed*sin(heading));
64        //    std::cout << " nx " << (*itr->getValue()) ;
65    } else if (name == "ypos") {
66        return (val+speed*cos(heading));
67        //    std::cout << " ny " << (*itr->getValue()) ;
68    } else if (name == "sped") {
69        speed=(0.00025*(ghand->getMouseY()-300)); // (*itr->getValue());
70        return (speed);
71    } else if (name == "heading") {
72        heading-= 0.01*DEG2RAD(ghand->getMouseX()-400); // =DEG2RAD(*itr->getValue());
73        return (RAD2DEG(heading));
74    } else if (name == "conerot") {
75        return ((ghand->getMouseX()-400));
76    } else if (name == "planrot") {
77        return ((ghand->getMouseY()-300)/200.0);
78    } else if (name == "secint" || name == "minutehand"|| name == "hourhand") {
79    //    std::cout << " updating " << name << " " << val << std::endl;
80    }
81    return val;
82}
83
84int main( int argc, char **argv )
85{
86    // use an ArgumentParser object to manage the program arguments.
87    osg::ArgumentParser arguments(&argc,argv);
88
89    // construct the viewer.
90    osgViewer::Viewer viewer;
91
92    // load the nodes from the commandline arguments.
93    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
94    if (!rootnode)
95    {
96        osg::notify(osg::NOTICE)<<"Please specify and geo model filename on the command line."<<std::endl;
97        return 1;
98    }
99   
100    // run optimization over the scene graph
101    osgUtil::Optimizer optimzer;
102    optimzer.optimize(rootnode);
103     
104    // add a viewport to the viewer and attach the scene graph.
105    viewer.setSceneData( rootnode );
106
107    geoHeader *gh = dynamic_cast<geoHeader *>(rootnode);
108    if (gh)
109    { // it is a geo file, so set function to update its animation variables.
110        ghand=new geodemoEventHandler();
111        gh->setUserUpdate(dodynamics);
112        viewer.addEventHandler(ghand);
113    }
114    else
115    { // maybe a group with geo models below.
116        osg::Group *gpall=dynamic_cast<osg::Group *>(rootnode);
117        if (gpall)
118        {
119            int nchild=gpall->getNumChildren();
120            for (int i=0; i<nchild; i++)
121            {
122                osg::Node *nod=gpall->getChild(i);
123                gh = dynamic_cast<geoHeader *>(nod);
124                if (gh)
125                {
126                    ghand=new geodemoEventHandler();
127                    gh->setUserUpdate(dodynamics);
128                    viewer.addEventHandler(ghand);
129                }
130            }
131        }
132    }
133
134    return viewer.run();
135}
Note: See TracBrowser for help on using the browser.