Changeset 106 for Swollen/swollen/main.cpp
- Timestamp:
- Jun 14, 2005, 1:28:07 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Swollen/swollen/main.cpp
r105 r106 1 1 /* 2 3 4 5 2 SWWViewer 3 4 An OpenSceneGraph viewer for pyVolution SWW files. 5 copyright (C) 2004-2005 Geoscience Australia 6 6 */ 7 7 … … 12 12 #include <osg/PositionAttitudeTransform> 13 13 #include <osg/StateAttribute> 14 #include <osgDB/FileNameUtils> 14 15 15 16 #include <project.h> … … 24 25 25 26 // prototypes 26 osg::Transform* createSky(float radius, c har*filename);27 osg::Transform* createSky(float radius, const std::string filename); 27 28 extern const char* version(); 28 29 … … 31 32 { 32 33 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 (z is up, default is overhead)"); 48 arguments.getApplicationUsage()->addCommandLineOption("-nosky","Omit background sky"); 49 arguments.getApplicationUsage()->addCommandLineOption("-cullangle <float angle 0-90>","Cull triangles steeper than this value"); 50 arguments.getApplicationUsage()->addCommandLineOption("-texture <file>","Image to use for bedslope topography"); 51 arguments.getApplicationUsage()->addCommandLineOption("-version","Revision number and creation (not compile) date"); 52 53 // construct the viewer. 54 CustomViewer viewer(arguments); 55 56 // set up with sensible default event handlers 57 viewer.setUpViewer( osgProducer::Viewer::STANDARD_SETTINGS ); 58 viewer.setClearColor( osg::Vec4(DEF_BACKGROUND_COLOUR) ); 59 //viewer.getCamera(0)->getRenderSurface()->setWindowRectangle(200,300,400,300); 60 viewer.getCullSettings().setComputeNearFarMode( osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES ); 61 62 63 // get details on keyboard and mouse bindings used by the viewer 64 viewer.getUsage(*arguments.getApplicationUsage()); 65 66 // if user requested help, write it out to cout 67 if( arguments.read("-help") ) 68 { 69 arguments.getApplicationUsage()->write(std::cout); 70 return 1; 71 } 72 73 // same for version info 74 if( arguments.read("-version") ) 75 { 76 std::cout << version() << std::endl; 77 return 1; 78 } 79 80 81 // load sww file specified as final argument on command line (static bedslope 82 // geometry only, per timestep height field geometry is done in loop below) 83 int lastarg = arguments.argc()-1; 84 std::string swwfile = arguments.argv()[lastarg]; 85 arguments.remove(lastarg); 86 if( swwfile.substr(swwfile.size()-4,4).find(".sww",0) == -1 ) // ensure filename ends in .sww 87 { 88 std::cout << "Require last argument be an .sww file ... quitting" << std::endl; 89 return 1; 90 } 91 SWWReader *sww = new SWWReader(swwfile); 92 if (sww->isValid() == false) 93 { 94 std::cout << "Unable to load " << swwfile << " ... is this really an .sww file?" << std::endl; 95 return 1; 96 } 97 98 99 // default arguments and command line parameters 100 float tmpfloat, tps, vscale; 101 if( !arguments.read("-tps", tps) || tps <= 0.0 ) tps = DEF_TPS; 102 if( !arguments.read("-scale", vscale) ) vscale = 1.0; 103 if( arguments.read("-hmin",tmpfloat) ) sww->setHeightMin( tmpfloat ); 104 if( arguments.read("-hmax",tmpfloat) ) sww->setHeightMax( tmpfloat ); 105 if( arguments.read("-alphamin",tmpfloat) ) sww->setAlphaMin( tmpfloat ); 106 if( arguments.read("-alphamax",tmpfloat) ) sww->setAlphaMax( tmpfloat ); 107 if( arguments.read("-cullangle",tmpfloat) ) sww->setCullAngle( tmpfloat ); 108 109 std::string bedslopetexture; 110 if( arguments.read("-texture",bedslopetexture) ) sww->setBedslopeTexture( bedslopetexture ); 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(1,1,1) ); // z is up 136 137 std::string lightposstr; 138 while (arguments.read("-lightpos",lightposstr)) 139 { 140 float x, y, z; 141 int count = sscanf( lightposstr.c_str(), "%f,%f,%f", &x, &y, &z ); 142 if( count == 3 ) light->setPosition( osg::Vec3(x,y,z) ); // z is up 143 else osg::notify(osg::WARN) << "Invalid bedslope light position \"" << lightposstr << "\"" << std::endl; 144 } 145 146 147 148 // scenegraph hierarchy 149 rootnode->setStateSet(rootStateSet); 150 rootnode->addChild( hud->get() ); 151 rootnode->addChild( light->get() ); 152 rootnode->addChild(model); 153 model->addChild( bedslope->get() ); 154 model->addChild( water->get() ); 155 156 157 // allow vertical scaling from command line parameter 158 model->setScale( osg::Vec3(1.0, 1.0, vscale) ); 159 160 // surrounding sky sphere 161 if( !arguments.read("-nosky") ) 162 rootnode->addChild( createSky(10.0, "sky_small.jpg") ); 163 164 165 // add model to viewer. 166 viewer.setSceneData(rootnode); 34 // use an ArgumentParser object to manage the program arguments 35 osg::ArgumentParser arguments( &argc, argv ); 36 37 // set up the usage document 38 std::string appname = arguments.getApplicationName(); 39 arguments.getApplicationUsage()->setDescription( appname ); 40 arguments.getApplicationUsage()->setCommandLineUsage("swollen [options] swwfile ..."); 41 arguments.getApplicationUsage()->addCommandLineOption("-help","Display this information"); 42 arguments.getApplicationUsage()->addCommandLineOption("-scale <float>","Vertical scale factor"); 43 arguments.getApplicationUsage()->addCommandLineOption("-tps <rate>","Timesteps per second"); 44 arguments.getApplicationUsage()->addCommandLineOption("-hmin <float>","Height below which transparency is set to zero"); 45 arguments.getApplicationUsage()->addCommandLineOption("-hmax <float>","Height above which transparency is set to alphamax"); 46 arguments.getApplicationUsage()->addCommandLineOption("-alphamin <float 0-1>","Transparency value at hmin"); 47 arguments.getApplicationUsage()->addCommandLineOption("-alphamax <float 0-1>","Maximum transparency clamp value"); 48 arguments.getApplicationUsage()->addCommandLineOption("-lightpos <float>,<float>,<float>","x,y,z of bedslope directional light (z is up, default is overhead)"); 49 arguments.getApplicationUsage()->addCommandLineOption("-nosky","Omit background sky"); 50 arguments.getApplicationUsage()->addCommandLineOption("-cullangle <float angle 0-90>","Cull triangles steeper than this value"); 51 arguments.getApplicationUsage()->addCommandLineOption("-texture <file>","Image to use for bedslope topography"); 52 arguments.getApplicationUsage()->addCommandLineOption("-version","Revision number and creation (not compile) date"); 53 54 // construct the viewer. 55 CustomViewer viewer(arguments); 56 57 // set up with sensible default event handlers 58 viewer.setUpViewer( osgProducer::Viewer::STANDARD_SETTINGS ); 59 viewer.setClearColor( osg::Vec4(DEF_BACKGROUND_COLOUR) ); 60 //viewer.getCamera(0)->getRenderSurface()->setWindowRectangle(200,300,400,300); 61 viewer.getCullSettings().setComputeNearFarMode( osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES ); 62 63 64 // get details on keyboard and mouse bindings used by the viewer 65 viewer.getUsage(*arguments.getApplicationUsage()); 66 67 // if user requested help, write it out to cout 68 if( arguments.read("-help") ) 69 { 70 arguments.getApplicationUsage()->write(std::cout); 71 return 1; 72 } 73 74 // same for version info 75 if( arguments.read("-version") ) 76 { 77 std::cout << version() << std::endl; 78 return 1; 79 } 80 81 82 // load sww file specified as final argument on command line (static bedslope 83 // geometry only, per timestep height field geometry is done in loop below) 84 int lastarg = arguments.argc()-1; 85 std::string swwfile = arguments.argv()[lastarg]; 86 arguments.remove(lastarg); 87 if( swwfile.substr(swwfile.size()-4,4).find(".sww",0) == -1 ) // ensure filename ends in .sww 88 { 89 std::cout << "Require last argument be an .sww file ... quitting" << std::endl; 90 return 1; 91 } 92 SWWReader *sww = new SWWReader(swwfile); 93 if (sww->isValid() == false) 94 { 95 std::cout << "Unable to load " << swwfile << " ... is this really an .sww file?" << std::endl; 96 return 1; 97 } 98 99 100 // relative directory to swollen binary 101 if( osgDB::getFilePath(argv[0]) == "" ) 102 sww->setSwollenDir( std::string(".") ); 103 else 104 sww->setSwollenDir( osgDB::getFilePath(argv[0]) ); 105 106 107 // default arguments and command line parameters 108 float tmpfloat, tps, vscale; 109 if( !arguments.read("-tps", tps) || tps <= 0.0 ) tps = DEF_TPS; 110 if( !arguments.read("-scale", vscale) ) vscale = 1.0; 111 if( arguments.read("-hmin",tmpfloat) ) sww->setHeightMin( tmpfloat ); 112 if( arguments.read("-hmax",tmpfloat) ) sww->setHeightMax( tmpfloat ); 113 if( arguments.read("-alphamin",tmpfloat) ) sww->setAlphaMin( tmpfloat ); 114 if( arguments.read("-alphamax",tmpfloat) ) sww->setAlphaMax( tmpfloat ); 115 if( arguments.read("-cullangle",tmpfloat) ) sww->setCullAngle( tmpfloat ); 116 117 std::string bedslopetexture; 118 if( arguments.read("-texture",bedslopetexture) ) sww->setBedslopeTexture( bedslopetexture ); 119 120 // root node 121 osg::Group* rootnode = new osg::Group; 122 123 // transform 124 osg::PositionAttitudeTransform* model = new osg::PositionAttitudeTransform; 125 model->setName("position_attitude_transform"); 126 127 // enscapsulates OpenGL state 128 osg::StateSet* rootStateSet = new osg::StateSet; 129 130 // Bedslope geometry 131 BedSlope* bedslope = new BedSlope(sww); 132 133 // Water geometry 134 WaterSurface* water = new WaterSurface(sww); 135 136 // Heads Up Display (text overlay) 137 HeadsUpDisplay* hud = new HeadsUpDisplay(); 138 hud->setTitle("pyVolution SWW Viewer"); 139 140 141 // Lighting 142 DirectionalLight* light = new DirectionalLight(rootStateSet); 143 light->setPosition( osg::Vec3(1,1,1) ); // z is up 144 145 std::string lightposstr; 146 while (arguments.read("-lightpos",lightposstr)) 147 { 148 float x, y, z; 149 int count = sscanf( lightposstr.c_str(), "%f,%f,%f", &x, &y, &z ); 150 if( count == 3 ) light->setPosition( osg::Vec3(x,y,z) ); // z is up 151 else osg::notify(osg::WARN) << "Invalid bedslope light position \"" << lightposstr << "\"" << std::endl; 152 } 153 154 155 156 // scenegraph hierarchy 157 rootnode->setStateSet(rootStateSet); 158 rootnode->addChild( hud->get() ); 159 rootnode->addChild( light->get() ); 160 rootnode->addChild(model); 161 model->addChild( bedslope->get() ); 162 model->addChild( water->get() ); 163 164 165 // allow vertical scaling from command line parameter 166 model->setScale( osg::Vec3(1.0, 1.0, vscale) ); 167 168 // surrounding sky sphere 169 if( !arguments.read("-nosky") ) 170 rootnode->addChild( createSky(10.0, sww->getSwollenDir() + std::string("/sky_small.jpg") )); 171 172 173 // add model to viewer. 174 viewer.setSceneData(rootnode); 167 175 168 169 176 // any option left unread are converted into errors to write out later. 177 arguments.reportRemainingOptionsAsUnrecognized(); 170 178 171 172 173 174 175 176 177 178 179 180 179 // report any errors if they have occured when parsing the program aguments. 180 if (arguments.errors()) 181 { 182 arguments.writeErrorMessages(std::cout); 183 return 1; 184 } 185 186 // register additional event handler 187 KeyboardEventHandler* event_handler = new KeyboardEventHandler(sww->getNumberOfTimesteps(), tps); 188 viewer.getEventHandlerList().push_front(event_handler); 181 189 182 190 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 191 // create the windows and run the threads. 192 viewer.realize(); 193 194 195 // initial camera position 196 CustomTerrainManipulator* terrainmanipulator = viewer.getTerrainManipulator(); 197 terrainmanipulator->setNode( model ); 198 terrainmanipulator->setAutoComputeHomePosition( false ); 199 terrainmanipulator->setHomePosition( 200 osg::Vec3d(0,-3,3), // camera location 201 osg::Vec3d(0,0,0), // camera target 202 osg::Vec3d(0,1,1) ); // camera up vector 203 terrainmanipulator->moveToHome(); 204 terrainmanipulator->enable(); 205 206 207 unsigned int timestep = 0; 208 while( !viewer.done() ) 209 { 202 210 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 211 // wait for all cull and draw threads to complete. 212 viewer.sync(); 213 214 215 // current time 216 double time = viewer.getFrameStamp()->getReferenceTime(); 217 218 // advance sww frame? 219 event_handler->setTime( time ); 220 if( event_handler->timestepChanged() ) 221 { 222 timestep = event_handler->getTimestep(); 223 water->setTimeStep(timestep); 224 hud->setTime( sww->getTime(timestep) ); 225 } 226 227 // events 228 if( event_handler->toggleWireframe() ) 229 water->toggleWireframe(); 230 231 if( event_handler->toggleCulling() ) 232 { 233 sww->toggleCulling(); 234 water->setTimeStep(timestep); // refresh 235 } 236 237 // update the scene by traversing with the update visitor 238 viewer.update(); 231 239 232 233 234 240 // fire off the cull and draw traversals of the scene. 241 viewer.frame(); 242 } 235 243 236 237 238 239 244 // wait for all cull and draw threads to complete before exit. 245 viewer.sync(); 246 247 return 0; 240 248 }
Note: See TracChangeset
for help on using the changeset viewer.