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

Revision 5923, 6.2 kB (checked in by robert, 7 years ago)

Ported osgcopy, osgcubemap, osgdelaunay, osgdepthpartion, osgdistortion, osgfadetext, osgforest and osgfxbrowser to osgViewer

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgViewer/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/TerrainManipulator>
23
24#include <iostream>
25
26class MyGraphicsContext {
27    public:
28        MyGraphicsContext()
29        {
30            osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
31            traits->x = 0;
32            traits->y = 0;
33            traits->width = 1;
34            traits->height = 1;
35            traits->windowDecoration = false;
36            traits->doubleBuffer = false;
37            traits->sharedContext = 0;
38            traits->pbuffer = true;
39
40            _gc = osg::GraphicsContext::createGraphicsContext(traits.get());
41
42            if (!_gc)
43            {
44                osg::notify(osg::NOTICE)<<"Failed to create pbuffer, failing back to normal graphics window."<<std::endl;
45               
46                traits->pbuffer = false;
47                _gc = osg::GraphicsContext::createGraphicsContext(traits.get());
48            }
49
50            if (_gc.valid())
51           
52           
53            {
54                _gc->realize();
55                _gc->makeCurrent();
56                std::cout<<"Realized window"<<std::endl;
57            }
58        }
59       
60        bool valid() const { return _gc.valid() && _gc->isRealized(); }
61       
62    private:
63        osg::ref_ptr<osg::GraphicsContext> _gc;
64};
65
66osg::Node* createEarth()
67{
68    osg::ref_ptr<osg::Node> scene;
69   
70    {
71        std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg");
72
73        // make osgTerrain::DataSet quieter..
74        osgTerrain::DataSet::setNotifyOffset(1);
75
76        osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet;
77
78        // register the source imagery
79        {
80            osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename);
81
82            source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
83            source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84"));
84
85            source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION);
86            source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0);
87
88            dataSet->addSource(source);
89        }
90
91        // set up destination database paramters.
92        dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE);
93        dataSet->setConvertFromGeographicToGeocentric(true);
94        dataSet->setDestinationName("test.osg");
95
96        // load the source data and record sizes.
97        dataSet->loadSources();
98
99        MyGraphicsContext context;
100        dataSet->createDestination(30);
101
102        if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination();
103        else dataSet->writeDestination();
104       
105        scene = dataSet->getDestinationRootNode();
106       
107        // now we must get rid of all the old OpenGL objects before we start using the scene graph again
108        // otherwise it'll end up in an inconsistent state.
109        scene->releaseGLObjects(dataSet->getState());
110        osg::Texture::flushAllDeletedTextureObjects(0);
111        osg::Drawable::flushAllDeletedDisplayLists(0);
112    }
113       
114    return scene.release();
115   
116}
117
118osgText::Text* createText(osg::EllipsoidModel* ellipsoid, double latitude, double longitude, double height, const std::string& str)
119{
120    double X,Y,Z;
121    ellipsoid->convertLatLongHeightToXYZ( osg::DegreesToRadians(latitude), osg::DegreesToRadians(longitude), height, X, Y, Z);
122
123
124    osgText::Text* text = new osgText::FadeText;
125
126    osg::Vec3 normal = ellipsoid->computeLocalUpVector( X, Y, Z);
127    text->setCullCallback(new osg::ClusterCullingCallback(osg::Vec3(X,Y,Z), normal, 0.0));
128
129    text->setText(str);
130    text->setFont("fonts/arial.ttf");
131    text->setPosition(osg::Vec3(X,Y,Z));
132    text->setCharacterSize(300000.0f);
133    text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
134    text->setAutoRotateToScreen(true);
135   
136    return text;
137}
138
139osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid)
140{
141    osg::Group* group = new osg::Group;
142
143    group->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
144   
145    osg::Geode* geode = new osg::Geode;
146    group->addChild(geode);
147   
148    std::vector<std::string> textList;
149    textList.push_back("Town");
150    textList.push_back("City");
151    textList.push_back("Village");
152    textList.push_back("River");
153    textList.push_back("Mountain");
154    textList.push_back("Road");
155    textList.push_back("Lake");
156   
157    unsigned int numLat = 15;
158    unsigned int numLong = 20;
159    double latitude = 0.0;
160    double longitude = -100.0;
161    double deltaLatitude = 1.0f;
162    double deltaLongitude = 1.0f;
163
164    unsigned int t = 0;
165    for(unsigned int i = 0; i < numLat; ++i, latitude += deltaLatitude)
166    {
167        double lgnt = longitude;
168        for(unsigned int j = 0; j < numLong; ++j, ++t, lgnt += deltaLongitude)
169        {
170            geode->addDrawable( createText( ellipsoid, latitude, lgnt, 0, textList[t % textList.size()]) );
171        }
172    }
173
174    return group;
175}
176
177
178int main(int, char**)
179{
180    // construct the viewer.
181    osgViewer::Viewer viewer;
182
183    viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
184    viewer.getCamera()->setNearFarRatio(0.00001f);
185   
186    // read the scene from the list of file specified commandline args.
187    osg::ref_ptr<osg::Node> root = createEarth();
188   
189    if (!root) return 0;
190
191    // add a viewport to the viewer and attach the scene graph.
192    viewer.setSceneData(root.get());
193
194    osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
195    if (csn)
196    {
197        // add fade text around the globe
198        csn->addChild(createFadeText(csn->getEllipsoidModel()));
199    }   
200
201    viewer.setCameraManipulator(new osgGA::TerrainManipulator);
202
203    return viewer.run();
204}
Note: See TracBrowser for help on using the browser.