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

Revision 8868, 7.7 kB (checked in by robert, 6 years ago)

From Mathias Froehlich, "This is a generic optimization that does not depend on any cpu or instruction
set.

The optimization is based on the observation that matrix matrix multiplication
with a dense matrix 4x4 is 43 Operations whereas multiplication with a
transform, or scale matrix is only 4
2 operations. Which is a gain of a
*FACTOR*4* for these special cases.
The change implements these special cases, provides a unit test for these
implementation and converts uses of the expensiver dense matrix matrix
routine with the specialized versions.

Depending on the transform nodes in the scenegraph this change gives a
noticable improovement.
For example the osgforest code using the MatrixTransform? is about 20% slower
than the same codepath using the PositionAttitudeTransform? instead of the
MatrixTransform? with this patch applied.

If I remember right, the sse type optimizations did *not* provide a factor 4
improovement. Also these changes are totally independent of any cpu or
instruction set architecture. So I would prefer to have this current kind of
change instead of some hand coded and cpu dependent assembly stuff. If we
need that hand tuned stuff, these can go on top of this changes which must
provide than hand optimized additional variants for the specialized versions
to give a even better result in the end.

An other change included here is a change to rotation matrix from quaterion
code. There is a sqrt call which couold be optimized away. Since we divide in
effect by sqrt(length)*sqrt(length) which is just length ...
"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osghangglide.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
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
31#include <osgViewer/Viewer>
32
33#include "GliderManipulator.h"
34
35#include <iostream>
36
37extern osg::Node *makeTerrain( void );
38extern osg::Node *makeTrees( void );
39extern osg::Node *makeTank( void );
40extern osg::Node *makeWindsocks( void );
41extern osg::Node *makeGliders( void );
42extern osg::Node *makeGlider( void );
43extern osg::Node *makeSky( void );
44extern osg::Node *makeBase( void );
45extern osg::Node *makeClouds( void );
46
47class MoveEarthySkyWithEyePointTransform : public osg::Transform
48{
49public:
50    /** Get the transformation matrix which moves from local coords to world coords.*/
51    virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const 
52    {
53        osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
54        if (cv)
55        {
56            osg::Vec3 eyePointLocal = cv->getEyeLocal();
57            matrix.preMultTranslate(osg::Vec3(eyePointLocal.x(),eyePointLocal.y(),0.0f));
58        }
59        return true;
60    }
61
62    /** Get the transformation matrix which moves from world coords to local coords.*/
63    virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,osg::NodeVisitor* nv) const
64    {
65        std::cout<<"computing transform"<<std::endl;
66   
67        osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv);
68        if (cv)
69        {
70            osg::Vec3 eyePointLocal = cv->getEyeLocal();
71            matrix.postMultTranslate(osg::Vec3(-eyePointLocal.x(),-eyePointLocal.y(),0.0f));
72        }
73        return true;
74    }
75};
76
77osg::Group* createModel()
78{
79    // no database loaded so automatically create Ed Levin Park..
80    osg::Group* group = new osg::Group;
81
82    // the base and sky subgraphs go to set the earth sky of the
83    // model and clear the color and depth buffer for us, by using
84    // osg::Depth, and setting their bin numbers to less than 0,
85    // to force them to draw before the rest of the scene.
86
87    osg::ClearNode* clearNode = new osg::ClearNode;
88    clearNode->setRequiresClear(false); // we've got base and sky to do it.
89
90    // use a transform to make the sky and base around with the eye point.
91    osg::Transform* transform = new MoveEarthySkyWithEyePointTransform;
92
93    // transform's value isn't knowm until in the cull traversal so its bounding
94    // volume is can't be determined, therefore culling will be invalid,
95    // so switch it off, this cause all our paresnts to switch culling
96    // off as well. But don't worry culling will be back on once underneath
97    // this node or any other branch above this transform.
98    transform->setCullingActive(false);
99
100    // add the sky and base layer.
101    transform->addChild(makeSky());  // bin number -2 so drawn first.
102    transform->addChild(makeBase()); // bin number -1 so draw second.     
103
104    // add the transform to the earth sky.
105    clearNode->addChild(transform);
106
107    // add to earth sky to the scene.
108    group->addChild(clearNode);
109
110    // the rest of the scene drawn after the base and sky above.
111    group->addChild(makeTrees()); // will drop into a transparent, depth sorted bin (1)
112    group->addChild(makeTerrain()); // will drop into default bin - state sorted 0
113    group->addChild(makeTank()); // will drop into default bin - state sorted 0
114    // add the following in the future...
115    // makeGliders
116    // makeClouds
117
118    return group;
119}
120
121int main( int argc, char **argv )
122{
123
124    // use an ArgumentParser object to manage the program arguments.
125    osg::ArgumentParser arguments(&argc,argv);
126
127    // set up the usage document, in case we need to print out how to use this program.
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.");
129    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
130    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
131
132    // construct the viewer.
133    osgViewer::Viewer viewer;
134
135    // if user request help write it out to cout.
136    if (arguments.read("-h") || arguments.read("--help"))
137    {
138        arguments.getApplicationUsage()->write(std::cout);
139        return 1;
140    }
141   
142    bool customWindows = false;
143    while(arguments.read("-2")) customWindows = true;
144
145    if (customWindows)
146    {
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        }
153
154        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
155        traits->x = 250;
156        traits->y = 200;
157        traits->width = 800;
158        traits->height = 600;
159        traits->windowDecoration = true;
160        traits->doubleBuffer = true;
161        traits->sharedContext = 0;
162
163        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
164        if (gc.valid())
165        {
166            // need to ensure that the window is cleared make sure that the complete window is set the correct colour
167            // rather than just the parts of the window that are under the camera's viewports
168            gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
169            gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
170        }
171        else
172        {
173            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
174        }
175
176        unsigned int numCameras = 2;
177        double aspectRatioScale = 1.0;
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());
182            camera->setViewport(new osg::Viewport((i* traits->width)/numCameras,(i* traits->height)/numCameras,  traits->width/numCameras,  traits->height/numCameras));
183            GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
184            camera->setDrawBuffer(buffer);
185            camera->setReadBuffer(buffer);
186
187            viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
188        }
189    }   
190    else
191    {
192        viewer.setUpViewAcrossAllScreens();
193   
194    }
195
196    // set up the camera manipulation with out custom manipultor
197    viewer.setCameraManipulator(new GliderManipulator());
198
199    // pass the scene graph to the viewer   
200    viewer.setSceneData( createModel() );
201
202    return viewer.run();
203}
Note: See TracBrowser for help on using the browser.