| [9940] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| [11931] | 4 | |
|---|
| [9940] | 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 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 | |
|---|
| 29 | class ShaderGenReadFileCallback : public osgDB::Registry::ReadFileCallback |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 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 | |
|---|
| 50 | protected: |
|---|
| 51 | osgUtil::ShaderGenVisitor _visitor; |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | int main(int argc, char** argv) |
|---|
| 56 | { |
|---|
| 57 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 114 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | viewer.addEventHandler(new osgViewer::LODScaleHandler); |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | viewer.addEventHandler(new osgViewer::ScreenCaptureHandler); |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | ShaderGenReadFileCallback *readFileCallback = new ShaderGenReadFileCallback; |
|---|
| 139 | |
|---|
| 140 | readFileCallback->setRootStateSet(viewer.getCamera()->getStateSet()); |
|---|
| 141 | osgDB::Registry::instance()->setReadFileCallback(readFileCallback); |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 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 | |
|---|
| 152 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 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 | } |
|---|