root/OpenSceneGraph/trunk/examples/osgdepthpartition/osgdepthpartition.cpp @ 4582

Revision 4582, 6.0 kB (checked in by robert, 9 years ago)

From Ravi Mathur, osgdepthpartition example program.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1//C++ source file - Open Producer - Copyright (C) 2002 Don Burns
2//Distributed under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE (LGPL)
3//as published by the Free Software Foundation.
4
5// Simple example of use of Producer::RenderSurface
6// The myGraphics class is a simple sample of how one would implement
7// graphics drawing with Producer::RenderSurface
8
9#include <stdio.h>
10
11#include <Producer/Camera>
12#include <Producer/CameraConfig>
13#include <Producer/InputArea>
14#include <Producer/KeyboardMouse>
15#include <Producer/Trackball>
16
17#include <osg/Timer>
18
19#include <osgUtil/UpdateVisitor>
20
21#include <osgDB/ReadFile>
22
23#include <osg/ShapeDrawable>
24#include <osg/PositionAttitudeTransform>
25
26#include <osgProducer/OsgCameraGroup>
27#include <osgProducer/OsgSceneHandler>
28
29#include "DepthPartitionNode.h"
30
31class MyKeyboardMouseCallback : public Producer::KeyboardMouseCallback
32{
33    public:
34
35        MyKeyboardMouseCallback() :
36                Producer::KeyboardMouseCallback(),
37                _mx(0.0f),_my(0.0f),_mbutton(0),
38                _done(false)   
39                {}
40
41        virtual void specialKeyPress( Producer::KeyCharacter key )
42        {
43                if (key==Producer::KeyChar_Escape)
44                    shutdown();
45        }
46
47        virtual void shutdown()
48        {
49            _done = true;
50        }
51
52        virtual void keyPress( Producer::KeyCharacter )
53        {
54        }
55
56        virtual void mouseMotion( float mx, float my )
57        {
58                _mx = mx;
59                _my = my;
60        }
61        virtual void buttonPress( float mx, float my, unsigned int mbutton )
62        {
63                _mx = mx;
64                _my = my;
65                _mbutton |= (1<<(mbutton-1));
66        }
67        virtual void buttonRelease( float mx, float my, unsigned int mbutton )
68        {
69                _mx = mx;
70                _my = my;
71                _mbutton &= ~(1<<(mbutton-1));
72        }
73
74        bool done() { return _done; }
75        float mx()  { return _mx; }
76        float my()  { return _my; }
77        unsigned int mbutton()  { return _mbutton; }
78
79    private:
80
81        float _mx, _my;
82        unsigned int _mbutton;
83        bool _done;
84};
85
86const double r_earth = 6378.137;
87const double r_sun = 695990.0;
88const double AU = 149697900.0;
89
90osg::Node* createScene()
91{
92        // Create the Earth, in blue
93        osg::ShapeDrawable *earth_sd = new osg::ShapeDrawable;
94        osg::Sphere* earth_sphere = new osg::Sphere;
95        earth_sphere->setRadius(r_earth);
96        earth_sd->setShape(earth_sphere);
97        earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0));
98
99        osg::Geode* earth = new osg::Geode;
100        earth->setName("earth");
101        earth->addDrawable(earth_sd);
102
103        // Create the Sun, in yellow
104        osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable;
105        osg::Sphere* sun_sphere = new osg::Sphere;
106        sun_sphere->setRadius(r_sun);
107        sun_sd->setShape(sun_sphere);
108        sun_sd->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
109
110        osg::Geode* sun = new osg::Geode;
111        sun->setName("sun");
112        sun->addDrawable(sun_sd);
113
114        // Move the sun behind the earth
115        osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform;
116        pat->setPosition(osg::Vec3d(0.0, AU, 0.0));
117
118        osg::Group* scene = new osg::Group;
119        scene->addChild(earth);
120        scene->addChild(pat);
121        pat->addChild(sun);
122
123        return scene;
124}
125
126int main( int argc, char **argv )
127{
128
129    // use an ArgumentParser object to manage the program arguments.
130    osg::ArgumentParser arguments(&argc,argv);
131   
132    // set up the usage document, in case we need to print out how to use this program.
133    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
134    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard example of using osgProducer::CameraGroup.");
135    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] ...");
136    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
137
138    // create the camera group.
139    osgProducer::OsgCameraGroup cg(arguments);
140
141    // if user request help write it out to cout.
142    if (arguments.read("-h") || arguments.read("--help"))
143    {
144        arguments.getApplicationUsage()->write(std::cout);
145        return 1;
146    }
147
148    // any option left unread are converted into errors to write out later.
149    arguments.reportRemainingOptionsAsUnrecognized();
150
151    // report any errors if they have occured when parsing the program aguments.
152    if (arguments.errors())
153    {
154        arguments.writeErrorMessages(std::cout);
155        return 1;
156    }
157
158    // set up the keyboard and mouse handling.
159    Producer::InputArea *ia = cg.getCameraConfig()->getInputArea();
160    Producer::KeyboardMouse *kbm = ia ?
161                                   (new Producer::KeyboardMouse(ia)) :
162                                   (new Producer::KeyboardMouse(cg.getCamera(0)->getRenderSurface()));
163
164
165    osg::ref_ptr<MyKeyboardMouseCallback> kbmcb = new MyKeyboardMouseCallback;
166   
167    // register the callback with the keyboard mouse manger.
168    kbm->setCallback( kbmcb.get() );
169    kbm->startThread();
170
171    // Create a DepthPartitionNode to manage partitioning of the scene
172    DepthPartitionNode *dpn = new DepthPartitionNode;
173    osg::Node* scene = createScene();
174    dpn->addChild(scene);
175    dpn->setActive(true); // Control whether the node analyzes the scene
176       
177    // set the scene to render
178    cg.setSceneData(dpn);
179
180    // Set the view to be looking at the earth
181    osg::BoundingSphere bs;
182    bs._radius = r_earth;
183
184    osg::ref_ptr<Producer::Trackball> tb = new Producer::Trackball;
185    tb->setOrientation( Producer::Trackball::Z_UP );
186    tb->setDistance(bs.radius()*10.0f);
187    tb->translate(-bs.center().x(),-bs.center().y(),-bs.center().z());
188
189    // create the windows and run the threads.
190    cg.realize();
191
192    osgUtil::UpdateVisitor update;
193
194    while( !kbmcb->done() )
195    {
196        // syncronize to the when cull and draw threads have completed.
197        cg.sync();
198
199        // update the scene by traversing it with the the update visitor which will
200        // call all node update callbacks and animations.
201        cg.getSceneData()->accept(update);
202
203        tb->input( kbmcb->mx(), kbmcb->my(), kbmcb->mbutton() );
204
205        // update the main producer camera
206        cg.setViewByMatrix(tb->getMatrix());
207         
208        // fire off the cull and draw traversals of the scene.
209        cg.frame();
210    }
211
212    // syncronize to the when cull and draw threads have completed.
213    cg.sync();
214
215    return 0;
216}
Note: See TracBrowser for help on using the browser.