| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #include "TileMapper.h" |
|---|
| 19 | #include "TXPPagedLOD.h" |
|---|
| 20 | |
|---|
| 21 | #include <osg/Material> |
|---|
| 22 | |
|---|
| 23 | using namespace txp; |
|---|
| 24 | |
|---|
| 25 | float TileMapper::getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const |
|---|
| 26 | { |
|---|
| 27 | if (withLODScale) |
|---|
| 28 | return (pos-getEyeLocal()).length()*getLODScale(); |
|---|
| 29 | else |
|---|
| 30 | return (pos-getEyeLocal()).length(); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | inline TileMapper::value_type distance(const osg::Vec3& coord,const osg::Matrix& matrix) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | return -((TileMapper::value_type)coord[0]*(TileMapper::value_type)matrix(0,2)+ |
|---|
| 37 | (TileMapper::value_type)coord[1]*(TileMapper::value_type)matrix(1,2)+ |
|---|
| 38 | (TileMapper::value_type)coord[2]*(TileMapper::value_type)matrix(2,2)+ |
|---|
| 39 | matrix(3,2)); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | float TileMapper::getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScale) const |
|---|
| 43 | { |
|---|
| 44 | const osg::Matrix& matrix = *_modelviewStack.back(); |
|---|
| 45 | float dist = distance(pos,matrix); |
|---|
| 46 | |
|---|
| 47 | if (withLODScale) |
|---|
| 48 | return dist*getLODScale(); |
|---|
| 49 | else |
|---|
| 50 | return dist; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void TileMapper::apply(osg::Node& node) |
|---|
| 54 | { |
|---|
| 55 | if (node.getName()=="TileContent") |
|---|
| 56 | { |
|---|
| 57 | _containsGeode = true; |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | if (isCulled(node)) |
|---|
| 62 | return; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | pushCurrentMask(); |
|---|
| 66 | |
|---|
| 67 | traverse(node); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | popCurrentMask(); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void TileMapper::apply(osg::Group& node) |
|---|
| 74 | { |
|---|
| 75 | if (node.getName()=="TileContent") |
|---|
| 76 | { |
|---|
| 77 | _containsGeode = true; |
|---|
| 78 | return; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | if (isCulled(node)) |
|---|
| 82 | return; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | pushCurrentMask(); |
|---|
| 86 | |
|---|
| 87 | TileIdentifier* tid = dynamic_cast<TileIdentifier*>(node.getUserData()); |
|---|
| 88 | |
|---|
| 89 | if (tid) |
|---|
| 90 | { |
|---|
| 91 | _containsGeode = false; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | traverse(node); |
|---|
| 95 | |
|---|
| 96 | if (tid) |
|---|
| 97 | { |
|---|
| 98 | if (_containsGeode) |
|---|
| 99 | { |
|---|
| 100 | insertTile(*tid); |
|---|
| 101 | |
|---|
| 102 | _containsGeode = false; |
|---|
| 103 | |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | popCurrentMask(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void TileMapper::apply(osg::Geode&) |
|---|
| 113 | { |
|---|
| 114 | _containsGeode = true; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void TileMapper::apply(osg::PagedLOD& node) |
|---|
| 118 | { |
|---|
| 119 | if (isCulled(node)) |
|---|
| 120 | return; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | pushCurrentMask(); |
|---|
| 124 | |
|---|
| 125 | TXPPagedLOD* txpPagedLOD = dynamic_cast<TXPPagedLOD*>(&node); |
|---|
| 126 | if (txpPagedLOD) |
|---|
| 127 | { |
|---|
| 128 | _containsGeode = false; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | traverse(node); |
|---|
| 132 | |
|---|
| 133 | if (txpPagedLOD) |
|---|
| 134 | { |
|---|
| 135 | if (_containsGeode) |
|---|
| 136 | { |
|---|
| 137 | insertTile(txpPagedLOD->_tileIdentifier); |
|---|
| 138 | |
|---|
| 139 | _containsGeode = false; |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | popCurrentMask(); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void TileMapper::insertTile(const TileIdentifier& tid) |
|---|
| 148 | { |
|---|
| 149 | _tileMap.insert(TileMap::value_type(tid,1)); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | bool TileMapper::isTileNeighbourALowerLODLevel(const TileIdentifier& tid, int dx, int dy) const |
|---|
| 154 | { |
|---|
| 155 | if (_tileMap.count(TileIdentifier(tid.x+dx,tid.y+dy,tid.lod))!=0) |
|---|
| 156 | { |
|---|
| 157 | |
|---|
| 158 | return false; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | TileMap::const_iterator itr = _tileMap.find(tid); |
|---|
| 163 | if (itr==_tileMap.end()) |
|---|
| 164 | { |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | #if 0 |
|---|
| 168 | std::cout << "TileMapper::isTileNeighbourALowerLODLevel() Not found tile in map," << std::endl; |
|---|
| 169 | std::cout << " LOD=" << tid.lod << " X=" << tid.x << " Y=" << tid.y << std::endl; |
|---|
| 170 | #endif |
|---|
| 171 | return true; |
|---|
| 172 | } |
|---|
| 173 | TileIdentifier parent_tid(tid.x/2,tid.y/2,tid.lod-1); |
|---|
| 174 | |
|---|
| 175 | bool parentHasNorthNeighour = _tileMap.count(TileIdentifier(parent_tid.x, parent_tid.y+1,parent_tid.lod))!=0; |
|---|
| 176 | bool parentHasEastNeighour = _tileMap.count(TileIdentifier(parent_tid.x+1,parent_tid.y, parent_tid.lod))!=0; |
|---|
| 177 | bool parentHasSouthNeighour = _tileMap.count(TileIdentifier(parent_tid.x, parent_tid.y-1,parent_tid.lod))!=0; |
|---|
| 178 | bool parentHasWestNeighour = _tileMap.count(TileIdentifier(parent_tid.x-1,parent_tid.y, parent_tid.lod))!=0; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | osg::Vec3 delta(tid.x%2,tid.y%2,0); |
|---|
| 183 | |
|---|
| 184 | if (delta.y()>0.0f) |
|---|
| 185 | { |
|---|
| 186 | if (delta.x()>0.0f) |
|---|
| 187 | { |
|---|
| 188 | |
|---|
| 189 | if (dy==1) |
|---|
| 190 | return parentHasNorthNeighour; |
|---|
| 191 | else if (dx==1) |
|---|
| 192 | return parentHasEastNeighour; |
|---|
| 193 | } |
|---|
| 194 | else |
|---|
| 195 | { |
|---|
| 196 | |
|---|
| 197 | if (dy==1) |
|---|
| 198 | return parentHasNorthNeighour; |
|---|
| 199 | else if (dx==-1) |
|---|
| 200 | return parentHasWestNeighour; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | else |
|---|
| 204 | { |
|---|
| 205 | if (delta.x()>0.0f) |
|---|
| 206 | { |
|---|
| 207 | |
|---|
| 208 | if (dy==-1) |
|---|
| 209 | return parentHasSouthNeighour; |
|---|
| 210 | else if (dx==1) |
|---|
| 211 | return parentHasEastNeighour; |
|---|
| 212 | } |
|---|
| 213 | else |
|---|
| 214 | { |
|---|
| 215 | |
|---|
| 216 | if (dy==-1) |
|---|
| 217 | return parentHasSouthNeighour; |
|---|
| 218 | else if (dx==-1) |
|---|
| 219 | return parentHasWestNeighour; |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | return false; |
|---|
| 224 | } |
|---|