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 <directionallight.h> |
---|
22 | #include <watersurface.h> |
---|
23 | |
---|
24 | |
---|
25 | // prototypes |
---|
26 | osg::Transform* createSky(float radius, char* filename); |
---|
27 | extern const char* version(); |
---|
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 |
---|
37 | std::string appname = arguments.getApplicationName(); |
---|
38 | arguments.getApplicationUsage()->setDescription( appname ); |
---|
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 0-1>","Transparency value at hmin"); |
---|
46 | arguments.getApplicationUsage()->addCommandLineOption("-alphamax <float 0-1>","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 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,300,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 requested help, write it out to cout |
---|
65 | if( arguments.read("-help") ) |
---|
66 | { |
---|
67 | arguments.getApplicationUsage()->write(std::cout); |
---|
68 | return 1; |
---|
69 | } |
---|
70 | |
---|
71 | // same for 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 tmpfloat, tps, vscale; |
---|
100 | if( !arguments.read("-tps", tps) || tps <= 0.0 ) tps = DEF_TPS; |
---|
101 | if( !arguments.read("-scale", vscale) ) vscale = 1.0; |
---|
102 | if( arguments.read("-hmin",tmpfloat) ) sww->setHeightMin( tmpfloat ); |
---|
103 | if( arguments.read("-hmax",tmpfloat) ) sww->setHeightMax( tmpfloat ); |
---|
104 | if( arguments.read("-alphamin",tmpfloat) ) sww->setAlphaMin( tmpfloat ); |
---|
105 | if( arguments.read("-alphamax",tmpfloat) ) sww->setAlphaMax( tmpfloat ); |
---|
106 | |
---|
107 | std::string bedslopetexture; |
---|
108 | if( arguments.read("-texture",bedslopetexture) ) sww->setBedslopeTexture( bedslopetexture ); |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | // root node |
---|
113 | osg::Group* rootnode = new osg::Group; |
---|
114 | |
---|
115 | // transform |
---|
116 | osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform; |
---|
117 | model->setName("position_attitude_transform"); |
---|
118 | |
---|
119 | // enscapsulates OpenGL state |
---|
120 | osg::StateSet* rootStateSet = new osg::StateSet; |
---|
121 | |
---|
122 | // Bedslope geometry |
---|
123 | BedSlope* bedslope = new BedSlope(sww); |
---|
124 | |
---|
125 | // Water geometry |
---|
126 | WaterSurface* water = new WaterSurface(sww); |
---|
127 | |
---|
128 | // Heads Up Display (text overlay) |
---|
129 | HeadsUpDisplay* hud = new HeadsUpDisplay(); |
---|
130 | hud->setTitle("pyVolution SWW Viewer"); |
---|
131 | |
---|
132 | |
---|
133 | // Lighting |
---|
134 | DirectionalLight* light = new DirectionalLight(rootStateSet); |
---|
135 | light->setPosition( osg::Vec3(0,0,2) ); // z is up |
---|
136 | |
---|
137 | |
---|
138 | // scenegraph hierarchy |
---|
139 | rootnode->setStateSet(rootStateSet); |
---|
140 | rootnode->addChild( hud->get() ); |
---|
141 | rootnode->addChild( light->get() ); |
---|
142 | rootnode->addChild(model); |
---|
143 | model->addChild( bedslope->get() ); |
---|
144 | model->addChild( water->get() ); |
---|
145 | |
---|
146 | |
---|
147 | // allow vertical scaling from command line parameter |
---|
148 | model->setScale( osg::Vec3(1.0, 1.0, vscale) ); |
---|
149 | |
---|
150 | // surrounding sky sphere |
---|
151 | if( !arguments.read("-nosky") ) |
---|
152 | rootnode->addChild( createSky(10.0, "sky_small.jpg") ); |
---|
153 | |
---|
154 | |
---|
155 | // add model to viewer. |
---|
156 | viewer.setSceneData(rootnode); |
---|
157 | |
---|
158 | // any option left unread are converted into errors to write out later. |
---|
159 | arguments.reportRemainingOptionsAsUnrecognized(); |
---|
160 | |
---|
161 | // report any errors if they have occured when parsing the program aguments. |
---|
162 | if (arguments.errors()) |
---|
163 | { |
---|
164 | arguments.writeErrorMessages(std::cout); |
---|
165 | return 1; |
---|
166 | } |
---|
167 | |
---|
168 | // register additional event handler |
---|
169 | KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps); |
---|
170 | viewer.getEventHandlerList().push_front(event_handler); |
---|
171 | |
---|
172 | |
---|
173 | // create the windows and run the threads. |
---|
174 | viewer.realize(); |
---|
175 | |
---|
176 | |
---|
177 | // initial camera position |
---|
178 | CustomTerrainManipulator* terrainmanipulator = viewer.getTerrainManipulator(); |
---|
179 | terrainmanipulator->setNode( model ); |
---|
180 | terrainmanipulator->setAutoComputeHomePosition( false ); |
---|
181 | terrainmanipulator->setHomePosition( |
---|
182 | osg::Vec3d(0,-3,0), // camera location |
---|
183 | osg::Vec3d(0,0,0), // camera target |
---|
184 | osg::Vec3d(0,0,1) ); // camera up vector |
---|
185 | terrainmanipulator->moveToHome(); |
---|
186 | terrainmanipulator->enable(); |
---|
187 | |
---|
188 | |
---|
189 | // second camera |
---|
190 | // Producer::ref_ptr<Producer::Camera> pcam1 = viewer.getCamera(0); |
---|
191 | // //pcam1->setProjectionRectangle( 0.0f, 0.5f, 0.0f, 1.0f ); |
---|
192 | |
---|
193 | // Producer::Camera* pcam2 = new Producer::Camera(); |
---|
194 | // pcam2->setRenderSurface( pcam1->getRenderSurface() ); |
---|
195 | // pcam2->setProjectionRectangle( 0.75f, 1.0f, 0.75f, 1.0f ); |
---|
196 | // viewer.getCameraConfig()->addCamera( "pictureInPicture", pcam2 ); |
---|
197 | |
---|
198 | // osgProducer::OsgSceneHandler* sh = new osgProducer::OsgSceneHandler(); |
---|
199 | // sh->getSceneView()->setSceneData( rootnode ); |
---|
200 | // pcam2->setSceneHandler( sh ); |
---|
201 | |
---|
202 | |
---|
203 | unsigned int timestep = 0; |
---|
204 | while( !viewer.done() ) |
---|
205 | { |
---|
206 | |
---|
207 | // wait for all cull and draw threads to complete. |
---|
208 | viewer.sync(); |
---|
209 | |
---|
210 | // current time |
---|
211 | double time = viewer.getFrameStamp()->getReferenceTime(); |
---|
212 | |
---|
213 | // advance sww frame? |
---|
214 | event_handler->setTime( time ); |
---|
215 | if( event_handler->timestepChanged() ) |
---|
216 | { |
---|
217 | timestep = event_handler->getTimestep(); |
---|
218 | water->setTimeStep(timestep); |
---|
219 | hud->setTime( sww->getTime(timestep) ); |
---|
220 | |
---|
221 | // bedslope->toggleTexture(); |
---|
222 | |
---|
223 | } |
---|
224 | |
---|
225 | // events |
---|
226 | if( event_handler->toggleWireframe() ) |
---|
227 | water->toggleWireframe(); |
---|
228 | |
---|
229 | // click-mouse movement can change either camera view or scene light position |
---|
230 | if( event_handler->toggleManipulatorMode() ) |
---|
231 | { |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | // update the scene by traversing with the update visitor |
---|
236 | viewer.update(); |
---|
237 | |
---|
238 | // fire off the cull and draw traversals of the scene. |
---|
239 | viewer.frame(); |
---|
240 | //pcam1->frame(false); |
---|
241 | //pcam2->frame(); |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | // wait for all cull and draw threads to complete before exit. |
---|
246 | viewer.sync(); |
---|
247 | |
---|
248 | return 0; |
---|
249 | } |
---|