root/OpenSceneGraph/trunk/examples/osgshaders/osgshaders.cpp @ 3984

Revision 3984, 3.3 kB (checked in by robert, 9 years ago)

From Mike Weiblen, ported osgshaders example from osgGL2 to core osg based.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
2 * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
3 *
4 * This application is open source and may be redistributed and/or modified   
5 * freely and without restriction, both in commericial and non commericial applications,
6 * as long as this copyright notice is maintained.
7 *
8 * This application is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*/
12
13/* file:        examples/osgglsl/osgshaders.cpp
14 * author:      Mike Weiblen 2005-04-05
15 *
16 * A demo of the OpenGL Shading Language shaders using core OSG.
17 *
18 * See http://www.3dlabs.com/opengl2/ for more information regarding
19 * the OpenGL Shading Language.
20*/
21
22#include <osg/Notify>
23#include <osgGA/GUIEventAdapter>
24#include <osgGA/GUIActionAdapter>
25#include <osgDB/ReadFile>
26#include <osgUtil/Optimizer>
27#include <osgProducer/Viewer>
28
29#include "GL2Scene.h"
30
31using namespace osg;
32
33///////////////////////////////////////////////////////////////////////////
34
35class KeyHandler: public osgGA::GUIEventHandler
36{
37    public:
38        KeyHandler( GL2ScenePtr gl2Scene ) :
39                _gl2Scene(gl2Scene)
40        {}
41
42        bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& )
43        {
44            if( ea.getEventType() != osgGA::GUIEventAdapter::KEYDOWN )
45                return false;
46
47            switch( ea.getKey() )
48            {
49                case 'x':
50                    _gl2Scene->reloadShaderSource();
51                    return true;
52                case 'y':
53                    _gl2Scene->toggleShaderEnable();
54                    return true;
55            }
56            return false;
57        }
58
59    private:
60        GL2ScenePtr _gl2Scene;
61};
62
63///////////////////////////////////////////////////////////////////////////
64
65int main( int argc, char **argv )
66{
67    // use an ArgumentParser object to manage the program arguments.
68    osg::ArgumentParser args(&argc,argv);
69   
70    // set up the usage document
71    args.getApplicationUsage()->setApplicationName(args.getApplicationName());
72    args.getApplicationUsage()->setDescription(args.getApplicationName() +
73            " demonstrates the OpenGL Shading Language using core OSG");
74    args.getApplicationUsage()->setCommandLineUsage(args.getApplicationName());
75    args.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
76
77    args.getApplicationUsage()->addKeyboardMouseBinding( "x", "Reload and recompile shader source files." );
78    args.getApplicationUsage()->addKeyboardMouseBinding( "y", "Toggle shader enable" );
79
80    // construct the viewer.
81    osgProducer::Viewer viewer(args);
82    viewer.setUpViewer( osgProducer::Viewer::STANDARD_SETTINGS );
83    viewer.getUsage( *args.getApplicationUsage() );
84
85    if( args.read("-h") || args.read("--help") )
86    {
87        args.getApplicationUsage()->write(std::cout);
88        return 1;
89    }
90
91    // any option left unread are converted into errors to write out later.
92    args.reportRemainingOptionsAsUnrecognized();
93    if( args.errors() )
94    {
95        args.writeErrorMessages(std::cout);
96        return 1;
97    }
98
99    // create the scene
100    GL2ScenePtr gl2Scene = new GL2Scene;
101
102    viewer.setSceneData( gl2Scene->getRootNode().get() );
103    viewer.getEventHandlerList().push_front( new KeyHandler(gl2Scene) );
104    viewer.realize();
105    while( !viewer.done() )
106    {
107        viewer.sync();
108        viewer.update();
109        viewer.frame();
110    }
111    viewer.sync();
112    return 0;
113}
114
115/*EOF*/
Note: See TracBrowser for help on using the browser.