Changeset 2352 for OpenSceneGraph/trunk/examples/osgshaders/osgshaders.cpp
- Timestamp:
- 10/05/03 13:30:54 (10 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgshaders/osgshaders.cpp
r2027 r2352 12 12 13 13 /* file: examples/osgshaders/osgshaders.cpp 14 * author: Mike Weiblen 2003-0 7-1414 * author: Mike Weiblen 2003-09-18 15 15 * 16 * A simple app skeleton for viewing OpenGL Shading Language shaders; 17 * derived from osgviewer.cpp from OSG 0.9.4-2 16 * A demo of the OpenGL Shading Language shaders using osgGL2. 18 17 * 19 18 * See http://www.3dlabs.com/opengl2/ for more information regarding … … 21 20 */ 22 21 22 #include <osg/Notify> 23 #include <osgGA/GUIEventAdapter> 24 #include <osgGA/GUIActionAdapter> 23 25 #include <osgDB/ReadFile> 24 26 #include <osgUtil/Optimizer> 25 27 #include <osgProducer/Viewer> 28 #include <osgGL2/Version> 26 29 27 #define GL2SCENE 28 osg::Node* GL2Scene(); 29 void GL2Update(); 30 #include "GL2Scene.h" 31 32 using namespace osg; 33 34 /////////////////////////////////////////////////////////////////////////// 35 36 class KeyHandler: public osgGA::GUIEventHandler 37 { 38 public: 39 KeyHandler( GL2ScenePtr gl2Scene ) : 40 _gl2Scene(gl2Scene) 41 {} 42 43 bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& ) 44 { 45 if( ea.getEventType() != osgGA::GUIEventAdapter::KEYDOWN ) 46 return false; 47 48 switch( ea.getKey() ) 49 { 50 case 'x': 51 _gl2Scene->reloadShaderSource(); 52 return true; 53 case 'y': 54 _gl2Scene->toggleShaderEnable(); 55 return true; 56 } 57 return false; 58 } 59 60 private: 61 GL2ScenePtr _gl2Scene; 62 }; 63 64 /////////////////////////////////////////////////////////////////////////// 30 65 31 66 int main( int argc, char **argv ) 32 67 { 68 // use an ArgumentParser object to manage the program arguments. 69 ArgumentParser args(&argc,argv); 70 71 // set up the usage document 72 args.getApplicationUsage()->setApplicationName(args.getApplicationName()); 73 args.getApplicationUsage()->setDescription(args.getApplicationName() + 74 " demonstrates the OpenGL Shading Language using osgGL2"); 75 args.getApplicationUsage()->setCommandLineUsage(args.getApplicationName()); 76 args.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); 33 77 34 // use an ArgumentParser object to manage the program arguments. 35 osg::ArgumentParser arguments(&argc,argv); 36 37 // set up the usage document, in case we need to print out how to use this program. 38 arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); 39 arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); 40 arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); 41 arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); 42 78 args.getApplicationUsage()->addKeyboardMouseBinding( "x", "Reload and recompile shader source files." ); 79 args.getApplicationUsage()->addKeyboardMouseBinding( "y", "Toggle shader enable" ); 43 80 44 81 // construct the viewer. 45 osgProducer::Viewer viewer(arguments); 82 osgProducer::Viewer viewer(args); 83 viewer.setUpViewer( osgProducer::Viewer::STANDARD_SETTINGS ); 84 viewer.getUsage( *args.getApplicationUsage() ); 46 85 47 // set up the value with sensible default event handlers. 48 viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); 49 50 // get details on keyboard and mouse bindings used by the viewer. 51 viewer.getUsage(*arguments.getApplicationUsage()); 52 53 // if user request help write it out to cout. 54 if (arguments.read("-h") || arguments.read("--help")) 86 if( args.read("-h") || args.read("--help") ) 55 87 { 56 arg uments.getApplicationUsage()->write(std::cout);88 args.getApplicationUsage()->write(std::cout); 57 89 return 1; 58 90 } 59 91 60 92 // any option left unread are converted into errors to write out later. 61 arguments.reportRemainingOptionsAsUnrecognized(); 62 63 // report any errors if they have occured when parsing the program aguments. 64 if (arguments.errors()) 93 args.reportRemainingOptionsAsUnrecognized(); 94 if( args.errors() ) 65 95 { 66 arguments.writeErrorMessages(std::cout); 67 return 1; 68 } 69 70 #ifdef GL2SCENE //( 71 osg::ref_ptr<osg::Node> loadedModel = GL2Scene(); 72 #else //)( 73 if (arguments.argc()<=1) 74 { 75 arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); 96 args.writeErrorMessages(std::cout); 76 97 return 1; 77 98 } 78 99 79 // read the scene from the list of file specified commandline args. 80 osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); 100 // create the scene 101 notify(NOTICE) << "osgGL2 version " << osgGL2GetVersion() << std::endl; 102 GL2ScenePtr gl2Scene = new GL2Scene; 81 103 82 // if no model has been successfully loaded report failure. 83 if (!loadedModel) 84 { 85 std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; 86 return 1; 87 } 88 89 90 // optimize the scene graph, remove rendundent nodes and state etc. 91 osgUtil::Optimizer optimizer; 92 optimizer.optimize(loadedModel.get()); 93 #endif //) 94 95 // set the scene to render 96 viewer.setSceneData(loadedModel.get()); 97 98 // create the windows and run the threads. 104 viewer.setSceneData( gl2Scene->getRootNode().get() ); 105 viewer.getEventHandlerList().push_front( new KeyHandler(gl2Scene) ); 99 106 viewer.realize(); 100 101 107 while( !viewer.done() ) 102 108 { 103 // wait for all cull and draw threads to complete.104 109 viewer.sync(); 105 106 // update the scene by traversing it with the the update visitor which will107 // call all node update callbacks and animations.108 110 viewer.update(); 109 #ifdef GL2SCENE //(110 GL2Update();111 #endif //)112 113 // fire off the cull and draw traversals of the scene.114 111 viewer.frame(); 115 116 112 } 117 118 // wait for all cull and draw threads to complete before exit.119 113 viewer.sync(); 120 121 114 return 0; 122 115 } 123 116 117 /*EOF*/ 118
