| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgGA/TerrainManipulator> |
|---|
| 15 | #include <osgUtil/LineSegmentIntersector> |
|---|
| 16 | #include <osg/io_utils> |
|---|
| 17 | |
|---|
| 18 | using namespace osg; |
|---|
| 19 | using namespace osgGA; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | TerrainManipulator::TerrainManipulator( int flags ) |
|---|
| 25 | : inherited( flags ) |
|---|
| 26 | { |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | TerrainManipulator::TerrainManipulator( const TerrainManipulator& tm, const CopyOp& copyOp ) |
|---|
| 32 | : inherited( tm, copyOp ), |
|---|
| 33 | _previousUp( tm._previousUp ) |
|---|
| 34 | { |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | void TerrainManipulator::setRotationMode( TerrainManipulator::RotationMode mode ) |
|---|
| 42 | { |
|---|
| 43 | setVerticalAxisFixed( mode == ELEVATION_AZIM ); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | TerrainManipulator::RotationMode TerrainManipulator::getRotationMode() const |
|---|
| 49 | { |
|---|
| 50 | return getVerticalAxisFixed() ? ELEVATION_AZIM : ELEVATION_AZIM_ROLL; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | void TerrainManipulator::setNode( Node* node ) |
|---|
| 55 | { |
|---|
| 56 | inherited::setNode( node ); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | if( _flags & UPDATE_MODEL_SIZE ) |
|---|
| 60 | { |
|---|
| 61 | if( _node.valid() ) |
|---|
| 62 | { |
|---|
| 63 | setMinimumDistance( clampBetween( _modelSize * 0.001, 0.00001, 1.0 ) ); |
|---|
| 64 | OSG_INFO << "TerrainManipulator: setting _minimumDistance to " |
|---|
| 65 | << _minimumDistance << std::endl; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | void TerrainManipulator::setByMatrix(const Matrixd& matrix) |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | Vec3d lookVector(- matrix(2,0),-matrix(2,1),-matrix(2,2)); |
|---|
| 75 | Vec3d eye(matrix(3,0),matrix(3,1),matrix(3,2)); |
|---|
| 76 | |
|---|
| 77 | OSG_INFO<<"eye point "<<eye<<std::endl; |
|---|
| 78 | OSG_INFO<<"lookVector "<<lookVector<<std::endl; |
|---|
| 79 | |
|---|
| 80 | if (!_node) |
|---|
| 81 | { |
|---|
| 82 | _center = eye+ lookVector; |
|---|
| 83 | _distance = lookVector.length(); |
|---|
| 84 | _rotation = matrix.getRotate(); |
|---|
| 85 | return; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | const BoundingSphere& bs = _node->getBound(); |
|---|
| 91 | float distance = (eye-bs.center()).length() + _node->getBound().radius(); |
|---|
| 92 | Vec3d start_segment = eye; |
|---|
| 93 | Vec3d end_segment = eye + lookVector*distance; |
|---|
| 94 | |
|---|
| 95 | Vec3d ip; |
|---|
| 96 | bool hitFound = false; |
|---|
| 97 | if (intersect(start_segment, end_segment, ip)) |
|---|
| 98 | { |
|---|
| 99 | OSG_INFO << "Hit terrain ok A"<< std::endl; |
|---|
| 100 | _center = ip; |
|---|
| 101 | |
|---|
| 102 | _distance = (eye-ip).length(); |
|---|
| 103 | |
|---|
| 104 | Matrixd rotation_matrix = Matrixd::translate(0.0,0.0,-_distance)* |
|---|
| 105 | matrix* |
|---|
| 106 | Matrixd::translate(-_center); |
|---|
| 107 | |
|---|
| 108 | _rotation = rotation_matrix.getRotate(); |
|---|
| 109 | |
|---|
| 110 | hitFound = true; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (!hitFound) |
|---|
| 114 | { |
|---|
| 115 | CoordinateFrame eyePointCoordFrame = getCoordinateFrame( eye ); |
|---|
| 116 | |
|---|
| 117 | if (intersect(eye+getUpVector(eyePointCoordFrame)*distance, |
|---|
| 118 | eye-getUpVector(eyePointCoordFrame)*distance, |
|---|
| 119 | ip)) |
|---|
| 120 | { |
|---|
| 121 | _center = ip; |
|---|
| 122 | |
|---|
| 123 | _distance = (eye-ip).length(); |
|---|
| 124 | |
|---|
| 125 | _rotation.set(0,0,0,1); |
|---|
| 126 | |
|---|
| 127 | hitFound = true; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | CoordinateFrame coordinateFrame = getCoordinateFrame(_center); |
|---|
| 133 | _previousUp = getUpVector(coordinateFrame); |
|---|
| 134 | |
|---|
| 135 | clampOrientation(); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | void TerrainManipulator::setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up ) |
|---|
| 140 | { |
|---|
| 141 | if (!_node) return; |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | Vec3d lv(center-eye); |
|---|
| 145 | _distance = lv.length(); |
|---|
| 146 | _center = center; |
|---|
| 147 | |
|---|
| 148 | OSG_INFO << "In compute"<< std::endl; |
|---|
| 149 | |
|---|
| 150 | if (_node.valid()) |
|---|
| 151 | { |
|---|
| 152 | bool hitFound = false; |
|---|
| 153 | |
|---|
| 154 | double distance = lv.length(); |
|---|
| 155 | double maxDistance = distance+2*(eye-_node->getBound().center()).length(); |
|---|
| 156 | Vec3d farPosition = eye+lv*(maxDistance/distance); |
|---|
| 157 | Vec3d endPoint = center; |
|---|
| 158 | for(int i=0; |
|---|
| 159 | !hitFound && i<2; |
|---|
| 160 | ++i, endPoint = farPosition) |
|---|
| 161 | { |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | Vec3d ip; |
|---|
| 165 | if (intersect(eye, endPoint, ip)) |
|---|
| 166 | { |
|---|
| 167 | _center = ip; |
|---|
| 168 | _distance = (ip-eye).length(); |
|---|
| 169 | |
|---|
| 170 | hitFound = true; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | Matrixd rotation_matrix = Matrixd::lookAt(eye,center,up); |
|---|
| 179 | |
|---|
| 180 | _rotation = rotation_matrix.getRotate().inverse(); |
|---|
| 181 | |
|---|
| 182 | CoordinateFrame coordinateFrame = getCoordinateFrame(_center); |
|---|
| 183 | _previousUp = getUpVector(coordinateFrame); |
|---|
| 184 | |
|---|
| 185 | clampOrientation(); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | bool TerrainManipulator::intersect( const Vec3d& start, const Vec3d& end, Vec3d& intersection ) const |
|---|
| 190 | { |
|---|
| 191 | ref_ptr<osgUtil::LineSegmentIntersector> lsi = new osgUtil::LineSegmentIntersector(start,end); |
|---|
| 192 | |
|---|
| 193 | osgUtil::IntersectionVisitor iv(lsi.get()); |
|---|
| 194 | iv.setTraversalMask(_intersectTraversalMask); |
|---|
| 195 | |
|---|
| 196 | _node->accept(iv); |
|---|
| 197 | |
|---|
| 198 | if (lsi->containsIntersections()) |
|---|
| 199 | { |
|---|
| 200 | intersection = lsi->getIntersections().begin()->getWorldIntersectPoint(); |
|---|
| 201 | return true; |
|---|
| 202 | } |
|---|
| 203 | return false; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | bool TerrainManipulator::performMovementMiddleMouseButton( const double eventTimeDelta, const double dx, const double dy ) |
|---|
| 208 | { |
|---|
| 209 | |
|---|
| 210 | double scale = -0.3f * _distance * getThrowScale( eventTimeDelta ); |
|---|
| 211 | |
|---|
| 212 | Matrixd rotation_matrix; |
|---|
| 213 | rotation_matrix.makeRotate(_rotation); |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | Vec3d sideVector = getSideVector(rotation_matrix); |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | Vec3d localUp = _previousUp; |
|---|
| 222 | |
|---|
| 223 | Vec3d forwardVector =localUp^sideVector; |
|---|
| 224 | sideVector = forwardVector^localUp; |
|---|
| 225 | |
|---|
| 226 | forwardVector.normalize(); |
|---|
| 227 | sideVector.normalize(); |
|---|
| 228 | |
|---|
| 229 | Vec3d dv = forwardVector * (dy*scale) + sideVector * (dx*scale); |
|---|
| 230 | |
|---|
| 231 | _center += dv; |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | bool hitFound = false; |
|---|
| 236 | |
|---|
| 237 | if (_node.valid()) |
|---|
| 238 | { |
|---|
| 239 | |
|---|
| 240 | CoordinateFrame coordinateFrame = getCoordinateFrame(_center); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | double distance = _node->getBound().radius()*0.25f; |
|---|
| 244 | |
|---|
| 245 | Vec3d ip1; |
|---|
| 246 | Vec3d ip2; |
|---|
| 247 | bool hit_ip1 = intersect(_center, _center + getUpVector(coordinateFrame) * distance, ip1); |
|---|
| 248 | bool hit_ip2 = intersect(_center, _center - getUpVector(coordinateFrame) * distance, ip2); |
|---|
| 249 | if (hit_ip1) |
|---|
| 250 | { |
|---|
| 251 | if (hit_ip2) |
|---|
| 252 | { |
|---|
| 253 | _center = (_center-ip1).length2() < (_center-ip2).length2() ? |
|---|
| 254 | ip1 : |
|---|
| 255 | ip2; |
|---|
| 256 | |
|---|
| 257 | hitFound = true; |
|---|
| 258 | } |
|---|
| 259 | else |
|---|
| 260 | { |
|---|
| 261 | _center = ip1; |
|---|
| 262 | hitFound = true; |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | else if (hit_ip2) |
|---|
| 266 | { |
|---|
| 267 | _center = ip2; |
|---|
| 268 | hitFound = true; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | if (!hitFound) |
|---|
| 272 | { |
|---|
| 273 | |
|---|
| 274 | OSG_INFO<<"TerrainManipulator unable to intersect with terrain."<<std::endl; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | coordinateFrame = getCoordinateFrame(_center); |
|---|
| 278 | Vec3d new_localUp = getUpVector(coordinateFrame); |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | Quat pan_rotation; |
|---|
| 282 | pan_rotation.makeRotate(localUp,new_localUp); |
|---|
| 283 | |
|---|
| 284 | if (!pan_rotation.zeroRotation()) |
|---|
| 285 | { |
|---|
| 286 | _rotation = _rotation * pan_rotation; |
|---|
| 287 | _previousUp = new_localUp; |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | } |
|---|
| 292 | else |
|---|
| 293 | { |
|---|
| 294 | OSG_INFO<<"New up orientation nearly inline - no need to rotate"<<std::endl; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | return true; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | bool TerrainManipulator::performMovementRightMouseButton( const double eventTimeDelta, const double dx, const double dy ) |
|---|
| 303 | { |
|---|
| 304 | |
|---|
| 305 | zoomModel( dy * getThrowScale( eventTimeDelta ), false ); |
|---|
| 306 | return true; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | void TerrainManipulator::clampOrientation() |
|---|
| 311 | { |
|---|
| 312 | if (!getVerticalAxisFixed()) |
|---|
| 313 | { |
|---|
| 314 | Matrixd rotation_matrix; |
|---|
| 315 | rotation_matrix.makeRotate(_rotation); |
|---|
| 316 | |
|---|
| 317 | Vec3d lookVector = -getUpVector(rotation_matrix); |
|---|
| 318 | Vec3d upVector = getFrontVector(rotation_matrix); |
|---|
| 319 | |
|---|
| 320 | CoordinateFrame coordinateFrame = getCoordinateFrame(_center); |
|---|
| 321 | Vec3d localUp = getUpVector(coordinateFrame); |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | Vec3d sideVector = lookVector ^ localUp; |
|---|
| 325 | |
|---|
| 326 | if (sideVector.length()<0.1) |
|---|
| 327 | { |
|---|
| 328 | OSG_INFO<<"Side vector short "<<sideVector.length()<<std::endl; |
|---|
| 329 | |
|---|
| 330 | sideVector = upVector^localUp; |
|---|
| 331 | sideVector.normalize(); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | Vec3d newUpVector = sideVector^lookVector; |
|---|
| 335 | newUpVector.normalize(); |
|---|
| 336 | |
|---|
| 337 | Quat rotate_roll; |
|---|
| 338 | rotate_roll.makeRotate(upVector,newUpVector); |
|---|
| 339 | |
|---|
| 340 | if (!rotate_roll.zeroRotation()) |
|---|
| 341 | { |
|---|
| 342 | _rotation = _rotation * rotate_roll; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | } |
|---|