source: Swollen/swollen/main.cpp @ 30

Last change on this file since 30 was 15, checked in by darran, 20 years ago
  • admin only
File size: 5.8 KB
Line 
1/*
2    SWWViewer
3
4    An OpenSceneGraph viewer for pyVolution SWW files.
5    copyright (C) 2004 Geoscience Australia
6*/
7
8#include <osg/Group>
9#include <osg/Material>
10#include <osg/MatrixTransform>
11#include <osg/Notify>
12#include <osg/PositionAttitudeTransform>
13#include <osg/StateAttribute>
14
15#include <project.h>
16#include <SWWReader.h>
17#include <bedslope.h>
18#include <customviewer.h>
19#include <hud.h>
20#include <keyboardeventhandler.h>
21#include <spotlight.h>
22#include <watersurface.h>
23
24
25// prototypes
26osg::Geode* createSky(float radius, char* filename);
27
28
29
30int main( int argc, char **argv )
31{
32    // use an ArgumentParser object to manage the program arguments.
33    osg::ArgumentParser arguments(&argc,argv);
34
35    // set up the usage document, in case we need to print out how to use this program.
36    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName());
37    arguments.getApplicationUsage()->setCommandLineUsage("swwviewer [options] swwfile ...");
38   
39    // construct the viewer.
40    CustomViewer viewer(arguments);
41
42    // set up the value with sensible default event handlers.
43    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
44    viewer.setClearColor(osg::Vec4(DEF_BACKGROUND_COLOUR));
45    viewer.getCamera(0)->getRenderSurface()->setWindowRectangle(550,100,640,480);
46
47
48    // get details on keyboard and mouse bindings used by the viewer.
49    viewer.getUsage(*arguments.getApplicationUsage());
50
51    // if user request help write it out to cout.
52    if (arguments.read("-h") || arguments.read("--help"))
53    {
54        arguments.getApplicationUsage()->write(std::cout);
55        return 1;
56    }
57
58
59    // load sww file specified as final argument on command line (static
60    // geometry only, per timestep height field geometry is done in loop below)
61    int lastarg = arguments.argc()-1;
62    std::string swwfile = arguments.argv()[lastarg];
63    arguments.remove(lastarg);
64    if( swwfile.substr(swwfile.size()-4,4).find(".sww",0) == -1 )  // ensure filename ends in .sww
65    {
66        std::cout << "Require last argument be an .sww file ... quitting" << std::endl;
67        return 1; 
68    }
69    SWWReader *sww = new SWWReader(swwfile);
70    if (sww->isValid() == false)
71    {
72        std::cout << "Could not load " << swwfile << " ... quitting" << std::endl;
73        return 1;
74    }
75
76
77    // default arguments
78    float tps;
79    if( !arguments.read("-tps",tps) || tps <= 0.0 ) tps = DEF_TPS;
80
81
82    // root node
83    osg::Group* rootnode = new osg::Group;
84
85    // transform
86    osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform;
87    model->setName("model_transform");
88
89    // enscapsulates OpenGL state
90    osg::StateSet* rootStateSet = new osg::StateSet;
91
92    // Bedslope geometry
93    BedSlope* bedslope = new BedSlope(sww);
94
95    // Water geometry
96    WaterSurface* water = new WaterSurface(sww);
97
98    // Heads Up Display (text overlay)
99    HeadsUpDisplay* hud = new HeadsUpDisplay();
100    hud->setTitle("pyVolution SWW Viewer");
101
102    // Lighting
103    SpotLight* spotlight = new SpotLight(rootStateSet);
104    spotlight->setPosition( osg::Vec3(0,0,2) );  // overhead
105    spotlight->setSpotAngle( 45.0 );
106
107    // scenegraph hierarchy
108    rootnode->setStateSet(rootStateSet);
109    rootnode->addChild( hud->get() );
110    rootnode->addChild( spotlight->get() );
111    rootnode->addChild(model);
112    model->addChild( bedslope->get() );
113    model->addChild( water->get() );
114
115
116    // center model in x and y (now done in swwreader)
117    /*
118    osg::BoundingBox bbox = bedslope->getBound();
119    float scale = 1.0/bbox.radius();
120    model->setPosition( -bbox.center()*scale );
121    */
122
123    // FIXME: should be reasonable default and adjustable
124    model->setScale( osg::Vec3(1.0, 1.0, 0.2) );
125
126    // sky
127    rootnode->addChild( createSky(10.0, "sky_small.jpg") ); 
128
129
130    // add model to viewer.
131    viewer.setSceneData(rootnode);
132 
133    // any option left unread are converted into errors to write out later.
134    arguments.reportRemainingOptionsAsUnrecognized();
135 
136    // report any errors if they have occured when parsing the program aguments.
137    if (arguments.errors())
138    {
139        arguments.writeErrorMessages(std::cout);
140        return 1;
141    }
142
143    // register additional event handler
144    KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps);
145    viewer.getEventHandlerList().push_front(event_handler);
146 
147    // create the windows and run the threads.
148    viewer.realize();
149
150
151    // Initial camera position
152    viewer.getTrackball()->setNode( rootnode );
153    viewer.getTrackball()->setAutoComputeHomePosition(false);
154    viewer.getTrackball()->setHomePosition(
155        osg::Vec3d(0,-4,0),  // camera location
156        osg::Vec3d(0,0,0),     // camera target
157        osg::Vec3d(0,0,1) );   // camera up vector
158    viewer.getTrackball()->moveToHome();
159
160
161    unsigned int timestep = 0;
162    while( !viewer.done() )
163    {
164        // wait for all cull and draw threads to complete.
165        viewer.sync();
166
167        // current time
168        double time = viewer.getFrameStamp()->getReferenceTime();
169
170        // advance sww frame?
171        event_handler->setTime( time );
172        if( event_handler->timestepChanged() )
173        {
174            timestep = event_handler->getTimestep();
175            water->setTimeStep(timestep);
176            hud->setTime( sww->getTime(timestep) );
177        }
178
179        // events
180        if( event_handler->toggleWireframe() )
181            water->toggleWireframe();
182        if( event_handler->toggleCullSetting() )
183        {
184            sww->toggleCullSetting();
185                water->setTimeStep(timestep);  // forced reload
186        }
187
188
189        // update the scene by traversing with the update visitor
190        viewer.update();
191         
192        // fire off the cull and draw traversals of the scene.
193        viewer.frame();
194    }
195   
196    // wait for all cull and draw threads to complete before exit.
197    viewer.sync();
198
199    return 0;
200}
Note: See TracBrowser for help on using the repository browser.