root/OpenSceneGraph/trunk/examples/osgfadetext/osgfadetext.cpp @ 5578

Revision 5578, 6.7 kB (checked in by robert, 8 years ago)

Updates for fade text to work properly, but only in perspetive views at present.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgProducer/Viewer>
2
3#include <osg/Group>
4#include <osg/Geode>
5#include <osg/ShapeDrawable>
6#include <osg/Texture2D>
7#include <osg/PositionAttitudeTransform>
8#include <osg/MatrixTransform>
9#include <osg/CoordinateSystemNode>
10
11#include <osgDB/FileUtils>
12#include <osgDB/ReadFile>
13
14#include <osgText/FadeText>
15
16#include <osgTerrain/DataSet>
17
18#include <osgSim/OverlayNode>
19#include <osgSim/SphereSegment>
20
21#include <osgGA/NodeTrackerManipulator>
22
23class GraphicsContext {
24    public:
25        GraphicsContext()
26        {
27            rs = new Producer::RenderSurface;
28            rs->setWindowRectangle(0,0,1,1);
29            rs->useBorder(false);
30            rs->useConfigEventThread(false);
31            rs->realize();
32        }
33
34        virtual ~GraphicsContext()
35        {
36        }
37       
38    private:
39        Producer::ref_ptr<Producer::RenderSurface> rs;
40};
41
42osg::Node* createEarth()
43{
44    osg::ref_ptr<osg::Node> scene;
45   
46    {
47        std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg");
48
49        // make osgTerrain::DataSet quieter..
50        osgTerrain::DataSet::setNotifyOffset(1);
51
52        osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet;
53
54        // register the source imagery
55        {
56            osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename);
57
58            source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
59            source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84"));
60
61             source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION);
62             source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0);
63
64            dataSet->addSource(source);
65        }
66
67        // set up destination database paramters.
68        dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE);
69        dataSet->setConvertFromGeographicToGeocentric(true);
70        dataSet->setDestinationName("test.osg");
71
72        // load the source data and record sizes.
73        dataSet->loadSources();
74
75        GraphicsContext context;
76        dataSet->createDestination(30);
77
78        if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination();
79        else dataSet->writeDestination();
80       
81        scene = dataSet->getDestinationRootNode();
82       
83        // now we must get rid of all the old OpenGL objects before we start using the scene graph again
84        // otherwise it'll end up in an inconsistent state.
85        scene->releaseGLObjects(dataSet->getState());
86        osg::Texture::flushAllDeletedTextureObjects(0);
87        osg::Drawable::flushAllDeletedDisplayLists(0);
88    }
89       
90    return scene.release();
91   
92}
93
94osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid)
95{
96    osg::Group* group = new osg::Group;
97   
98    osg::Vec3 position;
99
100    {
101        osg::Geode* geode = new osg::Geode;
102        osgText::FadeText* text = new osgText::FadeText;
103       
104        text->setText("One");
105        text->setFont("fonts/arial.ttf");
106        text->setPosition(position);
107        text->setCharacterSize(300000.0f);
108        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
109        text->setAutoRotateToScreen(true);
110       
111        geode->addDrawable(text);
112        group->addChild(geode);
113    }
114   
115    position.z() += 300000.0f;
116   
117    {
118        osg::Geode* geode = new osg::Geode;
119        osgText::FadeText* text = new osgText::FadeText;
120       
121        text->setText("Two");
122        text->setFont("fonts/arial.ttf");
123        text->setPosition(position);
124        text->setCharacterSize(300000.0f);
125        text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
126        text->setAutoRotateToScreen(true);
127       
128        geode->addDrawable(text);
129        group->addChild(geode);
130    }
131
132    return group;
133}
134
135
136int main(int argc, char **argv)
137{
138    // use an ArgumentParser object to manage the program arguments.
139    osg::ArgumentParser arguments(&argc,argv);
140   
141    // set up the usage document, in case we need to print out how to use this program.
142    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker.");
143    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName());
144    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
145   
146
147    // construct the viewer.
148    osgProducer::Viewer viewer(arguments);
149
150    // set up the value with sensible default event handlers.
151    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
152
153    viewer.getCullSettings().setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
154    viewer.getCullSettings().setNearFarRatio(0.00001f);
155
156    // get details on keyboard and mouse bindings used by the viewer.
157    viewer.getUsage(*arguments.getApplicationUsage());
158
159
160    // if user request help write it out to cout.
161    if (arguments.read("-h") || arguments.read("--help"))
162    {
163        arguments.getApplicationUsage()->write(std::cout);
164        return 1;
165    }
166
167    // any option left unread are converted into errors to write out later.
168    arguments.reportRemainingOptionsAsUnrecognized();
169
170    // report any errors if they have occured when parsing the program aguments.
171    if (arguments.errors())
172    {
173        arguments.writeErrorMessages(std::cout);
174        return 1;
175    }
176   
177    // read the scene from the list of file specified commandline args.
178    osg::ref_ptr<osg::Node> root = createEarth();
179   
180    if (!root) return 0;
181
182    // add a viewport to the viewer and attach the scene graph.
183    viewer.setSceneData(root.get());
184
185    osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
186    if (csn)
187    {
188        // add fade text around the globe
189        csn->addChild(createFadeText(csn->getEllipsoidModel()));
190    }   
191       
192
193    // create the windows and run the threads.
194    viewer.realize();
195
196    while( !viewer.done() )
197    {
198        // wait for all cull and draw threads to complete.
199        viewer.sync();
200
201        // update the scene by traversing it with the the update visitor which will
202        // call all node update callbacks and animations.
203        viewer.update();
204         
205        // fire off the cull and draw traversals of the scene.
206        viewer.frame();
207       
208    }
209   
210    // wait for all cull and draw threads to complete.
211    viewer.sync();
212
213    // run a clean up frame to delete all OpenGL objects.
214    viewer.cleanup_frame();
215
216    // wait for all the clean up frame to complete.
217    viewer.sync();
218
219    return 0;
220}
Note: See TracBrowser for help on using the browser.