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