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

Revision 4626, 4.6 kB (checked in by robert, 9 years ago)

Added option to osgdepthpartion for loading the model specified on the commandline.

  • 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 <osgUtil/UpdateVisitor>
10
11#include <osgDB/ReadFile>
12
13#include <osg/ShapeDrawable>
14#include <osg/PositionAttitudeTransform>
15
16#include <osgProducer/Viewer>
17
18#include "DepthPartitionNode.h"
19
20const double r_earth = 6378.137;
21const double r_sun = 695990.0;
22const double AU = 149697900.0;
23
24osg::Node* createScene()
25{
26        // Create the Earth, in blue
27        osg::ShapeDrawable *earth_sd = new osg::ShapeDrawable;
28        osg::Sphere* earth_sphere = new osg::Sphere;
29        earth_sphere->setRadius(r_earth);
30        earth_sd->setShape(earth_sphere);
31        earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0));
32
33        osg::Geode* earth = new osg::Geode;
34        earth->setName("earth");
35        earth->addDrawable(earth_sd);
36
37        // Create the Sun, in yellow
38        osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable;
39        osg::Sphere* sun_sphere = new osg::Sphere;
40        sun_sphere->setRadius(r_sun);
41        sun_sd->setShape(sun_sphere);
42        sun_sd->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
43
44        osg::Geode* sun = new osg::Geode;
45        sun->setName("sun");
46        sun->addDrawable(sun_sd);
47
48        // Move the sun behind the earth
49        osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform;
50        pat->setPosition(osg::Vec3d(0.0, AU, 0.0));
51
52        osg::Group* scene = new osg::Group;
53        scene->addChild(earth);
54        scene->addChild(pat);
55        pat->addChild(sun);
56
57        return scene;
58}
59
60int main( int argc, char **argv )
61{
62
63    // use an ArgumentParser object to manage the program arguments.
64    osg::ArgumentParser arguments(&argc,argv);
65   
66    // set up the usage document, in case we need to print out how to use this program.
67    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
68    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard example of using osgProducer::CameraGroup.");
69    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] ...");
70    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
71
72    // construct the viewer.
73    osgProducer::Viewer viewer(arguments);
74
75    // set up the value with sensible default event handlers.
76    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
77
78    // if user request help write it out to cout.
79    if (arguments.read("-h") || arguments.read("--help"))
80    {
81        arguments.getApplicationUsage()->write(std::cout);
82        return 1;
83    }
84
85    // any option left unread are converted into errors to write out later.
86    arguments.reportRemainingOptionsAsUnrecognized();
87
88    // report any errors if they have occured when parsing the program aguments.
89    if (arguments.errors())
90    {
91        arguments.writeErrorMessages(std::cout);
92        return 1;
93    }
94
95
96    bool needToSetHomePosition = false;
97
98    // read the scene from the list of file specified commandline args.
99    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
100
101    // if one hasn't been loaded create an earth and sun test model.
102    if (!scene)
103    {
104        scene = createScene();
105        needToSetHomePosition = true;
106    }
107   
108    // Create a DepthPartitionNode to manage partitioning of the scene
109    osg::ref_ptr<DepthPartitionNode> dpn = new DepthPartitionNode;
110    dpn->addChild(scene.get());
111    dpn->setActive(true); // Control whether the node analyzes the scene
112       
113    // pass the loaded scene graph to the viewer.
114    viewer.setSceneData(dpn.get());
115
116    if (needToSetHomePosition)
117    {
118        viewer.getKeySwitchMatrixManipulator()->setHomePosition(osg::Vec3d(0.0,-5.0*r_earth,0.0),osg::Vec3d(0.0,0.0,0.0),osg::Vec3d(0.0,0.0,1.0));
119    }
120
121    // create the windows and run the threads.
122    viewer.realize();
123
124    while( !viewer.done() )
125    {
126        // wait for all cull and draw threads to complete.
127        viewer.sync();
128
129        // update the scene by traversing it with the the update visitor which will
130        // call all node update callbacks and animations.
131        viewer.update();
132         
133        // fire off the cull and draw traversals of the scene.
134        viewer.frame();
135       
136    }
137   
138    // wait for all cull and draw threads to complete before exit.
139    viewer.sync();
140
141    // run a clean up frame to delete all OpenGL objects.
142    viewer.cleanup_frame();
143
144    // wait for all the clean up frame to complete.
145    viewer.sync();
146
147    return 0;
148}
Note: See TracBrowser for help on using the browser.