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

Revision 5582, 7.4 kB (checked in by robert, 8 years ago)

Updated positions of the text labels to make them move obvious on start up

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