source: Swollen/swollen/main.cpp @ 100

Last change on this file since 100 was 100, checked in by darran, 19 years ago
  • added -lightpos x,y,z command line parameter
  • colour still looks "blown out" ... investigate this
File size: 8.0 KB
Line 
1/*
2    SWWViewer
3
4    An OpenSceneGraph viewer for pyVolution SWW files.
5    copyright (C) 2004-2005 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 <hud.h>
19#include <keyboardeventhandler.h>
20#include <directionallight.h>
21#include <watersurface.h>
22#include <customviewer.h>
23
24
25// prototypes
26osg::Transform* createSky(float radius, char* filename);
27extern const char* version();
28
29
30int 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("-lightpos <float>,<float>,<float>","x,y,z of bedslope directional light (default is overhead)");
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,300,400,300);
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    // 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    // root node
111    osg::Group* rootnode = new osg::Group;
112
113    // transform
114    osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform;
115    model->setName("position_attitude_transform");
116
117    // enscapsulates OpenGL state
118    osg::StateSet* rootStateSet = new osg::StateSet;
119
120    // Bedslope geometry
121    BedSlope* bedslope = new BedSlope(sww);
122
123    // Water geometry
124    WaterSurface* water = new WaterSurface(sww);
125
126    // Heads Up Display (text overlay)
127    HeadsUpDisplay* hud = new HeadsUpDisplay();
128    hud->setTitle("pyVolution SWW Viewer");
129
130
131    // Lighting
132    DirectionalLight* light = new DirectionalLight(rootStateSet);
133    light->setPosition( osg::Vec3(0,0,2) );  // z is up
134
135    std::string lightposstr;
136    while (arguments.read("-lightpos",lightposstr))
137    {
138        float x, y, z;
139        int count = sscanf( lightposstr.c_str(), "%f,%f,%f", &x, &y, &z );
140        if( count == 3 ) light->setPosition( osg::Vec3(x,y,z) );  // z is up
141        else osg::notify(osg::WARN) << "Invalid bedslope light position \"" << lightposstr << "\"" << std::endl;
142    }
143
144
145
146    // scenegraph hierarchy
147    rootnode->setStateSet(rootStateSet);
148    rootnode->addChild( hud->get() );
149    rootnode->addChild( light->get() );
150    rootnode->addChild(model);
151    model->addChild( bedslope->get() );
152    model->addChild( water->get() );
153
154
155    // allow vertical scaling from command line parameter
156    model->setScale( osg::Vec3(1.0, 1.0, vscale) );
157
158    // surrounding sky sphere
159    if( !arguments.read("-nosky") )
160      rootnode->addChild( createSky(10.0, "sky_small.jpg") ); 
161
162
163    // add model to viewer.
164    viewer.setSceneData(rootnode);
165 
166    // any option left unread are converted into errors to write out later.
167    arguments.reportRemainingOptionsAsUnrecognized();
168 
169    // report any errors if they have occured when parsing the program aguments.
170    if (arguments.errors())
171    {
172        arguments.writeErrorMessages(std::cout);
173        return 1;
174    }
175
176    // register additional event handler
177    KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps);
178    viewer.getEventHandlerList().push_front(event_handler);
179
180 
181    // create the windows and run the threads.
182    viewer.realize();
183
184
185    // initial camera position
186    CustomTerrainManipulator* terrainmanipulator = viewer.getTerrainManipulator();
187    terrainmanipulator->setNode( model );
188    terrainmanipulator->setAutoComputeHomePosition( false );
189    terrainmanipulator->setHomePosition(
190        osg::Vec3d(0,-3,3),    // camera location
191        osg::Vec3d(0,0,0),     // camera target
192        osg::Vec3d(0,1,1) );   // camera up vector
193    terrainmanipulator->moveToHome();
194    terrainmanipulator->enable();
195
196
197    unsigned int timestep = 0;
198    while( !viewer.done() )
199    {
200   
201        // wait for all cull and draw threads to complete.
202        viewer.sync();
203
204        // current time
205        double time = viewer.getFrameStamp()->getReferenceTime();
206
207        // advance sww frame?
208        event_handler->setTime( time );
209        if( event_handler->timestepChanged() )
210        {
211            timestep = event_handler->getTimestep();
212            water->setTimeStep(timestep);
213            hud->setTime( sww->getTime(timestep) );
214        }
215
216        // events
217        if( event_handler->toggleWireframe() )
218            water->toggleWireframe();
219
220        // update the scene by traversing with the update visitor
221        viewer.update();
222         
223        // fire off the cull and draw traversals of the scene.
224        viewer.frame();
225    }
226   
227    // wait for all cull and draw threads to complete before exit.
228    viewer.sync();
229
230    return 0;
231}
Note: See TracBrowser for help on using the repository browser.