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