| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/AutoTransform> |
|---|
| 14 | #include <osg/CullStack> |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | #include <osg/io_utils> |
|---|
| 17 | |
|---|
| 18 | using namespace osg; |
|---|
| 19 | |
|---|
| 20 | AutoTransform::AutoTransform(): |
|---|
| 21 | _autoUpdateEyeMovementTolerance(0.0), |
|---|
| 22 | _autoRotateMode(NO_ROTATION), |
|---|
| 23 | _autoScaleToScreen(false), |
|---|
| 24 | _scale(1.0,1.0,1.0), |
|---|
| 25 | _firstTimeToInitEyePoint(true), |
|---|
| 26 | _minimumScale(0.0), |
|---|
| 27 | _maximumScale(DBL_MAX), |
|---|
| 28 | _autoScaleTransitionWidthRatio(0.25), |
|---|
| 29 | _matrixDirty(true) |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | AutoTransform::AutoTransform(const AutoTransform& pat,const CopyOp& copyop): |
|---|
| 35 | Transform(pat,copyop), |
|---|
| 36 | _position(pat._position), |
|---|
| 37 | _pivotPoint(pat._pivotPoint), |
|---|
| 38 | _autoUpdateEyeMovementTolerance(pat._autoUpdateEyeMovementTolerance), |
|---|
| 39 | _autoRotateMode(pat._autoRotateMode), |
|---|
| 40 | _autoScaleToScreen(pat._autoScaleToScreen), |
|---|
| 41 | _rotation(pat._rotation), |
|---|
| 42 | _scale(pat._scale), |
|---|
| 43 | _firstTimeToInitEyePoint(true), |
|---|
| 44 | _minimumScale(pat._minimumScale), |
|---|
| 45 | _maximumScale(pat._maximumScale), |
|---|
| 46 | _autoScaleTransitionWidthRatio(pat._autoScaleTransitionWidthRatio), |
|---|
| 47 | _matrixDirty(true) |
|---|
| 48 | { |
|---|
| 49 | |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | void AutoTransform::setScale(const Vec3d& scale) |
|---|
| 53 | { |
|---|
| 54 | _scale = scale; |
|---|
| 55 | if (_scale.x()<_minimumScale) _scale.x() = _minimumScale; |
|---|
| 56 | if (_scale.y()<_minimumScale) _scale.y() = _minimumScale; |
|---|
| 57 | if (_scale.z()<_minimumScale) _scale.z() = _minimumScale; |
|---|
| 58 | |
|---|
| 59 | if (_scale.x()>_maximumScale) _scale.x() = _maximumScale; |
|---|
| 60 | if (_scale.y()>_maximumScale) _scale.y() = _maximumScale; |
|---|
| 61 | if (_scale.z()>_maximumScale) _scale.z() = _maximumScale; |
|---|
| 62 | |
|---|
| 63 | _matrixDirty=true; |
|---|
| 64 | dirtyBound(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | bool AutoTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const |
|---|
| 69 | { |
|---|
| 70 | if (_matrixDirty) computeMatrix(); |
|---|
| 71 | |
|---|
| 72 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 73 | { |
|---|
| 74 | matrix.preMult(_cachedMatrix); |
|---|
| 75 | } |
|---|
| 76 | else |
|---|
| 77 | { |
|---|
| 78 | matrix = _cachedMatrix; |
|---|
| 79 | } |
|---|
| 80 | return true; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | bool AutoTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const |
|---|
| 85 | { |
|---|
| 86 | if (_scale.x() == 0.0 || _scale.y() == 0.0 || _scale.z() == 0.0) |
|---|
| 87 | return false; |
|---|
| 88 | |
|---|
| 89 | if (_referenceFrame==RELATIVE_RF) |
|---|
| 90 | { |
|---|
| 91 | matrix.postMultTranslate(-_position); |
|---|
| 92 | matrix.postMultRotate(_rotation.inverse()); |
|---|
| 93 | matrix.postMultScale(Vec3d(1.0/_scale.x(), 1.0/_scale.y(), 1.0/_scale.z())); |
|---|
| 94 | matrix.postMultTranslate(_pivotPoint); |
|---|
| 95 | } |
|---|
| 96 | else |
|---|
| 97 | { |
|---|
| 98 | matrix.makeRotate(_rotation.inverse()); |
|---|
| 99 | matrix.preMultTranslate(-_position); |
|---|
| 100 | matrix.postMultScale(Vec3d(1.0/_scale.x(), 1.0/_scale.y(), 1.0/_scale.z())); |
|---|
| 101 | matrix.postMultTranslate(_pivotPoint); |
|---|
| 102 | } |
|---|
| 103 | return true; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | void AutoTransform::computeMatrix() const |
|---|
| 107 | { |
|---|
| 108 | if (!_matrixDirty) return; |
|---|
| 109 | |
|---|
| 110 | _cachedMatrix.makeRotate(_rotation); |
|---|
| 111 | _cachedMatrix.postMultTranslate(_position); |
|---|
| 112 | _cachedMatrix.preMultScale(_scale); |
|---|
| 113 | _cachedMatrix.preMultTranslate(-_pivotPoint); |
|---|
| 114 | |
|---|
| 115 | _matrixDirty = false; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void AutoTransform::accept(NodeVisitor& nv) |
|---|
| 119 | { |
|---|
| 120 | if (nv.validNodeMask(*this)) |
|---|
| 121 | { |
|---|
| 122 | |
|---|
| 123 | if (nv.getVisitorType()==NodeVisitor::UPDATE_VISITOR) |
|---|
| 124 | { |
|---|
| 125 | } |
|---|
| 126 | else |
|---|
| 127 | if (nv.getVisitorType()==NodeVisitor::CULL_VISITOR) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | CullStack* cs = dynamic_cast<CullStack*>(&nv); |
|---|
| 131 | if (cs) |
|---|
| 132 | { |
|---|
| 133 | |
|---|
| 134 | Viewport::value_type width = _previousWidth; |
|---|
| 135 | Viewport::value_type height = _previousHeight; |
|---|
| 136 | |
|---|
| 137 | osg::Viewport* viewport = cs->getViewport(); |
|---|
| 138 | if (viewport) |
|---|
| 139 | { |
|---|
| 140 | width = viewport->width(); |
|---|
| 141 | height = viewport->height(); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | osg::Vec3d eyePoint = cs->getEyeLocal(); |
|---|
| 145 | osg::Vec3d localUp = cs->getUpLocal(); |
|---|
| 146 | osg::Vec3d position = getPosition(); |
|---|
| 147 | |
|---|
| 148 | const osg::Matrix& projection = *(cs->getProjectionMatrix()); |
|---|
| 149 | |
|---|
| 150 | bool doUpdate = _firstTimeToInitEyePoint; |
|---|
| 151 | if (!_firstTimeToInitEyePoint) |
|---|
| 152 | { |
|---|
| 153 | osg::Vec3d dv = _previousEyePoint-eyePoint; |
|---|
| 154 | if (dv.length2()>getAutoUpdateEyeMovementTolerance()*(eyePoint-getPosition()).length2()) |
|---|
| 155 | { |
|---|
| 156 | doUpdate = true; |
|---|
| 157 | } |
|---|
| 158 | osg::Vec3d dupv = _previousLocalUp-localUp; |
|---|
| 159 | |
|---|
| 160 | if (_autoRotateMode && |
|---|
| 161 | dupv.length2()>getAutoUpdateEyeMovementTolerance()) |
|---|
| 162 | { |
|---|
| 163 | doUpdate = true; |
|---|
| 164 | } |
|---|
| 165 | else if (width!=_previousWidth || height!=_previousHeight) |
|---|
| 166 | { |
|---|
| 167 | doUpdate = true; |
|---|
| 168 | } |
|---|
| 169 | else if (projection != _previousProjection) |
|---|
| 170 | { |
|---|
| 171 | doUpdate = true; |
|---|
| 172 | } |
|---|
| 173 | else if (position != _previousPosition) |
|---|
| 174 | { |
|---|
| 175 | doUpdate = true; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | _firstTimeToInitEyePoint = false; |
|---|
| 179 | |
|---|
| 180 | if (doUpdate) |
|---|
| 181 | { |
|---|
| 182 | |
|---|
| 183 | if (getAutoScaleToScreen()) |
|---|
| 184 | { |
|---|
| 185 | double size = 1.0/cs->pixelSize(getPosition(),0.48); |
|---|
| 186 | |
|---|
| 187 | if (_autoScaleTransitionWidthRatio>0.0) |
|---|
| 188 | { |
|---|
| 189 | if (_minimumScale>0.0) |
|---|
| 190 | { |
|---|
| 191 | double j = _minimumScale; |
|---|
| 192 | double i = (_maximumScale<DBL_MAX) ? |
|---|
| 193 | _minimumScale+(_maximumScale-_minimumScale)*_autoScaleTransitionWidthRatio : |
|---|
| 194 | _minimumScale*(1.0+_autoScaleTransitionWidthRatio); |
|---|
| 195 | double c = 1.0/(4.0*(i-j)); |
|---|
| 196 | double b = 1.0 - 2.0*c*i; |
|---|
| 197 | double a = j + b*b / (4.0*c); |
|---|
| 198 | double k = -b / (2.0*c); |
|---|
| 199 | |
|---|
| 200 | if (size<k) size = _minimumScale; |
|---|
| 201 | else if (size<i) size = a + b*size + c*(size*size); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | if (_maximumScale<DBL_MAX) |
|---|
| 205 | { |
|---|
| 206 | double n = _maximumScale; |
|---|
| 207 | double m = (_minimumScale>0.0) ? |
|---|
| 208 | _maximumScale+(_minimumScale-_maximumScale)*_autoScaleTransitionWidthRatio : |
|---|
| 209 | _maximumScale*(1.0-_autoScaleTransitionWidthRatio); |
|---|
| 210 | double c = 1.0 / (4.0*(m-n)); |
|---|
| 211 | double b = 1.0 - 2.0*c*m; |
|---|
| 212 | double a = n + b*b/(4.0*c); |
|---|
| 213 | double p = -b / (2.0*c); |
|---|
| 214 | |
|---|
| 215 | if (size>p) size = _maximumScale; |
|---|
| 216 | else if (size>m) size = a + b*size + c*(size*size); |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | setScale(size); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | if (_autoRotateMode==ROTATE_TO_SCREEN) |
|---|
| 224 | { |
|---|
| 225 | osg::Vec3d translation; |
|---|
| 226 | osg::Quat rotation; |
|---|
| 227 | osg::Vec3d scale; |
|---|
| 228 | osg::Quat so; |
|---|
| 229 | |
|---|
| 230 | cs->getModelViewMatrix()->decompose( translation, rotation, scale, so ); |
|---|
| 231 | |
|---|
| 232 | setRotation(rotation.inverse()); |
|---|
| 233 | } |
|---|
| 234 | else if (_autoRotateMode==ROTATE_TO_CAMERA) |
|---|
| 235 | { |
|---|
| 236 | osg::Vec3d PosToEye = _position - eyePoint; |
|---|
| 237 | osg::Matrix lookto = osg::Matrix::lookAt( |
|---|
| 238 | osg::Vec3d(0,0,0), PosToEye, localUp); |
|---|
| 239 | Quat q; |
|---|
| 240 | q.set(osg::Matrix::inverse(lookto)); |
|---|
| 241 | setRotation(q); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | _previousEyePoint = eyePoint; |
|---|
| 245 | _previousLocalUp = localUp; |
|---|
| 246 | _previousWidth = width; |
|---|
| 247 | _previousHeight = height; |
|---|
| 248 | _previousProjection = projection; |
|---|
| 249 | _previousPosition = position; |
|---|
| 250 | |
|---|
| 251 | _matrixDirty = true; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | Transform::accept(nv); |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | BoundingSphere AutoTransform::computeBound() const |
|---|
| 263 | { |
|---|
| 264 | BoundingSphere bsphere; |
|---|
| 265 | |
|---|
| 266 | if ( getAutoScaleToScreen() && _firstTimeToInitEyePoint ) |
|---|
| 267 | return bsphere; |
|---|
| 268 | |
|---|
| 269 | bsphere = Transform::computeBound(); |
|---|
| 270 | |
|---|
| 271 | return bsphere; |
|---|
| 272 | } |
|---|