| 1 | #ifdef WIN32 |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #pragma warning(disable : 4103 4114 4201 4237 4251 4275 4290 4503 4335 4786) |
|---|
| 16 | |
|---|
| 17 | #endif // WIN32 |
|---|
| 18 | |
|---|
| 19 | #include <osgViewer/Viewer> |
|---|
| 20 | #include <osgViewer/StatsHandler> |
|---|
| 21 | #include <osgViewer/HelpHandler> |
|---|
| 22 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 23 | |
|---|
| 24 | #include <osg/Group> |
|---|
| 25 | #include <osg/Geode> |
|---|
| 26 | #include <osg/ShapeDrawable> |
|---|
| 27 | #include <osg/Texture2D> |
|---|
| 28 | #include <osg/PositionAttitudeTransform> |
|---|
| 29 | #include <osg/MatrixTransform> |
|---|
| 30 | #include <osg/CoordinateSystemNode> |
|---|
| 31 | |
|---|
| 32 | #include <osgDB/FileUtils> |
|---|
| 33 | #include <osgDB/ReadFile> |
|---|
| 34 | |
|---|
| 35 | #include <osgText/Text> |
|---|
| 36 | |
|---|
| 37 | #include <osg/CoordinateSystemNode> |
|---|
| 38 | |
|---|
| 39 | #include <osgSim/OverlayNode> |
|---|
| 40 | #include <osgSim/SphereSegment> |
|---|
| 41 | |
|---|
| 42 | #include <osgGA/NodeTrackerManipulator> |
|---|
| 43 | #include <osgGA/StateSetManipulator> |
|---|
| 44 | |
|---|
| 45 | #include <iostream> |
|---|
| 46 | |
|---|
| 47 | osg::Node* createEarth() |
|---|
| 48 | { |
|---|
| 49 | osg::TessellationHints* hints = new osg::TessellationHints; |
|---|
| 50 | hints->setDetailRatio(5.0f); |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | osg::ShapeDrawable* sd = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0,0.0,0.0), osg::WGS_84_RADIUS_POLAR), hints); |
|---|
| 54 | |
|---|
| 55 | osg::Geode* geode = new osg::Geode; |
|---|
| 56 | geode->addDrawable(sd); |
|---|
| 57 | |
|---|
| 58 | std::string filename = osgDB::findDataFile("Images/land_shallow_topo_2048.jpg"); |
|---|
| 59 | geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile(filename))); |
|---|
| 60 | |
|---|
| 61 | osg::CoordinateSystemNode* csn = new osg::CoordinateSystemNode; |
|---|
| 62 | csn->setEllipsoidModel(new osg::EllipsoidModel()); |
|---|
| 63 | csn->addChild(geode); |
|---|
| 64 | |
|---|
| 65 | return csn; |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | class ModelPositionCallback : public osg::NodeCallback |
|---|
| 71 | { |
|---|
| 72 | public: |
|---|
| 73 | |
|---|
| 74 | ModelPositionCallback(double speed): |
|---|
| 75 | _latitude(0.0), |
|---|
| 76 | _longitude(0.0), |
|---|
| 77 | _height(100000.0), |
|---|
| 78 | _speed(speed) |
|---|
| 79 | { |
|---|
| 80 | _rotation.makeRotate(osg::DegreesToRadians(90.0),0.0,0.0,1.0); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void updateParameters() |
|---|
| 84 | { |
|---|
| 85 | _longitude += _speed * ((2.0*osg::PI)/360.0)/20.0; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 90 | { |
|---|
| 91 | updateParameters(); |
|---|
| 92 | |
|---|
| 93 | osg::NodePath nodePath = nv->getNodePath(); |
|---|
| 94 | |
|---|
| 95 | osg::MatrixTransform* mt = nodePath.empty() ? 0 : dynamic_cast<osg::MatrixTransform*>(nodePath.back()); |
|---|
| 96 | if (mt) |
|---|
| 97 | { |
|---|
| 98 | osg::CoordinateSystemNode* csn = 0; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | unsigned int i; |
|---|
| 102 | for(i=0; i<nodePath.size() && csn==0; ++i) |
|---|
| 103 | { |
|---|
| 104 | csn = dynamic_cast<osg::CoordinateSystemNode*>(nodePath[i]); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | if (csn) |
|---|
| 108 | { |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | osg::EllipsoidModel* ellipsoid = csn->getEllipsoidModel(); |
|---|
| 112 | if (ellipsoid) |
|---|
| 113 | { |
|---|
| 114 | osg::Matrix inheritedMatrix; |
|---|
| 115 | for(i+=1; i<nodePath.size()-1; ++i) |
|---|
| 116 | { |
|---|
| 117 | osg::Transform* transform = nodePath[i]->asTransform(); |
|---|
| 118 | if (transform) transform->computeLocalToWorldMatrix(inheritedMatrix, nv); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | osg::Matrixd matrix(inheritedMatrix); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | ellipsoid->computeLocalToWorldTransformFromLatLongHeight(_latitude,_longitude,_height,matrix); |
|---|
| 125 | matrix.preMult(osg::Matrix::rotate(_rotation)); |
|---|
| 126 | |
|---|
| 127 | mt->setMatrix(matrix); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | traverse(node,nv); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | double _latitude; |
|---|
| 137 | double _longitude; |
|---|
| 138 | double _height; |
|---|
| 139 | osg::Quat _rotation; |
|---|
| 140 | double _speed; |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | class FindNamedNodeVisitor : public osg::NodeVisitor |
|---|
| 145 | { |
|---|
| 146 | public: |
|---|
| 147 | FindNamedNodeVisitor(const std::string& name): |
|---|
| 148 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 149 | _name(name) {} |
|---|
| 150 | |
|---|
| 151 | virtual void apply(osg::Node& node) |
|---|
| 152 | { |
|---|
| 153 | if (node.getName()==_name) |
|---|
| 154 | { |
|---|
| 155 | _foundNodes.push_back(&node); |
|---|
| 156 | } |
|---|
| 157 | traverse(node); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | typedef std::vector< osg::ref_ptr<osg::Node> > NodeList; |
|---|
| 161 | |
|---|
| 162 | std::string _name; |
|---|
| 163 | NodeList _foundNodes; |
|---|
| 164 | }; |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | int main(int argc, char **argv) |
|---|
| 168 | { |
|---|
| 169 | |
|---|
| 170 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker."); |
|---|
| 174 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()); |
|---|
| 175 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | osgViewer::Viewer viewer; |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | viewer.addEventHandler(new osgViewer::ThreadingHandler); |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | viewer.getCamera()->setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES); |
|---|
| 198 | viewer.getCamera()->setNearFarRatio(0.00001f); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | double speed = 1.0; |
|---|
| 202 | while (arguments.read("-f") || arguments.read("--fixed")) speed = 0.0; |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | osg::Quat rotation; |
|---|
| 206 | osg::Vec4 vec4; |
|---|
| 207 | while (arguments.read("--rotate-model",vec4[0],vec4[1],vec4[2],vec4[3])) |
|---|
| 208 | { |
|---|
| 209 | osg::Quat local_rotate; |
|---|
| 210 | local_rotate.makeRotate(osg::DegreesToRadians(vec4[0]),vec4[1],vec4[2],vec4[3]); |
|---|
| 211 | |
|---|
| 212 | rotation = rotation * local_rotate; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | osg::NodeCallback* nc = 0; |
|---|
| 216 | std::string flightpath_filename; |
|---|
| 217 | while (arguments.read("--flight-path",flightpath_filename)) |
|---|
| 218 | { |
|---|
| 219 | std::ifstream fin(flightpath_filename.c_str()); |
|---|
| 220 | if (fin) |
|---|
| 221 | { |
|---|
| 222 | osg::AnimationPath* path = new osg::AnimationPath; |
|---|
| 223 | path->read(fin); |
|---|
| 224 | nc = new osg::AnimationPathCallback(path); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION; |
|---|
| 229 | std::string mode; |
|---|
| 230 | while (arguments.read("--tracker-mode",mode)) |
|---|
| 231 | { |
|---|
| 232 | if (mode=="NODE_CENTER_AND_ROTATION") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION; |
|---|
| 233 | else if (mode=="NODE_CENTER_AND_AZIM") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_AZIM; |
|---|
| 234 | else if (mode=="NODE_CENTER") trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER; |
|---|
| 235 | else |
|---|
| 236 | { |
|---|
| 237 | std::cout<<"Unrecognized --tracker-mode option "<<mode<<", valid options are:"<<std::endl; |
|---|
| 238 | std::cout<<" NODE_CENTER_AND_ROTATION"<<std::endl; |
|---|
| 239 | std::cout<<" NODE_CENTER_AND_AZIM"<<std::endl; |
|---|
| 240 | std::cout<<" NODE_CENTER"<<std::endl; |
|---|
| 241 | return 1; |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL; |
|---|
| 247 | while (arguments.read("--rotation-mode",mode)) |
|---|
| 248 | { |
|---|
| 249 | if (mode=="TRACKBALL") rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL; |
|---|
| 250 | else if (mode=="ELEVATION_AZIM") rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM; |
|---|
| 251 | else |
|---|
| 252 | { |
|---|
| 253 | std::cout<<"Unrecognized --rotation-mode option "<<mode<<", valid options are:"<<std::endl; |
|---|
| 254 | std::cout<<" TRACKBALL"<<std::endl; |
|---|
| 255 | std::cout<<" ELEVATION_AZIM"<<std::endl; |
|---|
| 256 | return 1; |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | bool useOverlay = true; |
|---|
| 261 | while (arguments.read("--no-overlay") || arguments.read("-n")) useOverlay = false; |
|---|
| 262 | |
|---|
| 263 | osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; |
|---|
| 264 | while (arguments.read("--object")) technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; |
|---|
| 265 | while (arguments.read("--ortho") || arguments.read("--orthographic")) technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; |
|---|
| 266 | while (arguments.read("--persp") || arguments.read("--perspective")) technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY; |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 272 | { |
|---|
| 273 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 274 | return 1; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | if (arguments.errors()) |
|---|
| 282 | { |
|---|
| 283 | arguments.writeErrorMessages(std::cout); |
|---|
| 284 | return 1; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | osg::ref_ptr<osg::Node> root = osgDB::readNodeFiles(arguments); |
|---|
| 289 | |
|---|
| 290 | if (!root) root = createEarth(); |
|---|
| 291 | |
|---|
| 292 | if (!root) return 0; |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | viewer.setSceneData(root.get()); |
|---|
| 296 | |
|---|
| 297 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(root.get()); |
|---|
| 298 | if (csn) |
|---|
| 299 | { |
|---|
| 300 | |
|---|
| 301 | osg::ref_ptr<osgSim::OverlayNode> overlayNode; |
|---|
| 302 | if (useOverlay) |
|---|
| 303 | { |
|---|
| 304 | overlayNode = new osgSim::OverlayNode(technique); |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | for(unsigned int i=0; i<csn->getNumChildren(); ++i) |
|---|
| 308 | { |
|---|
| 309 | overlayNode->addChild( csn->getChild(i) ); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | csn->removeChildren(0, csn->getNumChildren()); |
|---|
| 313 | csn->addChild(overlayNode.get()); |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | overlayNode->setContinuousUpdate(true); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 322 | if (cessna) |
|---|
| 323 | { |
|---|
| 324 | double s = 200000.0 / cessna->getBound().radius(); |
|---|
| 325 | |
|---|
| 326 | osg::MatrixTransform* scaler = new osg::MatrixTransform; |
|---|
| 327 | scaler->addChild(cessna); |
|---|
| 328 | scaler->setMatrix(osg::Matrixd::scale(s,s,s)*osg::Matrixd::rotate(rotation)); |
|---|
| 329 | scaler->getOrCreateStateSet()->setMode(GL_RESCALE_NORMAL,osg::StateAttribute::ON); |
|---|
| 330 | |
|---|
| 331 | if (false) |
|---|
| 332 | { |
|---|
| 333 | osgSim::SphereSegment* ss = new osgSim::SphereSegment( |
|---|
| 334 | osg::Vec3(0.0f,0.0f,0.0f), |
|---|
| 335 | 19.9f, |
|---|
| 336 | osg::DegreesToRadians(135.0f), |
|---|
| 337 | osg::DegreesToRadians(240.0f), |
|---|
| 338 | osg::DegreesToRadians(-10.0f), |
|---|
| 339 | osg::DegreesToRadians(30.0f), |
|---|
| 340 | 60); |
|---|
| 341 | |
|---|
| 342 | scaler->addChild(ss); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | osg::MatrixTransform* mt = new osg::MatrixTransform; |
|---|
| 346 | mt->addChild(scaler); |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | if (!nc) nc = new ModelPositionCallback(speed); |
|---|
| 350 | |
|---|
| 351 | mt->setUpdateCallback(nc); |
|---|
| 352 | |
|---|
| 353 | csn->addChild(mt); |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | if (overlayNode.valid()) |
|---|
| 357 | { |
|---|
| 358 | overlayNode->setOverlaySubgraph(mt); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | osgGA::NodeTrackerManipulator* tm = new osgGA::NodeTrackerManipulator; |
|---|
| 362 | tm->setTrackerMode(trackerMode); |
|---|
| 363 | tm->setRotationMode(rotationMode); |
|---|
| 364 | tm->setTrackNode(scaler); |
|---|
| 365 | |
|---|
| 366 | viewer.setCameraManipulator(tm); |
|---|
| 367 | } |
|---|
| 368 | else |
|---|
| 369 | { |
|---|
| 370 | std::cout<<"Failed to read cessna.osg"<<std::endl; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 376 | |
|---|
| 377 | return viewer.run(); |
|---|
| 378 | } |
|---|