root/OpenSceneGraph/trunk/examples/osghangglide/osghangglide.cpp @ 5890

Revision 5890, 7.2 kB (checked in by robert, 7 years ago)

Removed the setting of thread safe ref/unref and max number of graphics contexts
as this is now done automatically by osgViewer::Viewer

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
19extern osg::Node *makeTerrain( void );
20extern osg::Node *makeTrees( void );
21extern osg::Node *makeTank( void );
22extern osg::Node *makeWindsocks( void );
23extern osg::Node *makeGliders( void );
24extern osg::Node *makeGlider( void );
25extern osg::Node *makeSky( void );
26extern osg::Node *makeBase( void );
27extern osg::Node *makeClouds( void );
28
29class MoveEarthySkyWithEyePointTransform : public osg::Transform
30{
31public:
32    /** Get the transformation matrix which moves from local coords to world coords.*/
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    /** Get the transformation matrix which moves from world coords to local coords.*/
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
59osg::Group* createModel()
60{
61    // no database loaded so automatically create Ed Levin Park..
62    osg::Group* group = new osg::Group;
63
64    // the base and sky subgraphs go to set the earth sky of the
65    // model and clear the color and depth buffer for us, by using
66    // osg::Depth, and setting their bin numbers to less than 0,
67    // to force them to draw before the rest of the scene.
68
69    osg::ClearNode* clearNode = new osg::ClearNode;
70    clearNode->setRequiresClear(false); // we've got base and sky to do it.
71
72    // use a transform to make the sky and base around with the eye point.
73    osg::Transform* transform = new MoveEarthySkyWithEyePointTransform;
74
75    // transform's value isn't knowm until in the cull traversal so its bounding
76    // volume is can't be determined, therefore culling will be invalid,
77    // so switch it off, this cause all our paresnts to switch culling
78    // off as well. But don't worry culling will be back on once underneath
79    // this node or any other branch above this transform.
80    transform->setCullingActive(false);
81
82    // add the sky and base layer.
83    transform->addChild(makeSky());  // bin number -2 so drawn first.
84    transform->addChild(makeBase()); // bin number -1 so draw second.     
85
86    // add the transform to the earth sky.
87    clearNode->addChild(transform);
88
89    // add to earth sky to the scene.
90    group->addChild(clearNode);
91
92    // the rest of the scene drawn after the base and sky above.
93    group->addChild(makeTrees()); // will drop into a transparent, depth sorted bin (1)
94    group->addChild(makeTerrain()); // will drop into default bin - state sorted 0
95    group->addChild(makeTank()); // will drop into default bin - state sorted 0
96    // add the following in the future...
97    // makeGliders
98    // makeClouds
99
100    return group;
101}
102
103int main( int argc, char **argv )
104{
105
106    // use an ArgumentParser object to manage the program arguments.
107    osg::ArgumentParser arguments(&argc,argv);
108
109    // set up the usage document, in case we need to print out how to use this program.
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    // construct the viewer.
115    osgViewer::Viewer viewer;
116
117    viewer.setCameraManipulator(new GliderManipulator());
118
119    // if user request help write it out to cout.
120    if (arguments.read("-h") || arguments.read("--help"))
121    {
122        arguments.getApplicationUsage()->write(std::cout);
123        return 1;
124    }
125
126    // any option left unread are converted into errors to write out later.
127    arguments.reportRemainingOptionsAsUnrecognized();
128
129    // report any errors if they have occured when parsing the program aguments.
130    if (arguments.errors())
131    {
132        arguments.writeErrorMessages(std::cout);
133        return 1;
134    }
135   
136    // load the nodes from the commandline arguments.
137    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
138    if (!rootnode) rootnode = createModel();
139
140    viewer.setSceneData( rootnode );
141
142#if 0
143
144    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
145    if (!wsi)
146    {
147        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
148        return 0;
149    }
150   
151    unsigned int width, height;
152    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
153
154    width -= 500;
155    height -= 500;
156
157    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
158    traits->x = 500;
159    traits->y = 500;
160    traits->width = width;
161    traits->height = height;
162#if 0
163    traits->windowDecoration = false;
164#else
165    traits->windowDecoration = true;
166#endif           
167    traits->doubleBuffer = true;
168    traits->sharedContext = 0;
169
170    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
171
172    osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
173    if (gw)
174    {
175        osg::notify(osg::NOTICE)<<"  GraphicsWindow has been created successfully."<<gw<<std::endl;
176
177        gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height );
178    }
179    else
180    {
181        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
182    }
183
184    unsigned int numCameras = 2;
185    double aspectRatioScale = 1.0/(double)numCameras;
186    for(unsigned int i=0; i<numCameras;++i)
187    {
188        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
189        camera->setGraphicsContext(gc.get());
190        camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
191        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
192        camera->setDrawBuffer(buffer);
193        camera->setReadBuffer(buffer);
194
195        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
196    }
197
198    viewer.setUpRenderingSupport();
199    viewer.assignSceneDataToCameras();
200   
201#else
202
203    viewer.setUpViewAcrossAllScreens();
204   
205#endif   
206
207    viewer.realize();
208
209    while( !viewer.done() )
210    {
211        // fire off the cull and draw traversals of the scene.
212        viewer.frame();
213       
214    }
215
216    return 0;
217}
Note: See TracBrowser for help on using the browser.