00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <stdio.h>
00040
00041 #include "OSGTrackball.h"
00042
00043 OSG_USING_NAMESPACE
00044
00045
00046
00047
00048
00049
00058 Trackball::Trackball(Real32 rSize) :
00059 _bSum(false),
00060 _bAutoPosition(false),
00061 _gMode(OSGCamera),
00062 _gTransMode(OSGFixedZ),
00063 _gTransGen (OSGAbsoluteTranslation),
00064 _rAutoPositionStep(1.),
00065 _rAutoPositionIncrement(1.),
00066 _rTrackballSize(rSize),
00067 _rTranslationScale(1.),
00068 _qVal(),
00069 _pVal(),
00070 _qValStart(),
00071 _pValStart(),
00072 _pRotationCenter()
00073 {
00074 }
00075
00076 Trackball::~Trackball(void)
00077 {
00078 }
00079
00080 void Trackball::setSize( Real32 s )
00081 {
00082 _rTrackballSize = s;
00083 }
00084
00085 Real32 Trackball::getSize() const
00086 {
00087 return _rTrackballSize;
00088 }
00089
00090 Real32 Trackball::projectToSphere(Real32 rRadius, Real32 rX, Real32 rY)
00091 {
00092 Real32 d, t, z;
00093
00094 d = sqrt(rX * rX + rY * rY);
00095
00096 if (d < rRadius * 0.70710678118654752440f)
00097 {
00098 z = sqrt(rRadius * rRadius - d * d);
00099 }
00100 else
00101 {
00102 t = rRadius / 1.41421356237309504880f;
00103 z = t * t / d;
00104 }
00105
00106 return z;
00107 }
00108
00109 void Trackball::setSum(bool bVal)
00110 {
00111 _bSum = bVal;
00112 }
00113
00114 void Trackball::updateRotation(Real32 rLastX, Real32 rLastY,
00115 Real32 rCurrentX, Real32 rCurrentY)
00116 {
00117 Quaternion qCurrVal;
00118
00119 Vec3f gAxis;
00120 Real32 rPhi = 0;
00121 Vec3f gP1;
00122 Vec3f gP2;
00123 Vec3f gDiff;
00124 Real32 rTmp;
00125
00126 if( (osgabs(rLastX - rCurrentX) > Eps) ||
00127 (osgabs(rLastY - rCurrentY) > Eps))
00128 {
00129
00130
00131
00132
00133
00134 gP1.setValues( rLastX,
00135 rLastY,
00136 projectToSphere(_rTrackballSize, rLastX, rLastY));
00137
00138 gP2.setValues( rCurrentX,
00139 rCurrentY,
00140 projectToSphere(_rTrackballSize, rCurrentX, rCurrentY));
00141
00142
00143
00144
00145
00146 gAxis = gP2;
00147 gAxis.crossThis(gP1);
00148
00149
00150
00151
00152
00153 gDiff = gP2;
00154 gDiff -= gP1;
00155
00156 rTmp = gDiff.length() / (2.0f * _rTrackballSize);
00157
00158
00159
00160
00161
00162
00163 if(rTmp > 1.0)
00164 rTmp = 1.0;
00165
00166 if(rTmp < -1.0)
00167 rTmp = -1.0;
00168
00169 if(_gMode == OSGObject)
00170 rPhi = (Real32) -2.0 * osgasin(rTmp);
00171 else
00172 rPhi = (Real32) 2.0 * osgasin(rTmp);
00173
00174 }
00175
00176 if(_bSum == false)
00177 {
00178 _qVal.setValueAsAxisRad(gAxis, rPhi);
00179 }
00180 else
00181 {
00182 qCurrVal.setValueAsAxisRad(gAxis, rPhi);
00183 _qVal *= qCurrVal;
00184
00185 }
00186 }
00187
00188 void Trackball::updatePosition(Real32 rLastX, Real32 rLastY,
00189 Real32 rCurrentX, Real32 rCurrentY)
00190 {
00191 if(_gTransMode == OSGFree)
00192 {
00193 if(_gTransGen == OSGAbsoluteTranslation)
00194 {
00195 _pVal[0] += (rLastX - rCurrentX) * -_rTranslationScale;
00196 _pVal[1] += (rLastY - rCurrentY) * -_rTranslationScale;
00197 }
00198 else
00199 {
00200 Vec3f tmpVec((rLastX - rCurrentX) * -_rTranslationScale,
00201 (rLastY - rCurrentY) * -_rTranslationScale,
00202 0.);
00203
00204 Matrix m1;
00205
00206 m1.setRotate(getRotation());
00207
00208 m1.mult(tmpVec);
00209
00210 _pVal[0] += tmpVec[0];
00211 _pVal[1] += tmpVec[1];
00212 _pVal[2] += tmpVec[2];
00213 }
00214 }
00215 }
00216
00217 void Trackball::updatePositionNeg(Real32 , Real32 rLastY,
00218 Real32 , Real32 rCurrentY)
00219 {
00220 if(_gTransMode == OSGFree)
00221 {
00222 if(_gTransGen == OSGAbsoluteTranslation)
00223 {
00224 _pVal[2] += (rLastY - rCurrentY) * -_rTranslationScale;
00225 }
00226 else
00227 {
00228 Vec3f tmpVec(0.,
00229 0.,
00230 (rLastY - rCurrentY) * -_rTranslationScale);
00231
00232 Matrix m1;
00233
00234 m1.setRotate(getRotation());
00235
00236 m1.mult(tmpVec);
00237
00238 _pVal[0] += tmpVec[0];
00239 _pVal[1] += tmpVec[1];
00240 _pVal[2] += tmpVec[2];
00241 }
00242 }
00243 }
00244
00245 void Trackball::setAutoPositionIncrement(Real32 rVal)
00246 {
00247 _rAutoPositionIncrement = rVal;
00248 }
00249
00250 void Trackball::setAutoPosition(bool bVal)
00251 {
00252 _bAutoPosition = bVal;
00253 _rAutoPositionStep = -_rAutoPositionIncrement;
00254 }
00255
00256 void Trackball::setAutoPositionNeg(bool bVal)
00257 {
00258 _bAutoPosition = bVal;
00259 _rAutoPositionStep = _rAutoPositionIncrement;
00260 }
00261
00262 void Trackball::setMode(Mode gMode)
00263 {
00264 _gMode = gMode;
00265 }
00266
00267 void Trackball::setTranslationMode(TranslationMode gMode)
00268 {
00269 _gTransMode = gMode;
00270 }
00271
00272 void Trackball::setTranslationScale(Real32 rTranslationScale)
00273 {
00274 _rTranslationScale = rTranslationScale;
00275 }
00276
00277 void Trackball::setTranslationGen(TranslationGen gMode)
00278 {
00279 if(_gTransGen == OSGAbsoluteTranslation &&
00280 gMode == OSGOrientedTranslation)
00281 {
00282 Matrix tmpMat = getFullTrackballMatrix();
00283
00284 _pVal[0] = tmpMat[3][0];
00285 _pVal[1] = tmpMat[3][1];
00286 _pVal[2] = tmpMat[3][2];
00287 }
00288
00289 _gTransGen = gMode;
00290 }
00291
00292 void Trackball::reset(void)
00293 {
00294 _qVal = _qValStart;
00295 _pVal = _pValStart;
00296 }
00297
00298 void Trackball::setStartPosition(Real32 rX, Real32 rY, Real32 rZ,
00299 bool bUpdate)
00300 {
00301 _pValStart.setValues(rX, rY, rZ);
00302
00303 if(bUpdate == true)
00304 _pVal = _pValStart;
00305 }
00306
00307 void Trackball::setStartPosition(Vec3f &gStartPos, bool bUpdate)
00308 {
00309 _pValStart = gStartPos;
00310
00311 if(bUpdate == true)
00312 _pVal = _pValStart;
00313 }
00314
00315 Vec3f &Trackball::getPosition(void)
00316 {
00317
00318
00319
00320
00321
00322
00323 if(_bAutoPosition == true)
00324 {
00325 switch(_gTransMode)
00326 {
00327 case OSGFixedX:
00328 _pVal[0] += _rAutoPositionStep;
00329 break;
00330
00331 case OSGFixedY:
00332 _pVal[1] += _rAutoPositionStep;
00333 break;
00334
00335 case OSGFixedZ:
00336 _pVal[2] += _rAutoPositionStep;
00337 break;
00338
00339 default:
00340 break;
00341 }
00342 }
00343
00344 return _pVal;
00345 }
00346
00347 void Trackball::setStartRotation(Real32 rX, Real32 rY, Real32 rZ, Real32 rW,
00348 bool bUpdate)
00349 {
00350 _qValStart.setValueAsAxisDeg(rX, rY, rZ, rW);
00351
00352 if(bUpdate == true)
00353 _qVal = _qValStart;
00354 }
00355
00356 void Trackball::setStartRotation(Quaternion &gStartRot, bool bUpdate)
00357 {
00358 _qValStart = gStartRot;
00359
00360 if(bUpdate == true)
00361 _qVal = _qValStart;
00362 }
00363
00364 void Trackball::setRotationCenter(const Pnt3f &pRotationCenter)
00365 {
00366 _pRotationCenter = pRotationCenter;
00367 }
00368
00369 Quaternion &Trackball::getRotation(void)
00370 {
00371 return _qVal;
00372 }
00373
00374 Matrix &Trackball::getFullExamineMatrix(void)
00375 {
00376 OSG::Matrix m1;
00377
00378 _fullMatrix.setIdentity();
00379
00380 _fullMatrix[3][0] = _pVal[0];
00381 _fullMatrix[3][1] = _pVal[1];
00382 _fullMatrix[3][2] = _pVal[2];
00383
00384 m1.setRotate (getRotation());
00385 _fullMatrix.mult(m1);
00386
00387 m1.setIdentity ();
00388 m1.setTranslate(getPosition());
00389
00390 _fullMatrix.mult(m1);
00391
00392 m1.setIdentity();
00393
00394 m1[3][0] = -_pVal[0];
00395 m1[3][1] = -_pVal[1];
00396 m1[3][2] = -_pVal[2];
00397
00398 _fullMatrix.mult(m1);
00399
00400 return _fullMatrix;
00401 }
00402
00403 Matrix &Trackball::getFullTrackballMatrix(void)
00404 {
00405 OSG::Matrix m1;
00406
00407 _fullMatrix.setIdentity();
00408
00409 _fullMatrix[3][0] = _pRotationCenter[0];
00410 _fullMatrix[3][1] = _pRotationCenter[1];
00411 _fullMatrix[3][2] = _pRotationCenter[2];
00412
00413 m1.setRotate (getRotation());
00414 _fullMatrix.mult(m1);
00415
00416 m1.setIdentity ();
00417 m1.setTranslate(getPosition());
00418
00419 _fullMatrix.mult(m1);
00420
00421 m1.setIdentity();
00422
00423 m1[3][0] = -_pRotationCenter[0];
00424 m1[3][1] = -_pRotationCenter[1];
00425 m1[3][2] = -_pRotationCenter[2];
00426
00427 _fullMatrix.mult(m1);
00428
00429 return _fullMatrix;
00430 }
00431
00432
00433