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

Revision 6283, 3.8 kB (checked in by robert, 8 years ago)

Moved include/osgTerrain/DataSet/.cpp, gdal plugin and osgdem out into the VirtualPlaneBuilder? project.

Updated examples to remove DataSet?/GDAL dependency.

Updated wrappers

  • 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 <osgSim/OverlayNode>
18#include <osgSim/SphereSegment>
19
20#include <osgGA/TerrainManipulator>
21
22#include <iostream>
23
24osg::Node* createEarth()
25{
26    osg::TessellationHints* hints = new osg::TessellationHints;
27    hints->setDetailRatio(5.0f);
28
29   
30    osg::ShapeDrawable* sd = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0,0.0,0.0), osg::WGS_84_RADIUS_POLAR), hints);
31   
32    osg::Geode* geode = new osg::Geode;
33    geode->addDrawable(sd);
34   
35    std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg");
36    geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile(filename)));
37   
38    osg::CoordinateSystemNode* csn = new osg::CoordinateSystemNode;
39    csn->setEllipsoidModel(new osg::EllipsoidModel());
40    csn->addChild(geode);
41   
42    return csn;
43   
44}
45
46osgText::Text* createText(osg::EllipsoidModel* ellipsoid, double latitude, double longitude, double height, const std::string& str)
47{
48    double X,Y,Z;
49    ellipsoid->convertLatLongHeightToXYZ( osg::DegreesToRadians(latitude), osg::DegreesToRadians(longitude), height, X, Y, Z);
50
51
52    osgText::Text* text = new osgText::FadeText;
53
54    osg::Vec3 normal = ellipsoid->computeLocalUpVector( X, Y, Z);
55    text->setCullCallback(new osg::ClusterCullingCallback(osg::Vec3(X,Y,Z), normal, 0.0));
56
57    text->setText(str);
58    text->setFont("fonts/arial.ttf");
59    text->setPosition(osg::Vec3(X,Y,Z));
60    text->setCharacterSize(300000.0f);
61    text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
62    text->setAutoRotateToScreen(true);
63   
64    return text;
65}
66
67osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid)
68{
69    osg::Group* group = new osg::Group;
70
71    group->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
72   
73    osg::Geode* geode = new osg::Geode;
74    group->addChild(geode);
75   
76    std::vector<std::string> textList;
77    textList.push_back("Town");
78    textList.push_back("City");
79    textList.push_back("Village");
80    textList.push_back("River");
81    textList.push_back("Mountain");
82    textList.push_back("Road");
83    textList.push_back("Lake");
84   
85    unsigned int numLat = 15;
86    unsigned int numLong = 20;
87    double latitude = 0.0;
88    double longitude = -100.0;
89    double deltaLatitude = 1.0f;
90    double deltaLongitude = 1.0f;
91
92    unsigned int t = 0;
93    for(unsigned int i = 0; i < numLat; ++i, latitude += deltaLatitude)
94    {
95        double lgnt = longitude;
96        for(unsigned int j = 0; j < numLong; ++j, ++t, lgnt += deltaLongitude)
97        {
98            geode->addDrawable( createText( ellipsoid, latitude, lgnt, 0, textList[t % textList.size()]) );
99        }
100    }
101
102    return group;
103}
104
105
106int main(int, char**)
107{
108    // construct the viewer.
109    osgViewer::Viewer viewer;
110
111    viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
112    viewer.getCamera()->setNearFarRatio(0.00001f);
113   
114    // read the scene from the list of file specified commandline args.
115    osg::ref_ptr<osg::Node> root = createEarth();
116   
117    if (!root) return 0;
118
119    // add a viewport to the viewer and attach the scene graph.
120    viewer.setSceneData(root.get());
121
122    osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
123    if (csn)
124    {
125        // add fade text around the globe
126        csn->addChild(createFadeText(csn->getEllipsoidModel()));
127    }   
128
129    viewer.setCameraManipulator(new osgGA::TerrainManipulator);
130
131    return viewer.run();
132}
Note: See TracBrowser for help on using the browser.