| 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 | |
|---|
| 24 | class 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 | |
|---|
| 43 | osg::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 | |
|---|
| 51 | osgTerrain::DataSet::setNotifyOffset(1); |
|---|
| 52 | |
|---|
| 53 | osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 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 | |
|---|
| 69 | dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE); |
|---|
| 70 | dataSet->setConvertFromGeographicToGeocentric(true); |
|---|
| 71 | dataSet->setDestinationName("test.osg"); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 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 | |
|---|
| 85 | |
|---|
| 86 | scene->releaseGLObjects(dataSet->getState()); |
|---|
| 87 | osg::Texture::flushAllDeletedTextureObjects(0); |
|---|
| 88 | osg::Drawable::flushAllDeletedDisplayLists(0); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | return scene.release(); |
|---|
| 92 | |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | osgText::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 | |
|---|
| 116 | osg::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 = 50.0; |
|---|
| 137 | double longitude = 0.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 | longitude = 0.0; |
|---|
| 145 | for(unsigned int j = 0; j < numLong; ++j, ++t, longitude += deltaLongitude) |
|---|
| 146 | { |
|---|
| 147 | geode->addDrawable( createText( ellipsoid, latitude, longitude, 0, textList[t % textList.size()]) ); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return group; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | int main(int argc, char **argv) |
|---|
| 156 | { |
|---|
| 157 | |
|---|
| 158 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 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 | |
|---|
| 167 | osgProducer::Viewer viewer(arguments); |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 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 | |
|---|
| 176 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 181 | { |
|---|
| 182 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 183 | return 1; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | if (arguments.errors()) |
|---|
| 191 | { |
|---|
| 192 | arguments.writeErrorMessages(std::cout); |
|---|
| 193 | return 1; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | osg::ref_ptr<osg::Node> root = createEarth(); |
|---|
| 198 | |
|---|
| 199 | if (!root) return 0; |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | viewer.setSceneData(root.get()); |
|---|
| 203 | |
|---|
| 204 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get()); |
|---|
| 205 | if (csn) |
|---|
| 206 | { |
|---|
| 207 | |
|---|
| 208 | csn->addChild(createFadeText(csn->getEllipsoidModel())); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | viewer.realize(); |
|---|
| 214 | |
|---|
| 215 | while( !viewer.done() ) |
|---|
| 216 | { |
|---|
| 217 | |
|---|
| 218 | viewer.sync(); |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | viewer.update(); |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | viewer.frame(); |
|---|
| 226 | |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | viewer.sync(); |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | viewer.cleanup_frame(); |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | viewer.sync(); |
|---|
| 237 | |
|---|
| 238 | return 0; |
|---|
| 239 | } |
|---|