| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 42 | osg::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 | |
|---|
| 64 | osgText::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 | |
|---|
| 85 | osg::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 | |
|---|
| 124 | int main(int, char**) |
|---|
| 125 | { |
|---|
| 126 | |
|---|
| 127 | osgViewer::Viewer viewer; |
|---|
| 128 | |
|---|
| 129 | viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES); |
|---|
| 130 | viewer.getCamera()->setNearFarRatio(0.00001f); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | osg::ref_ptr<osg::Node> root = createEarth(); |
|---|
| 134 | |
|---|
| 135 | if (!root) return 0; |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | viewer.setSceneData(root.get()); |
|---|
| 139 | |
|---|
| 140 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get()); |
|---|
| 141 | if (csn) |
|---|
| 142 | { |
|---|
| 143 | |
|---|
| 144 | csn->addChild(createFadeText(csn->getEllipsoidModel())); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | viewer.setCameraManipulator(new osgGA::TerrainManipulator); |
|---|
| 148 | |
|---|
| 149 | return viewer.run(); |
|---|
| 150 | } |
|---|