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::Geode* createSky(float radius, char* filename); |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | int main( int argc, char **argv ) |
---|
31 | { |
---|
32 | |
---|
33 | std::cout << argv[0] << std::endl; |
---|
34 | |
---|
35 | // use an ArgumentParser object to manage the program arguments. |
---|
36 | osg::ArgumentParser arguments(&argc,argv); |
---|
37 | |
---|
38 | // set up the usage document, in case we need to print out how to use this program. |
---|
39 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()); |
---|
40 | arguments.getApplicationUsage()->setCommandLineUsage("swwviewer [options] swwfile ..."); |
---|
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 | |
---|
84 | |
---|
85 | // root node |
---|
86 | osg::Group* rootnode = new osg::Group; |
---|
87 | |
---|
88 | // transform |
---|
89 | osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform; |
---|
90 | model->setName("model_transform"); |
---|
91 | |
---|
92 | // enscapsulates OpenGL state |
---|
93 | osg::StateSet* rootStateSet = new osg::StateSet; |
---|
94 | |
---|
95 | // Bedslope geometry |
---|
96 | BedSlope* bedslope = new BedSlope(sww); |
---|
97 | |
---|
98 | // Water geometry |
---|
99 | WaterSurface* water = new WaterSurface(sww); |
---|
100 | |
---|
101 | // Heads Up Display (text overlay) |
---|
102 | HeadsUpDisplay* hud = new HeadsUpDisplay(); |
---|
103 | hud->setTitle("pyVolution SWW Viewer"); |
---|
104 | |
---|
105 | // Lighting |
---|
106 | SpotLight* spotlight = new SpotLight(rootStateSet); |
---|
107 | spotlight->setPosition( osg::Vec3(0,0,2) ); // overhead |
---|
108 | spotlight->setSpotAngle( 45.0 ); |
---|
109 | |
---|
110 | // scenegraph hierarchy |
---|
111 | rootnode->setStateSet(rootStateSet); |
---|
112 | rootnode->addChild( hud->get() ); |
---|
113 | rootnode->addChild( spotlight->get() ); |
---|
114 | rootnode->addChild(model); |
---|
115 | model->addChild( bedslope->get() ); |
---|
116 | model->addChild( water->get() ); |
---|
117 | |
---|
118 | |
---|
119 | // FIXME: should be reasonable default and adjustable |
---|
120 | model->setScale( osg::Vec3(1.0, 1.0, 1.0) ); |
---|
121 | |
---|
122 | // sky |
---|
123 | rootnode->addChild( createSky(10.0, "sky_small.jpg") ); |
---|
124 | |
---|
125 | |
---|
126 | // add model to viewer. |
---|
127 | viewer.setSceneData(rootnode); |
---|
128 | |
---|
129 | // any option left unread are converted into errors to write out later. |
---|
130 | arguments.reportRemainingOptionsAsUnrecognized(); |
---|
131 | |
---|
132 | // report any errors if they have occured when parsing the program aguments. |
---|
133 | if (arguments.errors()) |
---|
134 | { |
---|
135 | arguments.writeErrorMessages(std::cout); |
---|
136 | return 1; |
---|
137 | } |
---|
138 | |
---|
139 | // register additional event handler |
---|
140 | KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps); |
---|
141 | viewer.getEventHandlerList().push_front(event_handler); |
---|
142 | |
---|
143 | // create the windows and run the threads. |
---|
144 | viewer.realize(); |
---|
145 | |
---|
146 | |
---|
147 | // Initial camera position |
---|
148 | viewer.getTrackball()->setNode( rootnode ); |
---|
149 | viewer.getTrackball()->setAutoComputeHomePosition(false); |
---|
150 | viewer.getTrackball()->setHomePosition( |
---|
151 | osg::Vec3d(0,-4,0), // camera location |
---|
152 | osg::Vec3d(0,0,0), // camera target |
---|
153 | osg::Vec3d(0,0,1) ); // camera up vector |
---|
154 | viewer.getTrackball()->moveToHome(); |
---|
155 | |
---|
156 | |
---|
157 | unsigned int timestep = 0; |
---|
158 | while( !viewer.done() ) |
---|
159 | { |
---|
160 | // wait for all cull and draw threads to complete. |
---|
161 | viewer.sync(); |
---|
162 | |
---|
163 | // current time |
---|
164 | double time = viewer.getFrameStamp()->getReferenceTime(); |
---|
165 | |
---|
166 | // advance sww frame? |
---|
167 | event_handler->setTime( time ); |
---|
168 | if( event_handler->timestepChanged() ) |
---|
169 | { |
---|
170 | timestep = event_handler->getTimestep(); |
---|
171 | water->setTimeStep(timestep); |
---|
172 | hud->setTime( sww->getTime(timestep) ); |
---|
173 | } |
---|
174 | |
---|
175 | // events |
---|
176 | if( event_handler->toggleWireframe() ) |
---|
177 | water->toggleWireframe(); |
---|
178 | if( event_handler->toggleCullSetting() ) |
---|
179 | { |
---|
180 | sww->toggleCullSetting(); |
---|
181 | water->setTimeStep(timestep); // forced reload |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | // update the scene by traversing with the update visitor |
---|
186 | viewer.update(); |
---|
187 | |
---|
188 | // fire off the cull and draw traversals of the scene. |
---|
189 | viewer.frame(); |
---|
190 | } |
---|
191 | |
---|
192 | // wait for all cull and draw threads to complete before exit. |
---|
193 | viewer.sync(); |
---|
194 | |
---|
195 | return 0; |
---|
196 | } |
---|