root/OpenSceneGraph/trunk/examples/osgshadergen/osgshadergen.cpp @ 13295

Revision 12292, 5.7 kB (checked in by robert, 4 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified   
4 * freely and without restriction, both in commercial and non commercial applications,
5 * as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*/
11
12#include <osgDB/ReadFile>
13#include <osgUtil/ShaderGen>
14
15#include <osgViewer/Viewer>
16#include <osgViewer/ViewerEventHandlers>
17
18#include <osgGA/TrackballManipulator>
19#include <osgGA/FlightManipulator>
20#include <osgGA/DriveManipulator>
21#include <osgGA/KeySwitchMatrixManipulator>
22#include <osgGA/StateSetManipulator>
23#include <osgGA/AnimationPathManipulator>
24#include <osgGA/TerrainManipulator>
25
26#include <iostream>
27
28
29class ShaderGenReadFileCallback : public osgDB::Registry::ReadFileCallback
30{
31public:
32    ShaderGenReadFileCallback()
33    {
34    }
35
36    virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& filename, const osgDB::ReaderWriter::Options* options)
37    {
38        osgDB::ReaderWriter::ReadResult result = osgDB::Registry::ReadFileCallback::readNode(filename, options);
39        if (osg::Node *node = result.getNode())
40        {
41            _visitor.reset();
42            node->accept(_visitor);
43        }
44        return result;
45    }
46
47    void setRootStateSet(osg::StateSet *stateSet) { _visitor.setRootStateSet(stateSet); }
48    osg::StateSet *getRootStateSet() const { return _visitor.getRootStateSet(); }
49
50protected:
51    osgUtil::ShaderGenVisitor _visitor;
52};
53
54
55int main(int argc, char** argv)
56{
57    // use an ArgumentParser object to manage the program arguments.
58    osg::ArgumentParser arguments(&argc,argv);
59
60    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
61    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+
62            " is an example of conversion of fixed function pipeline to GLSL");
63    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
64
65    osgViewer::Viewer viewer(arguments);
66
67    unsigned int helpType = 0;
68    if ((helpType = arguments.readHelpType()))
69    {
70        arguments.getApplicationUsage()->write(std::cout, helpType);
71        return 1;
72    }
73   
74    // report any errors if they have occurred when parsing the program arguments.
75    if (arguments.errors())
76    {
77        arguments.writeErrorMessages(std::cout);
78        return 1;
79    }
80   
81    if (arguments.argc()<=1)
82    {
83        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
84        return 1;
85    }
86
87    // set up the camera manipulators.
88    {
89        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
90
91        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
92        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
93        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
94        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
95
96        std::string pathfile;
97        char keyForAnimationPath = '5';
98        while (arguments.read("-p",pathfile))
99        {
100            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
101            if (apm || !apm->valid())
102            {
103                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
104                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
105                keyswitchManipulator->selectMatrixManipulator(num);
106                ++keyForAnimationPath;
107            }
108        }
109
110        viewer.setCameraManipulator( keyswitchManipulator.get() );
111    }
112
113    // add the state manipulator
114    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
115   
116    // add the thread model handler
117    viewer.addEventHandler(new osgViewer::ThreadingHandler);
118
119    // add the window size toggle handler
120    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
121       
122    // add the stats handler
123    viewer.addEventHandler(new osgViewer::StatsHandler);
124
125    // add the help handler
126    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
127
128    // add the record camera path handler
129    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
130
131    // add the LOD Scale handler
132    viewer.addEventHandler(new osgViewer::LODScaleHandler);
133
134    // add the screen capture handler
135    viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
136   
137    // Register shader generator callback
138    ShaderGenReadFileCallback *readFileCallback = new ShaderGenReadFileCallback;
139    // All read nodes will inherit root state set.
140    readFileCallback->setRootStateSet(viewer.getCamera()->getStateSet());
141    osgDB::Registry::instance()->setReadFileCallback(readFileCallback);
142
143    // load the data
144    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
145    if (!loadedModel)
146    {
147        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
148        return 1;
149    }
150
151    // any option left unread are converted into errors to write out later.
152    arguments.reportRemainingOptionsAsUnrecognized();
153
154    // report any errors if they have occurred when parsing the program arguments.
155    if (arguments.errors())
156    {
157        arguments.writeErrorMessages(std::cout);
158        return 1;
159    }
160
161    viewer.setSceneData( loadedModel.get() );
162
163    viewer.realize();
164
165    return viewer.run();
166
167}
Note: See TracBrowser for help on using the browser.