Show
Ignore:
Timestamp:
10/05/03 13:30:54 (11 years ago)
Author:
robert
Message:

From Mike Weiblen, updates to osgGL2 to support Uniform values

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgshaders/osgshaders.cpp

    r2027 r2352  
    1212 
    1313/* file:        examples/osgshaders/osgshaders.cpp 
    14  * author:      Mike Weiblen 2003-07-14 
     14 * author:      Mike Weiblen 2003-09-18 
    1515 * 
    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. 
    1817 * 
    1918 * See http://www.3dlabs.com/opengl2/ for more information regarding 
     
    2120*/ 
    2221 
     22#include <osg/Notify> 
     23#include <osgGA/GUIEventAdapter> 
     24#include <osgGA/GUIActionAdapter> 
    2325#include <osgDB/ReadFile> 
    2426#include <osgUtil/Optimizer> 
    2527#include <osgProducer/Viewer> 
     28#include <osgGL2/Version> 
    2629 
    27 #define GL2SCENE 
    28 osg::Node* GL2Scene(); 
    29 void GL2Update(); 
     30#include "GL2Scene.h" 
     31 
     32using namespace osg; 
     33 
     34/////////////////////////////////////////////////////////////////////////// 
     35 
     36class 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/////////////////////////////////////////////////////////////////////////// 
    3065 
    3166int main( int argc, char **argv ) 
    3267{ 
     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"); 
    3377 
    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" ); 
    4380 
    4481    // 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() ); 
    4685 
    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") ) 
    5587    { 
    56         arguments.getApplicationUsage()->write(std::cout); 
     88        args.getApplicationUsage()->write(std::cout); 
    5789        return 1; 
    5890    } 
    5991 
    6092    // 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() ) 
    6595    { 
    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); 
    7697        return 1; 
    7798    } 
    7899 
    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; 
    81103 
    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) ); 
    99106    viewer.realize(); 
    100  
    101107    while( !viewer.done() ) 
    102108    { 
    103         // wait for all cull and draw threads to complete. 
    104109        viewer.sync(); 
    105  
    106         // update the scene by traversing it with the the update visitor which will 
    107         // call all node update callbacks and animations. 
    108110        viewer.update(); 
    109 #ifdef GL2SCENE //( 
    110         GL2Update(); 
    111 #endif //) 
    112           
    113         // fire off the cull and draw traversals of the scene. 
    114111        viewer.frame(); 
    115          
    116112    } 
    117      
    118     // wait for all cull and draw threads to complete before exit. 
    119113    viewer.sync(); 
    120  
    121114    return 0; 
    122115} 
    123116 
     117/*EOF*/ 
     118