| 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 | |
|---|
| 11 | #include <osgDB/FileUtils> |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | |
|---|
| 14 | #include <osgText/FadeText> |
|---|
| 15 | |
|---|
| 16 | #include <osgTerrain/DataSet> |
|---|
| 17 | |
|---|
| 18 | #include <osgSim/OverlayNode> |
|---|
| 19 | #include <osgSim/SphereSegment> |
|---|
| 20 | |
|---|
| 21 | #include <osgGA/NodeTrackerManipulator> |
|---|
| 22 | |
|---|
| 23 | class GraphicsContext { |
|---|
| 24 | public: |
|---|
| 25 | GraphicsContext() |
|---|
| 26 | { |
|---|
| 27 | rs = new Producer::RenderSurface; |
|---|
| 28 | rs->setWindowRectangle(0,0,1,1); |
|---|
| 29 | rs->useBorder(false); |
|---|
| 30 | rs->useConfigEventThread(false); |
|---|
| 31 | rs->realize(); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | virtual ~GraphicsContext() |
|---|
| 35 | { |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | private: |
|---|
| 39 | Producer::ref_ptr<Producer::RenderSurface> rs; |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | osg::Node* createEarth() |
|---|
| 43 | { |
|---|
| 44 | osg::ref_ptr<osg::Node> scene; |
|---|
| 45 | |
|---|
| 46 | { |
|---|
| 47 | std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg"); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | osgTerrain::DataSet::setNotifyOffset(1); |
|---|
| 51 | |
|---|
| 52 | osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | { |
|---|
| 56 | osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename); |
|---|
| 57 | |
|---|
| 58 | source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); |
|---|
| 59 | source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84")); |
|---|
| 60 | |
|---|
| 61 | source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION); |
|---|
| 62 | source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0); |
|---|
| 63 | |
|---|
| 64 | dataSet->addSource(source); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE); |
|---|
| 69 | dataSet->setConvertFromGeographicToGeocentric(true); |
|---|
| 70 | dataSet->setDestinationName("test.osg"); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | dataSet->loadSources(); |
|---|
| 74 | |
|---|
| 75 | GraphicsContext context; |
|---|
| 76 | dataSet->createDestination(30); |
|---|
| 77 | |
|---|
| 78 | if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination(); |
|---|
| 79 | else dataSet->writeDestination(); |
|---|
| 80 | |
|---|
| 81 | scene = dataSet->getDestinationRootNode(); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | scene->releaseGLObjects(dataSet->getState()); |
|---|
| 86 | osg::Texture::flushAllDeletedTextureObjects(0); |
|---|
| 87 | osg::Drawable::flushAllDeletedDisplayLists(0); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | return scene.release(); |
|---|
| 91 | |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid) |
|---|
| 95 | { |
|---|
| 96 | osg::Group* group = new osg::Group; |
|---|
| 97 | |
|---|
| 98 | osg::Vec3 position; |
|---|
| 99 | |
|---|
| 100 | { |
|---|
| 101 | osg::Geode* geode = new osg::Geode; |
|---|
| 102 | osgText::FadeText* text = new osgText::FadeText; |
|---|
| 103 | |
|---|
| 104 | text->setText("This is a test"); |
|---|
| 105 | text->setFont("fonts/arial.ttf"); |
|---|
| 106 | text->setPosition(position); |
|---|
| 107 | text->setCharacterSize(300000.0f); |
|---|
| 108 | text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
|---|
| 109 | text->setAutoRotateToScreen(true); |
|---|
| 110 | |
|---|
| 111 | geode->addDrawable(text); |
|---|
| 112 | group->addChild(geode); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | position.z() += 300000.0f; |
|---|
| 116 | |
|---|
| 117 | { |
|---|
| 118 | osg::Geode* geode = new osg::Geode; |
|---|
| 119 | osgText::FadeText* text = new osgText::FadeText; |
|---|
| 120 | |
|---|
| 121 | text->setText("This is a test"); |
|---|
| 122 | text->setFont("fonts/arial.ttf"); |
|---|
| 123 | text->setPosition(position); |
|---|
| 124 | text->setCharacterSize(300000.0f); |
|---|
| 125 | text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
|---|
| 126 | text->setAutoRotateToScreen(true); |
|---|
| 127 | |
|---|
| 128 | geode->addDrawable(text); |
|---|
| 129 | group->addChild(geode); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | return group; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | int main(int argc, char **argv) |
|---|
| 137 | { |
|---|
| 138 | |
|---|
| 139 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker."); |
|---|
| 143 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()); |
|---|
| 144 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | osgProducer::Viewer viewer(arguments); |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 152 | |
|---|
| 153 | viewer.getCullSettings().setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES); |
|---|
| 154 | viewer.getCullSettings().setNearFarRatio(0.00001f); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 162 | { |
|---|
| 163 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 164 | return 1; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | if (arguments.errors()) |
|---|
| 172 | { |
|---|
| 173 | arguments.writeErrorMessages(std::cout); |
|---|
| 174 | return 1; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | osg::ref_ptr<osg::Node> root = createEarth(); |
|---|
| 179 | |
|---|
| 180 | if (!root) return 0; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | viewer.setSceneData(root.get()); |
|---|
| 184 | |
|---|
| 185 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get()); |
|---|
| 186 | if (csn) |
|---|
| 187 | { |
|---|
| 188 | |
|---|
| 189 | csn->addChild(createFadeText(csn->getEllipsoidModel())); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | viewer.realize(); |
|---|
| 195 | |
|---|
| 196 | while( !viewer.done() ) |
|---|
| 197 | { |
|---|
| 198 | |
|---|
| 199 | viewer.sync(); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | viewer.update(); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | viewer.frame(); |
|---|
| 207 | |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | viewer.sync(); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | viewer.cleanup_frame(); |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | viewer.sync(); |
|---|
| 218 | |
|---|
| 219 | return 0; |
|---|
| 220 | } |
|---|