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

Revision 5579, 7.2 kB (checked in by robert, 8 years ago)

Created stress test of fade text labelling

  • 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
94osgText::Text* createText(osg::EllipsoidModel* ellipsoid, double latitude, double longitude, double height, const std::string& str)
95{
96    double X,Y,Z;
97    ellipsoid->convertLatLongHeightToXYZ( osg::DegreesToRadians(latitude), osg::DegreesToRadians(longitude), height, X, Y, Z);
98
99    osgText::FadeText* text = new osgText::FadeText;
100
101    text->setText(str);
102    text->setFont("fonts/arial.ttf");
103    text->setPosition(osg::Vec3(X,Y,Z));
104    text->setCharacterSize(300000.0f);
105    text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
106    text->setAutoRotateToScreen(true);
107   
108    return text;
109}
110
111osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid)
112{
113    osg::Group* group = new osg::Group;
114
115    group->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
116   
117    osg::Geode* geode = new osg::Geode;
118    group->addChild(geode);
119   
120    std::vector<std::string> textList;
121    textList.push_back("Town");
122    textList.push_back("City");
123    textList.push_back("Village");
124    textList.push_back("River");
125    textList.push_back("Mountain");
126    textList.push_back("Road");
127    textList.push_back("Lake");
128   
129    unsigned int numLat = 15;
130    unsigned int numLong = 20;
131    double latitude = 50.0;
132    double longitude = 0.0;
133    double deltaLatitude = 1.0f;
134    double deltaLongitude = 1.0f;
135
136    unsigned int t = 0;
137    for(unsigned int i = 0; i < numLat; ++i, latitude += deltaLatitude)
138    {
139        longitude = 0.0;
140        for(unsigned int j = 0; j < numLong; ++j, ++t, longitude += deltaLongitude)
141        {
142            geode->addDrawable( createText( ellipsoid, latitude, longitude, 0, textList[t % textList.size()]) );
143        }
144    }
145
146    return group;
147}
148
149
150int main(int argc, char **argv)
151{
152    // use an ArgumentParser object to manage the program arguments.
153    osg::ArgumentParser arguments(&argc,argv);
154   
155    // set up the usage document, in case we need to print out how to use this program.
156    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker.");
157    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName());
158    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
159   
160
161    // construct the viewer.
162    osgProducer::Viewer viewer(arguments);
163
164    // set up the value with sensible default event handlers.
165    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
166
167    viewer.getCullSettings().setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
168    viewer.getCullSettings().setNearFarRatio(0.00001f);
169
170    // get details on keyboard and mouse bindings used by the viewer.
171    viewer.getUsage(*arguments.getApplicationUsage());
172
173
174    // if user request help write it out to cout.
175    if (arguments.read("-h") || arguments.read("--help"))
176    {
177        arguments.getApplicationUsage()->write(std::cout);
178        return 1;
179    }
180
181    // any option left unread are converted into errors to write out later.
182    arguments.reportRemainingOptionsAsUnrecognized();
183
184    // report any errors if they have occured when parsing the program aguments.
185    if (arguments.errors())
186    {
187        arguments.writeErrorMessages(std::cout);
188        return 1;
189    }
190   
191    // read the scene from the list of file specified commandline args.
192    osg::ref_ptr<osg::Node> root = createEarth();
193   
194    if (!root) return 0;
195
196    // add a viewport to the viewer and attach the scene graph.
197    viewer.setSceneData(root.get());
198
199    osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
200    if (csn)
201    {
202        // add fade text around the globe
203        csn->addChild(createFadeText(csn->getEllipsoidModel()));
204    }   
205       
206
207    // create the windows and run the threads.
208    viewer.realize();
209
210    while( !viewer.done() )
211    {
212        // wait for all cull and draw threads to complete.
213        viewer.sync();
214
215        // update the scene by traversing it with the the update visitor which will
216        // call all node update callbacks and animations.
217        viewer.update();
218         
219        // fire off the cull and draw traversals of the scene.
220        viewer.frame();
221       
222    }
223   
224    // wait for all cull and draw threads to complete.
225    viewer.sync();
226
227    // run a clean up frame to delete all OpenGL objects.
228    viewer.cleanup_frame();
229
230    // wait for all the clean up frame to complete.
231    viewer.sync();
232
233    return 0;
234}
Note: See TracBrowser for help on using the browser.