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 <customtrackball.h> |
---|
19 | #include <customviewer.h> |
---|
20 | #include <hud.h> |
---|
21 | #include <keyboardeventhandler.h> |
---|
22 | #include <spotlight.h> |
---|
23 | #include <watersurface.h> |
---|
24 | |
---|
25 | |
---|
26 | // prototypes |
---|
27 | osg::Transform* createSky(float radius, char* filename); |
---|
28 | extern const char* version(); |
---|
29 | |
---|
30 | |
---|
31 | int main( int argc, char **argv ) |
---|
32 | { |
---|
33 | |
---|
34 | // use an ArgumentParser object to manage the program arguments |
---|
35 | osg::ArgumentParser arguments(&argc,argv); |
---|
36 | |
---|
37 | // set up the usage document |
---|
38 | std::string appname = arguments.getApplicationName(); |
---|
39 | arguments.getApplicationUsage()->setDescription( appname ); |
---|
40 | arguments.getApplicationUsage()->setCommandLineUsage("swollen [options] swwfile ..."); |
---|
41 | arguments.getApplicationUsage()->addCommandLineOption("-help","Display this information"); |
---|
42 | arguments.getApplicationUsage()->addCommandLineOption("-scale <float>","Vertical scale factor"); |
---|
43 | arguments.getApplicationUsage()->addCommandLineOption("-tps <rate>","Timesteps per second"); |
---|
44 | arguments.getApplicationUsage()->addCommandLineOption("-hmin <float>","Height below which transparency is set to zero"); |
---|
45 | arguments.getApplicationUsage()->addCommandLineOption("-hmax <float>","Height above which transparency is set to alphamax"); |
---|
46 | arguments.getApplicationUsage()->addCommandLineOption("-alphamin <float>","Transparency value at hmin"); |
---|
47 | arguments.getApplicationUsage()->addCommandLineOption("-alphamax <float>","Maximum transparency clamp value"); |
---|
48 | arguments.getApplicationUsage()->addCommandLineOption("-nosky","Omit background sky"); |
---|
49 | arguments.getApplicationUsage()->addCommandLineOption("-texture <file>","Image to use for bedslope topography"); |
---|
50 | arguments.getApplicationUsage()->addCommandLineOption("-version","Revision number and creation (not compile) date"); |
---|
51 | |
---|
52 | // construct the viewer. |
---|
53 | CustomViewer viewer(arguments); |
---|
54 | |
---|
55 | // set up with sensible default event handlers |
---|
56 | viewer.setUpViewer( osgProducer::Viewer::STANDARD_SETTINGS ); |
---|
57 | viewer.setClearColor( osg::Vec4(DEF_BACKGROUND_COLOUR) ); |
---|
58 | viewer.getCamera(0)->getRenderSurface()->setWindowRectangle(200,100,800,600); |
---|
59 | viewer.getCullSettings().setComputeNearFarMode( osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES ); |
---|
60 | |
---|
61 | |
---|
62 | // get details on keyboard and mouse bindings used by the viewer |
---|
63 | viewer.getUsage(*arguments.getApplicationUsage()); |
---|
64 | |
---|
65 | // if user requested help, write it out to cout |
---|
66 | if( arguments.read("-help") ) |
---|
67 | { |
---|
68 | arguments.getApplicationUsage()->write(std::cout); |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | // same for version info |
---|
73 | if( arguments.read("-version") ) |
---|
74 | { |
---|
75 | std::cout << version() << std::endl; |
---|
76 | return 1; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | // load sww file specified as final argument on command line (static bedslope |
---|
82 | // geometry only, per timestep height field geometry is done in loop below) |
---|
83 | int lastarg = arguments.argc()-1; |
---|
84 | std::string swwfile = arguments.argv()[lastarg]; |
---|
85 | arguments.remove(lastarg); |
---|
86 | if( swwfile.substr(swwfile.size()-4,4).find(".sww",0) == -1 ) // ensure filename ends in .sww |
---|
87 | { |
---|
88 | std::cout << "Require last argument be an .sww file ... quitting" << std::endl; |
---|
89 | return 1; |
---|
90 | } |
---|
91 | SWWReader *sww = new SWWReader(swwfile); |
---|
92 | if (sww->isValid() == false) |
---|
93 | { |
---|
94 | std::cout << "Unable to load " << swwfile << " ... is this really an .sww file?" << std::endl; |
---|
95 | return 1; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | // default arguments and command line parameters |
---|
100 | float tps; |
---|
101 | if( !arguments.read("-tps",tps) || tps <= 0.0 ) tps = DEF_TPS; |
---|
102 | float vscale; |
---|
103 | if( !arguments.read("-scale",vscale) ) vscale = 1.0; |
---|
104 | |
---|
105 | std::string bedslopetexture; |
---|
106 | if( arguments.read("-texture",bedslopetexture) ) sww->setBedslopeTexture( bedslopetexture ); |
---|
107 | |
---|
108 | float tmpfloat; |
---|
109 | if( arguments.read("-hmin",tmpfloat) ) sww->setHeightMin( tmpfloat ); |
---|
110 | if( arguments.read("-hmax",tmpfloat) ) sww->setHeightMax( tmpfloat ); |
---|
111 | if( arguments.read("-alphamin",tmpfloat) ) sww->setAlphaMin( tmpfloat ); |
---|
112 | if( arguments.read("-alphamax",tmpfloat) ) sww->setAlphaMax( tmpfloat ); |
---|
113 | |
---|
114 | |
---|
115 | // root node |
---|
116 | osg::Group* rootnode = new osg::Group; |
---|
117 | |
---|
118 | // transform |
---|
119 | osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform; |
---|
120 | model->setName("position_attitude_transform"); |
---|
121 | |
---|
122 | // enscapsulates OpenGL state |
---|
123 | osg::StateSet* rootStateSet = new osg::StateSet; |
---|
124 | |
---|
125 | // Bedslope geometry |
---|
126 | BedSlope* bedslope = new BedSlope(sww); |
---|
127 | |
---|
128 | // Water geometry |
---|
129 | WaterSurface* water = new WaterSurface(sww); |
---|
130 | |
---|
131 | // Heads Up Display (text overlay) |
---|
132 | HeadsUpDisplay* hud = new HeadsUpDisplay(); |
---|
133 | hud->setTitle("pyVolution SWW Viewer"); |
---|
134 | |
---|
135 | |
---|
136 | // Lighting |
---|
137 | SpotLight* spotlight = new SpotLight(rootStateSet); |
---|
138 | spotlight->setPosition( osg::Vec3(0,1,0) ); // z is up |
---|
139 | spotlight->setSpotAngle( 45.0 ); |
---|
140 | |
---|
141 | |
---|
142 | // scenegraph hierarchy |
---|
143 | rootnode->setStateSet(rootStateSet); |
---|
144 | rootnode->addChild( hud->get() ); |
---|
145 | rootnode->addChild( spotlight->get() ); |
---|
146 | rootnode->addChild(model); |
---|
147 | model->addChild( bedslope->get() ); |
---|
148 | model->addChild( water->get() ); |
---|
149 | |
---|
150 | |
---|
151 | // allow vertical scaling from command line parameter |
---|
152 | model->setScale( osg::Vec3(1.0, 1.0, vscale) ); |
---|
153 | |
---|
154 | // surrounding sky sphere |
---|
155 | if( !arguments.read("-nosky") ) |
---|
156 | rootnode->addChild( createSky(10.0, "sky_small.jpg") ); |
---|
157 | |
---|
158 | |
---|
159 | // add model to viewer. |
---|
160 | viewer.setSceneData(rootnode); |
---|
161 | |
---|
162 | // any option left unread are converted into errors to write out later. |
---|
163 | arguments.reportRemainingOptionsAsUnrecognized(); |
---|
164 | |
---|
165 | // report any errors if they have occured when parsing the program aguments. |
---|
166 | if (arguments.errors()) |
---|
167 | { |
---|
168 | arguments.writeErrorMessages(std::cout); |
---|
169 | return 1; |
---|
170 | } |
---|
171 | |
---|
172 | // register additional event handler |
---|
173 | KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps); |
---|
174 | viewer.getEventHandlerList().push_front(event_handler); |
---|
175 | |
---|
176 | |
---|
177 | // create the windows and run the threads. |
---|
178 | viewer.realize(); |
---|
179 | |
---|
180 | |
---|
181 | // initial camera position |
---|
182 | CustomTrackballManipulator* trackball = viewer.getTrackball(); |
---|
183 | trackball->setNode( model ); |
---|
184 | trackball->setAutoComputeHomePosition( false ); |
---|
185 | trackball->setHomePosition( |
---|
186 | osg::Vec3d(0,1,0), // location |
---|
187 | osg::Vec3d(0,0,0), // target |
---|
188 | osg::Vec3d(0,0,1) ); // up vector |
---|
189 | trackball->moveToHome(); |
---|
190 | trackball->disable(); |
---|
191 | |
---|
192 | |
---|
193 | CustomTerrainManipulator* terrainmanipulator = viewer.getTerrainManipulator(); |
---|
194 | terrainmanipulator->setNode( rootnode ); |
---|
195 | terrainmanipulator->setAutoComputeHomePosition( false ); |
---|
196 | terrainmanipulator->setHomePosition( |
---|
197 | osg::Vec3d(0,-3,0), // camera location |
---|
198 | osg::Vec3d(0,0,0), // camera target |
---|
199 | osg::Vec3d(0,0,1) ); // camera up vector |
---|
200 | terrainmanipulator->moveToHome(); |
---|
201 | terrainmanipulator->enable(); |
---|
202 | |
---|
203 | |
---|
204 | unsigned int timestep = 0; |
---|
205 | while( !viewer.done() ) |
---|
206 | { |
---|
207 | |
---|
208 | // wait for all cull and draw threads to complete. |
---|
209 | viewer.sync(); |
---|
210 | |
---|
211 | // current time |
---|
212 | double time = viewer.getFrameStamp()->getReferenceTime(); |
---|
213 | |
---|
214 | // advance sww frame? |
---|
215 | event_handler->setTime( time ); |
---|
216 | if( event_handler->timestepChanged() ) |
---|
217 | { |
---|
218 | timestep = event_handler->getTimestep(); |
---|
219 | water->setTimeStep(timestep); |
---|
220 | hud->setTime( sww->getTime(timestep) ); |
---|
221 | } |
---|
222 | |
---|
223 | // light position manipulator |
---|
224 | if( trackball->isEnabled() ) |
---|
225 | { |
---|
226 | //osg::Matrixd matrix = trackball->getInverseMatrix(); |
---|
227 | osg::Matrixd matrix = trackball->getMatrix(); |
---|
228 | osg::Vec3d position = matrix.getTrans(); |
---|
229 | //std::cout << "trackball position x: " << position. << std::endl; |
---|
230 | spotlight->setPosition( position ); |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | // events |
---|
235 | if( event_handler->toggleWireframe() ) |
---|
236 | water->toggleWireframe(); |
---|
237 | |
---|
238 | // click-mouse movement can change either camera view or scene light position |
---|
239 | if( event_handler->toggleManipulatorMode() ) |
---|
240 | { |
---|
241 | if( trackball->isEnabled() ) |
---|
242 | { |
---|
243 | trackball->disable(); |
---|
244 | terrainmanipulator->enable(); |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | trackball->enable(); |
---|
249 | terrainmanipulator->disable(); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | // update the scene by traversing with the update visitor |
---|
255 | viewer.update(); |
---|
256 | |
---|
257 | // fire off the cull and draw traversals of the scene. |
---|
258 | viewer.frame(); |
---|
259 | } |
---|
260 | |
---|
261 | // wait for all cull and draw threads to complete before exit. |
---|
262 | viewer.sync(); |
---|
263 | |
---|
264 | return 0; |
---|
265 | } |
---|