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