| 1 | #include <osg/Notify> |
|---|
| 2 | #include <osg/BoundingBox> |
|---|
| 3 | #include <osg/PagedLOD> |
|---|
| 4 | #include <osg/Timer> |
|---|
| 5 | #include <osg/MatrixTransform> |
|---|
| 6 | #include <osgUtil/CullVisitor> |
|---|
| 7 | |
|---|
| 8 | #include <iostream> |
|---|
| 9 | #include <vector> |
|---|
| 10 | #include <algorithm> |
|---|
| 11 | #include <stdio.h> |
|---|
| 12 | |
|---|
| 13 | #include "TileMapper.h" |
|---|
| 14 | #include "TXPNode.h" |
|---|
| 15 | #include "TXPPagedLOD.h" |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | using namespace txp; |
|---|
| 20 | using namespace osg; |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | class RetestCallback : public osg::NodeCallback |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | public: |
|---|
| 28 | RetestCallback() |
|---|
| 29 | { |
|---|
| 30 | timer = osg::Timer::instance(); |
|---|
| 31 | prevTime = 0; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | virtual void operator () ( osg::Node * node, osg::NodeVisitor * nv ) |
|---|
| 35 | { |
|---|
| 36 | osg::Group *pLOD = (osg::Group *) node; |
|---|
| 37 | osg::Group *n = NULL; |
|---|
| 38 | if ((pLOD->getNumChildren() > 0) && |
|---|
| 39 | (n = (osg::Group *) pLOD->getChild(0)) && |
|---|
| 40 | (n->getNumChildren() == 0)) |
|---|
| 41 | { |
|---|
| 42 | osg::Timer_t curTime = timer->tick(); |
|---|
| 43 | if ((prevTime + 2.0/timer->getSecondsPerTick() ) < curTime) |
|---|
| 44 | { |
|---|
| 45 | prevTime = curTime; |
|---|
| 46 | pLOD->removeChildren( 0, pLOD->getNumChildren()); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | NodeCallback::traverse( node, nv ); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | protected: |
|---|
| 54 | const osg::Timer* timer; |
|---|
| 55 | osg::Timer_t prevTime; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | #define TXPNodeERROR(s) osg::notify(osg::NOTICE) << "txp::TXPNode::" << (s) << " error: " |
|---|
| 61 | |
|---|
| 62 | TXPNode::TXPNode(): |
|---|
| 63 | osg::Group(), |
|---|
| 64 | _originX(0.0), |
|---|
| 65 | _originY(0.0) |
|---|
| 66 | { |
|---|
| 67 | setNumChildrenRequiringUpdateTraversal(1); |
|---|
| 68 | setCullingActive(false); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | TXPNode::TXPNode(const TXPNode& txpNode,const osg::CopyOp& copyop): |
|---|
| 72 | osg::Group(txpNode,copyop), |
|---|
| 73 | _originX(txpNode._originX), |
|---|
| 74 | _originY(txpNode._originY) |
|---|
| 75 | { |
|---|
| 76 | setNumChildrenRequiringUpdateTraversal(1); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | TXPNode::~TXPNode() |
|---|
| 80 | { |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | TXPArchive* TXPNode::getArchive() |
|---|
| 84 | { |
|---|
| 85 | return _archive.get(); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void TXPNode::traverse(osg::NodeVisitor& nv) |
|---|
| 89 | { |
|---|
| 90 | switch(nv.getVisitorType()) |
|---|
| 91 | { |
|---|
| 92 | case osg::NodeVisitor::CULL_VISITOR: |
|---|
| 93 | { |
|---|
| 94 | |
|---|
| 95 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 96 | |
|---|
| 97 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv); |
|---|
| 98 | if (cv) |
|---|
| 99 | { |
|---|
| 100 | |
|---|
| 101 | #ifdef PRINT_TILEMAPP_TIMEINFO |
|---|
| 102 | const osg::Timer& timer = *osg::Timer::instance(); |
|---|
| 103 | osg::Timer_t start = timer.tick(); |
|---|
| 104 | std::cout<<"Doing visible tile search"<<std::endl; |
|---|
| 105 | #endif // PRINT_TILEMAPP_TIMEINFO |
|---|
| 106 | |
|---|
| 107 | osg::ref_ptr<TileMapper> tileMapper = new TileMapper; |
|---|
| 108 | tileMapper->setLODScale(cv->getLODScale()); |
|---|
| 109 | tileMapper->pushReferenceViewPoint(cv->getReferenceViewPoint()); |
|---|
| 110 | tileMapper->pushViewport(cv->getViewport()); |
|---|
| 111 | tileMapper->pushProjectionMatrix((cv->getProjectionMatrix())); |
|---|
| 112 | tileMapper->pushModelViewMatrix((cv->getModelViewMatrix()), osg::Transform::RELATIVE_RF); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | accept(*tileMapper); |
|---|
| 116 | |
|---|
| 117 | tileMapper->popModelViewMatrix(); |
|---|
| 118 | tileMapper->popProjectionMatrix(); |
|---|
| 119 | tileMapper->popViewport(); |
|---|
| 120 | tileMapper->popReferenceViewPoint(); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | tileMapper->checkValidityOfAllVisibleTiles(); |
|---|
| 125 | |
|---|
| 126 | cv->setUserData(tileMapper.get()); |
|---|
| 127 | |
|---|
| 128 | #ifdef PRINT_TILEMAPP_TIMEINFO |
|---|
| 129 | std::cout<<"Completed visible tile search in "<<timer.delta_m(start,timer.tick())<<std::endl; |
|---|
| 130 | #endif // PRINT_TILEMAPP_TIMEINFO |
|---|
| 131 | |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | updateEye(nv); |
|---|
| 135 | break; |
|---|
| 136 | } |
|---|
| 137 | case osg::NodeVisitor::UPDATE_VISITOR: |
|---|
| 138 | { |
|---|
| 139 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 140 | |
|---|
| 141 | updateSceneGraph(); |
|---|
| 142 | break; |
|---|
| 143 | } |
|---|
| 144 | default: |
|---|
| 145 | break; |
|---|
| 146 | } |
|---|
| 147 | Group::traverse(nv); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | osg::BoundingSphere TXPNode::computeBound() const |
|---|
| 151 | { |
|---|
| 152 | #if 1 |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | return osg::BoundingSphere( _extents ); |
|---|
| 159 | #else |
|---|
| 160 | |
|---|
| 161 | if (getNumChildren() == 0) |
|---|
| 162 | { |
|---|
| 163 | return osg::BoundingSphere( _extents ); |
|---|
| 164 | } |
|---|
| 165 | return Group::computeBound(); |
|---|
| 166 | #endif |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | void TXPNode::setArchiveName(const std::string& archiveName) |
|---|
| 170 | { |
|---|
| 171 | _archiveName = archiveName; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void TXPNode::setOptions(const std::string& options) |
|---|
| 175 | { |
|---|
| 176 | _options = options; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | const std::string& TXPNode::getOptions() const |
|---|
| 180 | { |
|---|
| 181 | return _options; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | const std::string& TXPNode::getArchiveName() const |
|---|
| 185 | { |
|---|
| 186 | return _archiveName; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | bool TXPNode::loadArchive(TXPArchive* archive) |
|---|
| 190 | { |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | if(archive == NULL) |
|---|
| 203 | { |
|---|
| 204 | _archive = new TXPArchive; |
|---|
| 205 | if (_archive->openFile(_archiveName) == false) |
|---|
| 206 | { |
|---|
| 207 | TXPNodeERROR("loadArchive()") << "failed to load archive: \"" << _archiveName << "\"" << std::endl; |
|---|
| 208 | return false; |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | else |
|---|
| 212 | { |
|---|
| 213 | _archive = archive; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | _archive->getOrigin(_originX,_originY); |
|---|
| 217 | _archive->getExtents(_extents); |
|---|
| 218 | |
|---|
| 219 | int32 numLod; |
|---|
| 220 | _archive->GetHeader()->GetNumLods(numLod); |
|---|
| 221 | |
|---|
| 222 | trpg2iPoint tileSize; |
|---|
| 223 | _archive->GetHeader()->GetLodSize(0,tileSize); |
|---|
| 224 | |
|---|
| 225 | _pageManager = new TXPPageManager; |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | _pageManager->Init(_archive.get(), 1); |
|---|
| 230 | |
|---|
| 231 | return true; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | void TXPNode::updateEye(osg::NodeVisitor& nv) |
|---|
| 235 | { |
|---|
| 236 | if (!_pageManager) |
|---|
| 237 | { |
|---|
| 238 | osg::notify(osg::NOTICE)<<"TXPNode::updateEye() no pageManager created"<<std::endl; |
|---|
| 239 | return; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | trpg2dPoint loc; |
|---|
| 243 | loc.x = nv.getEyePoint().x() - _originX; |
|---|
| 244 | loc.y = nv.getEyePoint().y() - _originY; |
|---|
| 245 | |
|---|
| 246 | if (_pageManager->SetLocation(loc)) |
|---|
| 247 | { |
|---|
| 248 | trpgManagedTile *tile=NULL; |
|---|
| 249 | |
|---|
| 250 | while((tile = _pageManager->GetNextUnload())) |
|---|
| 251 | { |
|---|
| 252 | int x,y,lod; |
|---|
| 253 | tile->GetTileLoc(x,y,lod); |
|---|
| 254 | if (lod == 0) |
|---|
| 255 | { |
|---|
| 256 | osg::Node* node = (osg::Node*)(tile->GetLocalData()); |
|---|
| 257 | _nodesToRemove.push_back(node); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | } |
|---|
| 261 | _pageManager->AckUnload(); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | while ((tile = _pageManager->GetNextLoad())) |
|---|
| 265 | { |
|---|
| 266 | int x,y,lod; |
|---|
| 267 | tile->GetTileLoc(x,y,lod); |
|---|
| 268 | if (lod==0) |
|---|
| 269 | { |
|---|
| 270 | osg::Node* node = addPagedLODTile(x,y); |
|---|
| 271 | tile->SetLocalData(node); |
|---|
| 272 | |
|---|
| 273 | } |
|---|
| 274 | _pageManager->AckLoad(); |
|---|
| 275 | |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | osg::Node* TXPNode::addPagedLODTile(int x, int y) |
|---|
| 281 | { |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | int lod = 0; |
|---|
| 286 | char pagedLODfile[1024]; |
|---|
| 287 | sprintf(pagedLODfile,"%s\\tile%d_%dx%d_%d.txp",_archive->getDir(),lod,x,y,_archive->getId()); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | TXPArchive::TileInfo info; |
|---|
| 291 | _archive->getTileInfo(x,y,lod,info); |
|---|
| 292 | |
|---|
| 293 | osg::PagedLOD* pagedLOD = new osg::PagedLOD; |
|---|
| 294 | pagedLOD->setFileName(0,pagedLODfile); |
|---|
| 295 | pagedLOD->setPriorityOffset(0,_archive->getNumLODs()); |
|---|
| 296 | pagedLOD->setPriorityScale(0,1.0f); |
|---|
| 297 | pagedLOD->setRange(0,0.0,info.maxRange); |
|---|
| 298 | pagedLOD->setCenter(info.center); |
|---|
| 299 | pagedLOD->setRadius(info.radius); |
|---|
| 300 | pagedLOD->setNumChildrenThatCannotBeExpired(1); |
|---|
| 301 | pagedLOD->setUpdateCallback(new RetestCallback); |
|---|
| 302 | |
|---|
| 303 | const trpgHeader* header = _archive->GetHeader(); |
|---|
| 304 | trpgHeader::trpgTileType tileType; |
|---|
| 305 | header->GetTileOriginType(tileType); |
|---|
| 306 | if(tileType == trpgHeader::TileLocal) |
|---|
| 307 | { |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | osg::Vec3d sw(info.bbox._min); |
|---|
| 311 | sw[2] = 0.0; |
|---|
| 312 | osg::Matrix offset; |
|---|
| 313 | offset.setTrans(sw); |
|---|
| 314 | osg::MatrixTransform *tform = new osg::MatrixTransform(offset); |
|---|
| 315 | pagedLOD->setCenter(info.center - sw); |
|---|
| 316 | tform->addChild(pagedLOD); |
|---|
| 317 | _nodesToAdd.push_back(tform); |
|---|
| 318 | return tform; |
|---|
| 319 | } |
|---|
| 320 | else |
|---|
| 321 | { |
|---|
| 322 | _nodesToAdd.push_back(pagedLOD); |
|---|
| 323 | |
|---|
| 324 | return pagedLOD; |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | void TXPNode::updateSceneGraph() |
|---|
| 329 | { |
|---|
| 330 | if (!_nodesToRemove.empty()) |
|---|
| 331 | { |
|---|
| 332 | for (unsigned int i = 0; i < _nodesToRemove.size(); i++) |
|---|
| 333 | { |
|---|
| 334 | removeChild(_nodesToRemove[i]); |
|---|
| 335 | } |
|---|
| 336 | _nodesToRemove.clear(); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | if (!_nodesToAdd.empty()) |
|---|
| 340 | { |
|---|
| 341 | for (unsigned int i = 0; i < _nodesToAdd.size(); i++) |
|---|
| 342 | { |
|---|
| 343 | addChild(_nodesToAdd[i]); |
|---|
| 344 | } |
|---|
| 345 | _nodesToAdd.clear(); |
|---|
| 346 | |
|---|
| 347 | } |
|---|
| 348 | } |
|---|
| 349 | |
|---|