source: Swollen/swollen/main.cpp @ 45

Last change on this file since 45 was 45, checked in by darran, 20 years ago

Major changes as per Robert's suggestions

  • removed all culling from swwreader
  • DrawElements? for primitive set improves speed
  • per vertex alpha (vec4 color)

Also,

  • initial view angle slightly above plane
File size: 6.0 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("-h or --help","Display this information");
40    arguments.getApplicationUsage()->addCommandLineOption("-scale <s>","Vertical scale factor");
41    arguments.getApplicationUsage()->addCommandLineOption("-tps <rate>","timesteps per second");
42
43    // construct the viewer.
44    CustomViewer viewer(arguments);
45
46    // set up the value with sensible default event handlers.
47    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
48    viewer.setClearColor(osg::Vec4(DEF_BACKGROUND_COLOUR));
49    viewer.getCamera(0)->getRenderSurface()->setWindowRectangle(200,100,800,600);
50
51
52    // get details on keyboard and mouse bindings used by the viewer.
53    viewer.getUsage(*arguments.getApplicationUsage());
54
55    // if user request help write it out to cout.
56    if (arguments.read("-h") || arguments.read("--help"))
57    {
58        arguments.getApplicationUsage()->write(std::cout);
59        return 1;
60    }
61
62
63    // load sww file specified as final argument on command line (static
64    // geometry only, per timestep height field geometry is done in loop below)
65    int lastarg = arguments.argc()-1;
66    std::string swwfile = arguments.argv()[lastarg];
67    arguments.remove(lastarg);
68    if( swwfile.substr(swwfile.size()-4,4).find(".sww",0) == -1 )  // ensure filename ends in .sww
69    {
70        std::cout << "Require last argument be an .sww file ... quitting" << std::endl;
71        return 1; 
72    }
73    SWWReader *sww = new SWWReader(swwfile);
74    if (sww->isValid() == false)
75    {
76        std::cout << "Unable to load " << swwfile << " ... is this really an .sww file?" << std::endl;
77        return 1;
78    }
79
80
81    // default arguments
82    float tps;
83    if( !arguments.read("-tps",tps) || tps <= 0.0 ) tps = DEF_TPS;
84    float vscale;
85    if( !arguments.read("-scale",vscale) ) vscale = 1.0;
86
87
88    // root node
89    osg::Group* rootnode = new osg::Group;
90
91    // transform
92    osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform;
93    model->setName("model_transform");
94
95    // enscapsulates OpenGL state
96    osg::StateSet* rootStateSet = new osg::StateSet;
97
98    // Bedslope geometry
99    BedSlope* bedslope = new BedSlope(sww);
100
101    // Water geometry
102    WaterSurface* water = new WaterSurface(sww);
103
104    // Heads Up Display (text overlay)
105    HeadsUpDisplay* hud = new HeadsUpDisplay();
106    hud->setTitle("pyVolution SWW Viewer");
107
108
109    // Lighting
110    SpotLight* spotlight = new SpotLight(rootStateSet);
111    spotlight->setPosition( osg::Vec3(0,0,2) );  // overhead
112    spotlight->setSpotAngle( 45.0 );
113
114
115    // scenegraph hierarchy
116    rootnode->setStateSet(rootStateSet);
117    rootnode->addChild( hud->get() );
118    rootnode->addChild( spotlight->get() );
119    rootnode->addChild(model);
120    model->addChild( bedslope->get() );
121    model->addChild( water->get() );
122
123
124    // FIXME: should be reasonable default and adjustable
125    model->setScale( osg::Vec3(1.0, 1.0, vscale) );
126
127    // sky
128    rootnode->addChild( createSky(10.0, "sky_small.jpg") ); 
129
130
131    // add model to viewer.
132    viewer.setSceneData(rootnode);
133 
134    // any option left unread are converted into errors to write out later.
135    arguments.reportRemainingOptionsAsUnrecognized();
136 
137    // report any errors if they have occured when parsing the program aguments.
138    if (arguments.errors())
139    {
140        arguments.writeErrorMessages(std::cout);
141        return 1;
142    }
143
144    // register additional event handler
145    KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps);
146    viewer.getEventHandlerList().push_front(event_handler);
147
148 
149    // create the windows and run the threads.
150    viewer.realize();
151
152
153    // initial camera position
154    viewer.getTrackball()->setNode( rootnode );
155    viewer.getTrackball()->setAutoComputeHomePosition(false);
156    viewer.getTrackball()->setHomePosition(
157        osg::Vec3d(0,-4,1),    // camera location
158        osg::Vec3d(0,0,0),     // camera target
159        osg::Vec3d(0,0,1) );   // camera up vector
160    viewer.getTrackball()->moveToHome();
161
162
163    unsigned int timestep = 0;
164    while( !viewer.done() )
165    {
166        // wait for all cull and draw threads to complete.
167        viewer.sync();
168
169        // current time
170        double time = viewer.getFrameStamp()->getReferenceTime();
171
172        // advance sww frame?
173        event_handler->setTime( time );
174        if( event_handler->timestepChanged() )
175        {
176            timestep = event_handler->getTimestep();
177            water->setTimeStep(timestep);
178            hud->setTime( sww->getTime(timestep) );
179        }
180
181        // events
182        if( event_handler->toggleWireframe() )
183            water->toggleWireframe();
184        if( event_handler->toggleCullSetting() )
185        {
186            sww->toggleCullSetting();
187                water->setTimeStep(timestep);  // forced reload
188        }
189
190
191        // update the scene by traversing with the update visitor
192        viewer.update();
193         
194        // fire off the cull and draw traversals of the scene.
195        viewer.frame();
196    }
197   
198    // wait for all cull and draw threads to complete before exit.
199    viewer.sync();
200
201    return 0;
202}
Note: See TracBrowser for help on using the repository browser.