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