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

Revision 6941, 4.8 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgfadetext.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgViewer/Viewer>
20
21#include <osg/Group>
22#include <osg/Geode>
23#include <osg/ShapeDrawable>
24#include <osg/Texture2D>
25#include <osg/PositionAttitudeTransform>
26#include <osg/MatrixTransform>
27#include <osg/CoordinateSystemNode>
28#include <osg/ClusterCullingCallback>
29
30#include <osgDB/FileUtils>
31#include <osgDB/ReadFile>
32
33#include <osgText/FadeText>
34
35#include <osgSim/OverlayNode>
36#include <osgSim/SphereSegment>
37
38#include <osgGA/TerrainManipulator>
39
40#include <iostream>
41
42osg::Node* createEarth()
43{
44    osg::TessellationHints* hints = new osg::TessellationHints;
45    hints->setDetailRatio(5.0f);
46
47   
48    osg::ShapeDrawable* sd = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0,0.0,0.0), osg::WGS_84_RADIUS_POLAR), hints);
49   
50    osg::Geode* geode = new osg::Geode;
51    geode->addDrawable(sd);
52   
53    std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg");
54    geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile(filename)));
55   
56    osg::CoordinateSystemNode* csn = new osg::CoordinateSystemNode;
57    csn->setEllipsoidModel(new osg::EllipsoidModel());
58    csn->addChild(geode);
59   
60    return csn;
61   
62}
63
64osgText::Text* createText(osg::EllipsoidModel* ellipsoid, double latitude, double longitude, double height, const std::string& str)
65{
66    double X,Y,Z;
67    ellipsoid->convertLatLongHeightToXYZ( osg::DegreesToRadians(latitude), osg::DegreesToRadians(longitude), height, X, Y, Z);
68
69
70    osgText::Text* text = new osgText::FadeText;
71
72    osg::Vec3 normal = ellipsoid->computeLocalUpVector( X, Y, Z);
73    text->setCullCallback(new osg::ClusterCullingCallback(osg::Vec3(X,Y,Z), normal, 0.0));
74
75    text->setText(str);
76    text->setFont("fonts/arial.ttf");
77    text->setPosition(osg::Vec3(X,Y,Z));
78    text->setCharacterSize(300000.0f);
79    text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT);
80    text->setAutoRotateToScreen(true);
81   
82    return text;
83}
84
85osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid)
86{
87    osg::Group* group = new osg::Group;
88
89    group->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
90   
91    osg::Geode* geode = new osg::Geode;
92    group->addChild(geode);
93   
94    std::vector<std::string> textList;
95    textList.push_back("Town");
96    textList.push_back("City");
97    textList.push_back("Village");
98    textList.push_back("River");
99    textList.push_back("Mountain");
100    textList.push_back("Road");
101    textList.push_back("Lake");
102   
103    unsigned int numLat = 15;
104    unsigned int numLong = 20;
105    double latitude = 0.0;
106    double longitude = -100.0;
107    double deltaLatitude = 1.0f;
108    double deltaLongitude = 1.0f;
109
110    unsigned int t = 0;
111    for(unsigned int i = 0; i < numLat; ++i, latitude += deltaLatitude)
112    {
113        double lgnt = longitude;
114        for(unsigned int j = 0; j < numLong; ++j, ++t, lgnt += deltaLongitude)
115        {
116            geode->addDrawable( createText( ellipsoid, latitude, lgnt, 0, textList[t % textList.size()]) );
117        }
118    }
119
120    return group;
121}
122
123
124int main(int, char**)
125{
126    // construct the viewer.
127    osgViewer::Viewer viewer;
128
129    viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES);
130    viewer.getCamera()->setNearFarRatio(0.00001f);
131   
132    // read the scene from the list of file specified commandline args.
133    osg::ref_ptr<osg::Node> root = createEarth();
134   
135    if (!root) return 0;
136
137    // add a viewport to the viewer and attach the scene graph.
138    viewer.setSceneData(root.get());
139
140    osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get());
141    if (csn)
142    {
143        // add fade text around the globe
144        csn->addChild(createFadeText(csn->getEllipsoidModel()));
145    }   
146
147    viewer.setCameraManipulator(new osgGA::TerrainManipulator);
148
149    return viewer.run();
150}
Note: See TracBrowser for help on using the browser.