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