source: Swollen/swollen/main.cpp @ 62

Last change on this file since 62 was 62, checked in by darran, 20 years ago
  • SWWReader is now extracting geospatial data from image passed via -texture <file> option
  • probably won't compile under Windows without changes to the solution file (I'll do this tommorrow)
File size: 7.1 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::Transform* createSky(float radius, char* filename);
27
28
29
30int main( int argc, char **argv )
31{
32
33    // use an ArgumentParser object to manage the program arguments.
34    osg::ArgumentParser arguments(&argc,argv);
35
36    // set up the usage document, in case we need to print out how to use this program.
37    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName());
38    arguments.getApplicationUsage()->setCommandLineUsage("swollen [options] swwfile ...");
39    arguments.getApplicationUsage()->addCommandLineOption("-help","Display this information");
40    arguments.getApplicationUsage()->addCommandLineOption("-scale <float>","Vertical scale factor");
41    arguments.getApplicationUsage()->addCommandLineOption("-tps <rate>","Timesteps per second");
42    arguments.getApplicationUsage()->addCommandLineOption("-hmin <float>","Height below which transparency is set to zero");   
43    arguments.getApplicationUsage()->addCommandLineOption("-hmax <float>","Height above which transparency is set to alphamax");       
44    arguments.getApplicationUsage()->addCommandLineOption("-alphamin <float>","Transparency value at hmin");   
45    arguments.getApplicationUsage()->addCommandLineOption("-alphamax <float>","Maximum transparency clamp value");
46    arguments.getApplicationUsage()->addCommandLineOption("-nosky","Omit background sky");
47    arguments.getApplicationUsage()->addCommandLineOption("-texture <file>","Image to use for bedslope topography");
48
49    // construct the viewer.
50    CustomViewer viewer(arguments);
51
52    // set up the value with sensible default event handlers.
53    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
54    viewer.setClearColor(osg::Vec4(DEF_BACKGROUND_COLOUR));
55    viewer.getCamera(0)->getRenderSurface()->setWindowRectangle(200,100,800,600);
56
57
58    // get details on keyboard and mouse bindings used by the viewer.
59    viewer.getUsage(*arguments.getApplicationUsage());
60
61    // if user request help write it out to cout.
62    if( arguments.read("-help") )
63    {
64        arguments.getApplicationUsage()->write(std::cout);
65        return 1;
66    }
67
68
69    // load sww file specified as final argument on command line (static bedslope
70    // geometry only, per timestep height field geometry is done in loop below)
71    int lastarg = arguments.argc()-1;
72    std::string swwfile = arguments.argv()[lastarg];
73    arguments.remove(lastarg);
74    if( swwfile.substr(swwfile.size()-4,4).find(".sww",0) == -1 )  // ensure filename ends in .sww
75    {
76        std::cout << "Require last argument be an .sww file ... quitting" << std::endl;
77        return 1; 
78    }
79    SWWReader *sww = new SWWReader(swwfile);
80    if (sww->isValid() == false)
81    {
82        std::cout << "Unable to load " << swwfile << " ... is this really an .sww file?" << std::endl;
83        return 1;
84    }
85
86
87    // default arguments and command line parameters
88    float tps;
89    if( !arguments.read("-tps",tps) || tps <= 0.0 ) tps = DEF_TPS;
90    float vscale;
91    if( !arguments.read("-scale",vscale) ) vscale = 1.0;
92
93    std::string bedslopetexture;
94    if( arguments.read("-texture",bedslopetexture) ) sww->setBedslopeTexture( bedslopetexture );
95
96    float tmpfloat;
97    if( arguments.read("-hmin",tmpfloat) ) sww->setHeightMin( tmpfloat ); 
98    if( arguments.read("-hmax",tmpfloat) ) sww->setHeightMax( tmpfloat );     
99    if( arguments.read("-alphamin",tmpfloat) ) sww->setAlphaMin( tmpfloat );   
100    if( arguments.read("-alphamax",tmpfloat) ) sww->setAlphaMax( tmpfloat );
101       
102
103    // root node
104    osg::Group* rootnode = new osg::Group;
105
106    // transform
107    osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform;
108    model->setName("position_attitude_transform");
109
110    // enscapsulates OpenGL state
111    osg::StateSet* rootStateSet = new osg::StateSet;
112
113    // Bedslope geometry
114    BedSlope* bedslope = new BedSlope(sww);
115
116    // Water geometry
117    WaterSurface* water = new WaterSurface(sww);
118
119    // Heads Up Display (text overlay)
120    HeadsUpDisplay* hud = new HeadsUpDisplay();
121    hud->setTitle("pyVolution SWW Viewer");
122
123
124    // Lighting
125    SpotLight* spotlight = new SpotLight(rootStateSet);
126    spotlight->setPosition( osg::Vec3(0,0,2) );  // overhead
127    spotlight->setSpotAngle( 45.0 );
128
129
130    // scenegraph hierarchy
131    rootnode->setStateSet(rootStateSet);
132    rootnode->addChild( hud->get() );
133    rootnode->addChild( spotlight->get() );
134    rootnode->addChild(model);
135    model->addChild( bedslope->get() );
136    model->addChild( water->get() );
137
138
139    // allow vertical scaling from command line parameter
140    model->setScale( osg::Vec3(1.0, 1.0, vscale) );
141
142    // surrounding sky sphere
143    if( !arguments.read("-nosky") )
144      rootnode->addChild( createSky(10.0, "sky_small.jpg") ); 
145
146
147    // add model to viewer.
148    viewer.setSceneData(rootnode);
149 
150    // any option left unread are converted into errors to write out later.
151    arguments.reportRemainingOptionsAsUnrecognized();
152 
153    // report any errors if they have occured when parsing the program aguments.
154    if (arguments.errors())
155    {
156        arguments.writeErrorMessages(std::cout);
157        return 1;
158    }
159
160    // register additional event handler
161    KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps);
162    viewer.getEventHandlerList().push_front(event_handler);
163
164 
165    // create the windows and run the threads.
166    viewer.realize();
167
168
169    // initial camera position
170    viewer.getTrackball()->setNode( rootnode );
171    viewer.getTrackball()->setAutoComputeHomePosition( false );
172    viewer.getTrackball()->setHomePosition(
173        osg::Vec3d(0,-3,0),    // camera location
174        osg::Vec3d(0,0,0),     // camera target
175        osg::Vec3d(0,0,1) );   // camera up vector
176    viewer.getTrackball()->moveToHome();
177
178
179    unsigned int timestep = 0;
180    while( !viewer.done() )
181    {
182        // wait for all cull and draw threads to complete.
183        viewer.sync();
184
185        // current time
186        double time = viewer.getFrameStamp()->getReferenceTime();
187
188        // advance sww frame?
189        event_handler->setTime( time );
190        if( event_handler->timestepChanged() )
191        {
192            timestep = event_handler->getTimestep();
193            water->setTimeStep(timestep);
194            hud->setTime( sww->getTime(timestep) );
195        }
196
197        // events
198        if( event_handler->toggleWireframe() )
199            water->toggleWireframe();
200
201
202        // update the scene by traversing with the update visitor
203        viewer.update();
204         
205        // fire off the cull and draw traversals of the scene.
206        viewer.frame();
207    }
208   
209    // wait for all cull and draw threads to complete before exit.
210    viewer.sync();
211
212    return 0;
213}
Note: See TracBrowser for help on using the repository browser.