| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [1697] | 19 | #include <osg/Group> |
|---|
| 20 | #include <osg/Notify> |
|---|
| 21 | #include <osg/Depth> |
|---|
| 22 | #include <osg/StateSet> |
|---|
| 23 | #include <osg/ClearNode> |
|---|
| 24 | #include <osg/Transform> |
|---|
| 25 | |
|---|
| 26 | #include <osgUtil/CullVisitor> |
|---|
| 27 | |
|---|
| 28 | #include <osgDB/Registry> |
|---|
| 29 | #include <osgDB/ReadFile> |
|---|
| 30 | |
|---|
| [5851] | 31 | #include <osgViewer/Viewer> |
|---|
| [1697] | 32 | |
|---|
| 33 | #include "GliderManipulator.h" |
|---|
| 34 | |
|---|
| [5851] | 35 | #include <iostream> |
|---|
| 36 | |
|---|
| [1697] | 37 | extern osg::Node *makeTerrain( void ); |
|---|
| 38 | extern osg::Node *makeTrees( void ); |
|---|
| 39 | extern osg::Node *makeTank( void ); |
|---|
| 40 | extern osg::Node *makeWindsocks( void ); |
|---|
| 41 | extern osg::Node *makeGliders( void ); |
|---|
| 42 | extern osg::Node *makeGlider( void ); |
|---|
| 43 | extern osg::Node *makeSky( void ); |
|---|
| 44 | extern osg::Node *makeBase( void ); |
|---|
| 45 | extern osg::Node *makeClouds( void ); |
|---|
| 46 | |
|---|
| [2517] | 47 | class MoveEarthySkyWithEyePointTransform : public osg::Transform |
|---|
| [1697] | 48 | { |
|---|
| [2517] | 49 | public: |
|---|
| [1697] | 50 | |
|---|
| [2517] | 51 | virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const |
|---|
| [1697] | 52 | { |
|---|
| 53 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
|---|
| 54 | if (cv) |
|---|
| 55 | { |
|---|
| 56 | osg::Vec3 eyePointLocal = cv->getEyeLocal(); |
|---|
| [8868] | 57 | matrix.preMultTranslate(osg::Vec3(eyePointLocal.x(),eyePointLocal.y(),0.0f)); |
|---|
| [1697] | 58 | } |
|---|
| 59 | return true; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| [2517] | 63 | virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const |
|---|
| [1697] | 64 | { |
|---|
| [2517] | 65 | std::cout<<"computing transform"<<std::endl; |
|---|
| 66 | |
|---|
| [1697] | 67 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
|---|
| 68 | if (cv) |
|---|
| 69 | { |
|---|
| 70 | osg::Vec3 eyePointLocal = cv->getEyeLocal(); |
|---|
| [8868] | 71 | matrix.postMultTranslate(osg::Vec3(-eyePointLocal.x(),-eyePointLocal.y(),0.0f)); |
|---|
| [1697] | 72 | } |
|---|
| 73 | return true; |
|---|
| 74 | } |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | osg::Group* createModel() |
|---|
| 78 | { |
|---|
| [10070] | 79 | |
|---|
| [1697] | 80 | osg::Group* group = new osg::Group; |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | osg::ClearNode* clearNode = new osg::ClearNode; |
|---|
| 88 | clearNode->setRequiresClear(false); |
|---|
| 89 | |
|---|
| [10070] | 90 | |
|---|
| [2517] | 91 | osg::Transform* transform = new MoveEarthySkyWithEyePointTransform; |
|---|
| [1697] | 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| [10070] | 95 | |
|---|
| 96 | |
|---|
| [1697] | 97 | |
|---|
| 98 | transform->setCullingActive(false); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | transform->addChild(makeSky()); |
|---|
| 102 | transform->addChild(makeBase()); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | clearNode->addChild(transform); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | group->addChild(clearNode); |
|---|
| 109 | |
|---|
| [10070] | 110 | |
|---|
| [1697] | 111 | group->addChild(makeTrees()); |
|---|
| 112 | group->addChild(makeTerrain()); |
|---|
| 113 | group->addChild(makeTank()); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | return group; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | int main( int argc, char **argv ) |
|---|
| 122 | { |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| [1844] | 128 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to create a scene programatically, in this case a hang gliding flying site."); |
|---|
| [1815] | 129 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| [1697] | 130 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| [5851] | 133 | osgViewer::Viewer viewer; |
|---|
| [1697] | 134 | |
|---|
| [10070] | 135 | |
|---|
| [1697] | 136 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 137 | { |
|---|
| 138 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 139 | return 1; |
|---|
| 140 | } |
|---|
| [5901] | 141 | |
|---|
| 142 | bool customWindows = false; |
|---|
| [5908] | 143 | while(arguments.read("-2")) customWindows = true; |
|---|
| [1697] | 144 | |
|---|
| [5901] | 145 | if (customWindows) |
|---|
| [1697] | 146 | { |
|---|
| [5901] | 147 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 148 | if (!wsi) |
|---|
| 149 | { |
|---|
| 150 | osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 151 | return 0; |
|---|
| 152 | } |
|---|
| [1697] | 153 | |
|---|
| [5901] | 154 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| [5908] | 155 | traits->x = 250; |
|---|
| 156 | traits->y = 200; |
|---|
| 157 | traits->width = 800; |
|---|
| 158 | traits->height = 600; |
|---|
| [5901] | 159 | traits->windowDecoration = true; |
|---|
| 160 | traits->doubleBuffer = true; |
|---|
| 161 | traits->sharedContext = 0; |
|---|
| [5863] | 162 | |
|---|
| [5901] | 163 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| [5908] | 164 | if (gc.valid()) |
|---|
| [5901] | 165 | { |
|---|
| [5907] | 166 | |
|---|
| 167 | |
|---|
| [5908] | 168 | gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); |
|---|
| 169 | gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| [5901] | 170 | } |
|---|
| 171 | else |
|---|
| 172 | { |
|---|
| 173 | osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 174 | } |
|---|
| [5863] | 175 | |
|---|
| [5901] | 176 | unsigned int numCameras = 2; |
|---|
| [5909] | 177 | double aspectRatioScale = 1.0; |
|---|
| [5901] | 178 | for(unsigned int i=0; i<numCameras;++i) |
|---|
| 179 | { |
|---|
| 180 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 181 | camera->setGraphicsContext(gc.get()); |
|---|
| [5908] | 182 | camera->setViewport(new osg::Viewport((i* traits->width)/numCameras,(i* traits->height)/numCameras, traits->width/numCameras, traits->height/numCameras)); |
|---|
| [5901] | 183 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 184 | camera->setDrawBuffer(buffer); |
|---|
| 185 | camera->setReadBuffer(buffer); |
|---|
| [5863] | 186 | |
|---|
| [5901] | 187 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0)); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| [5863] | 190 | else |
|---|
| 191 | { |
|---|
| [5901] | 192 | viewer.setUpViewAcrossAllScreens(); |
|---|
| 193 | |
|---|
| [5863] | 194 | } |
|---|
| 195 | |
|---|
| [10070] | 196 | |
|---|
| [5901] | 197 | viewer.setCameraManipulator(new GliderManipulator()); |
|---|
| [5863] | 198 | |
|---|
| [5901] | 199 | |
|---|
| 200 | viewer.setSceneData( createModel() ); |
|---|
| [5863] | 201 | |
|---|
| [5901] | 202 | return viewer.run(); |
|---|
| [1697] | 203 | } |
|---|