| 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/Text> |
|---|
| 15 | |
|---|
| 16 | #include <osgTerrain/DataSet> |
|---|
| 17 | |
|---|
| 18 | #include <osgGA/NodeTrackerManipulator> |
|---|
| 19 | |
|---|
| 20 | class GraphicsContext { |
|---|
| 21 | public: |
|---|
| 22 | GraphicsContext() |
|---|
| 23 | { |
|---|
| 24 | rs = new Producer::RenderSurface; |
|---|
| 25 | rs->setWindowRectangle(0,0,1,1); |
|---|
| 26 | rs->useBorder(false); |
|---|
| 27 | rs->useConfigEventThread(false); |
|---|
| 28 | rs->realize(); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | virtual ~GraphicsContext() |
|---|
| 32 | { |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | private: |
|---|
| 36 | Producer::ref_ptr<Producer::RenderSurface> rs; |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | osg::Node* createEarth() |
|---|
| 40 | { |
|---|
| 41 | osg::ref_ptr<osg::Node> scene; |
|---|
| 42 | |
|---|
| 43 | { |
|---|
| 44 | std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg"); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | osgTerrain::DataSet::setNotifyOffset(1); |
|---|
| 48 | |
|---|
| 49 | osg::ref_ptr<osgTerrain::DataSet> dataSet = new osgTerrain::DataSet; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | { |
|---|
| 53 | osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE, filename); |
|---|
| 54 | |
|---|
| 55 | source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS); |
|---|
| 56 | source->setCoordinateSystem(osgTerrain::DataSet::coordinateSystemStringToWTK("WGS84")); |
|---|
| 57 | |
|---|
| 58 | source->setGeoTransformPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION); |
|---|
| 59 | source->setGeoTransformFromRange(-180.0, 180.0, -90.0, 90.0); |
|---|
| 60 | |
|---|
| 61 | dataSet->addSource(source); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | dataSet->setDatabaseType(osgTerrain::DataSet::LOD_DATABASE); |
|---|
| 66 | dataSet->setConvertFromGeographicToGeocentric(true); |
|---|
| 67 | dataSet->setDestinationName("test.osg"); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | dataSet->loadSources(); |
|---|
| 71 | |
|---|
| 72 | GraphicsContext context; |
|---|
| 73 | dataSet->createDestination(30); |
|---|
| 74 | |
|---|
| 75 | if (dataSet->getDatabaseType()==osgTerrain::DataSet::LOD_DATABASE) dataSet->buildDestination(); |
|---|
| 76 | else dataSet->writeDestination(); |
|---|
| 77 | |
|---|
| 78 | scene = dataSet->getDestinationRootNode(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return scene.release(); |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | class ModelPositionCallback : public osg::NodeCallback |
|---|
| 87 | { |
|---|
| 88 | public: |
|---|
| 89 | |
|---|
| 90 | ModelPositionCallback(): |
|---|
| 91 | _latitude(0.0), |
|---|
| 92 | _longitude(0.0), |
|---|
| 93 | _height(100000.0) |
|---|
| 94 | {} |
|---|
| 95 | |
|---|
| 96 | void updateParameters() |
|---|
| 97 | { |
|---|
| 98 | _latitude -= ((2.0*osg::PI)/360.0)/20.0; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 103 | { |
|---|
| 104 | updateParameters(); |
|---|
| 105 | |
|---|
| 106 | osg::NodePath nodePath = nv->getNodePath(); |
|---|
| 107 | |
|---|
| 108 | osg::MatrixTransform* mt = nodePath.empty() ? 0 : dynamic_cast<osg::MatrixTransform*>(nodePath.back()); |
|---|
| 109 | if (mt) |
|---|
| 110 | { |
|---|
| 111 | osg::CoordinateSystemNode* csn = 0; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | unsigned int i; |
|---|
| 115 | for(i=0; i<nodePath.size() && csn==0; ++i) |
|---|
| 116 | { |
|---|
| 117 | csn = dynamic_cast<osg::CoordinateSystemNode*>(nodePath[i]); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | if (csn) |
|---|
| 121 | { |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
|---|
| 125 | if (ellipsoid) |
|---|
| 126 | { |
|---|
| 127 | osg::Matrixd matrix; |
|---|
| 128 | for(i+=1; i<nodePath.size()-1; ++i) |
|---|
| 129 | { |
|---|
| 130 | osg::Transform* transform = nodePath[i]->asTransform(); |
|---|
| 131 | if (transform) transform->computeLocalToWorldMatrix(matrix, nv); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | ellipsoid->computeLocalToWorldTransformFromLatLongHeight(_latitude,_longitude,_height,matrix); |
|---|
| 136 | matrix.preMult(osg::Matrixd::rotate(_rotation)); |
|---|
| 137 | |
|---|
| 138 | mt->setMatrix(matrix); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | traverse(node,nv); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | double _latitude; |
|---|
| 148 | double _longitude; |
|---|
| 149 | double _height; |
|---|
| 150 | osg::Quat _rotation; |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | class FindNamedNodeVisitor : public osg::NodeVisitor |
|---|
| 155 | { |
|---|
| 156 | public: |
|---|
| 157 | FindNamedNodeVisitor(const std::string& name): |
|---|
| 158 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 159 | _name(name) {} |
|---|
| 160 | |
|---|
| 161 | virtual void apply(osg::Node& node) |
|---|
| 162 | { |
|---|
| 163 | if (node.getName()==_name) |
|---|
| 164 | { |
|---|
| 165 | _foundNodes.push_back(&node); |
|---|
| 166 | } |
|---|
| 167 | traverse(node); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | typedef std::vector< osg::ref_ptr<osg::Node> > NodeList; |
|---|
| 171 | |
|---|
| 172 | std::string _name; |
|---|
| 173 | NodeList _foundNodes; |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | int main(int argc, char **argv) |
|---|
| 178 | { |
|---|
| 179 | |
|---|
| 180 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of particle systems."); |
|---|
| 184 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] image_file_left_eye image_file_right_eye"); |
|---|
| 185 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | osgProducer::Viewer viewer(arguments); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 193 | |
|---|
| 194 | viewer.getCullSettings().setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES); |
|---|
| 195 | viewer.getCullSettings().setNearFarRatio(0.00001f); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 199 | |
|---|
| 200 | osg::Quat rotation; |
|---|
| 201 | osg::Vec4 vec4; |
|---|
| 202 | while (arguments.read("--rotate-model",vec4[0],vec4[1],vec4[2],vec4[3])) |
|---|
| 203 | { |
|---|
| 204 | osg::Quat local_rotate; |
|---|
| 205 | local_rotate.makeRotate(osg::DegreesToRadians(vec4[0]),vec4[1],vec4[2],vec4[3]); |
|---|
| 206 | |
|---|
| 207 | rotation = rotation * local_rotate; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | osg::NodeCallback* nc = 0; |
|---|
| 211 | std::string flightpath_filename; |
|---|
| 212 | while (arguments.read("--flight-path",flightpath_filename)) |
|---|
| 213 | { |
|---|
| 214 | std::fstream fin(flightpath_filename.c_str()); |
|---|
| 215 | if (fin) |
|---|
| 216 | { |
|---|
| 217 | osg::AnimationPath* path = new osg::AnimationPath; |
|---|
| 218 | path->read(fin); |
|---|
| 219 | nc = new osg::AnimationPathCallback(path); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION; |
|---|
| 224 | std::string mode; |
|---|
| 225 | while (arguments.read("--tracker-mode",mode)) |
|---|
| 226 | { |
|---|
| 227 | if (mode=="NODE_CENTER_AND_ROTATION") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION; |
|---|
| 228 | else if (mode=="NODE_CENTER_AND_AZIM") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_AZIM; |
|---|
| 229 | else if (mode=="NODE_CENTER") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER; |
|---|
| 230 | else |
|---|
| 231 | { |
|---|
| 232 | std::cout<<"Unrecognized --tracker-mode option "<<mode<<", valid options are:"<<std::endl; |
|---|
| 233 | std::cout<<" NODE_CENTER_AND_ROTATION"<<std::endl; |
|---|
| 234 | std::cout<<" NODE_CENTER_AND_AZIM"<<std::endl; |
|---|
| 235 | std::cout<<" NODE_CENTER"<<std::endl; |
|---|
| 236 | return 1; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL; |
|---|
| 242 | while (arguments.read("--rotation-mode",mode)) |
|---|
| 243 | { |
|---|
| 244 | if (mode=="TRACKBALL") rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL; |
|---|
| 245 | else if (mode=="ELEVATION_AZIM") rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM; |
|---|
| 246 | else |
|---|
| 247 | { |
|---|
| 248 | std::cout<<"Unrecognized --rotation-mode option "<<mode<<", valid options are:"<<std::endl; |
|---|
| 249 | std::cout<<" TRACKBALL"<<std::endl; |
|---|
| 250 | std::cout<<" ELEVATION_AZIM"<<std::endl; |
|---|
| 251 | return 1; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 257 | { |
|---|
| 258 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 259 | return 1; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | if (arguments.errors()) |
|---|
| 267 | { |
|---|
| 268 | arguments.writeErrorMessages(std::cout); |
|---|
| 269 | return 1; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | osg::ref_ptr<osg::Node> root = createEarth(); |
|---|
| 273 | |
|---|
| 274 | if (!root) return 0; |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | viewer.setSceneData(root.get()); |
|---|
| 278 | |
|---|
| 279 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get()); |
|---|
| 280 | if (csn) |
|---|
| 281 | { |
|---|
| 282 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 283 | if (cessna) |
|---|
| 284 | { |
|---|
| 285 | double s = 30000.0 / cessna->getBound().radius(); |
|---|
| 286 | |
|---|
| 287 | osg::MatrixTransform* scaler = new osg::MatrixTransform; |
|---|
| 288 | scaler->addChild(cessna); |
|---|
| 289 | scaler->setMatrix(osg::Matrixd::scale(s,s,s)*osg::Matrixd::rotate(rotation)); |
|---|
| 290 | scaler->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,osg::StateAttribute::ON); |
|---|
| 291 | |
|---|
| 292 | osg::MatrixTransform* mt = new osg::MatrixTransform; |
|---|
| 293 | mt->addChild(scaler); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | if (!nc) nc = new ModelPositionCallback; |
|---|
| 297 | |
|---|
| 298 | mt->setUpdateCallback(nc); |
|---|
| 299 | |
|---|
| 300 | csn->addChild(mt); |
|---|
| 301 | |
|---|
| 302 | osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator; |
|---|
| 303 | tm->setTrackerMode(trackerMode); |
|---|
| 304 | tm->setRotationMode(rotationMode); |
|---|
| 305 | tm->setTrackNode(scaler); |
|---|
| 306 | |
|---|
| 307 | unsigned int num = viewer.addCameraManipulator(tm); |
|---|
| 308 | viewer.selectCameraManipulator(num); |
|---|
| 309 | } |
|---|
| 310 | else |
|---|
| 311 | { |
|---|
| 312 | std::cout<<"Failed to read cessna.osg"<<std::endl; |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | viewer.realize(); |
|---|
| 320 | |
|---|
| 321 | while( !viewer.done() ) |
|---|
| 322 | { |
|---|
| 323 | |
|---|
| 324 | viewer.sync(); |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | viewer.update(); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | viewer.frame(); |
|---|
| 332 | |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | viewer.sync(); |
|---|
| 337 | |
|---|
| 338 | return 0; |
|---|
| 339 | } |
|---|