OSGNavigator.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000,2001 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038
00039 #include "OSGConfig.h"
00040 #include "OSGBaseTypes.h"
00041 #include "OSGMatrix.h"
00042 #include "OSGMatrixUtility.h"
00043 #include "OSGCamera.h"
00044 #include "OSGTransform.h"
00045
00046 #include "OSGNavigator.h"
00047
00048 OSG_USING_NAMESPACE
00049
00050 /***************************************************************************\
00051  *                            Description                                  *
00052 \***************************************************************************/
00053
00141 /*------------------------- constructors ----------------------------------*/
00142
00143 Navigator::Navigator() :
00144     _engine(NULL), // pointer to current engine
00145
00146     _trackballEngine(TrackballEngine::create()),
00147     _flyEngine      (FlyEngine      ::create()),
00148     _walkEngine     (WalkEngine     ::create()),
00149     _noneEngine     (NoneEngine     ::create()),
00150     _userEngine     (TrackballEngine::create()),
00151
00152     _rRotationAngle(0.04908739f),
00153     _rMotionFactor(1.f),
00154     _absolute(true),
00155     _vp(NULL),
00156     _cartN(NULL),
00157     _moved(false),
00158     _clickCenter(true),
00159     _clickNoIntersect(false),
00160     _lastX(0),
00161     _lastY(0)
00162 {
00163     setMode(TRACKBALL); // use trackball as default
00164 }
00165
00166 /*-------------------------- destructors ----------------------------------*/
00167
00168 Navigator::~Navigator()
00169 {
00170     _cartN = NULL;
00171     _vp    = NULL;
00172
00173     _engine          = NULL;
00174     _trackballEngine = NULL;
00175     _flyEngine       = NULL;
00176     _walkEngine      = NULL;
00177     _noneEngine      = NULL;
00178     _userEngine      = NULL;
00179 }
00180
00181 /*-------------------------- Notificators ---------------------------------*/
00182
00185 void Navigator::buttonPress(Int16 button, Int16 x, Int16 y)
00186 {
00187     _lastX = x; _lastY = y;
00188     _moved = false;
00189
00190     _engine->buttonPress(button, x, y, this);
00191 }
00192
00195 void Navigator::buttonRelease(Int16 button, Int16 x, Int16 y)
00196 {
00197     _engine->buttonRelease(button, x, y, this);
00198 }
00199
00202 void Navigator::keyPress(Int16 key, Int16 x, Int16 y)
00203 {
00204     _engine->keyPress(key, x, y, this);
00205 }
00206
00209 void Navigator::moveTo(Int16 x, Int16 y)
00210 {
00211     _moved = true;
00212
00213     Real32 fromX, fromY, toX, toY;
00214     if (!calcFromTo(x, y, fromX, fromY, toX, toY)) return;
00215
00216     _engine->moveTo(x, y, this);
00217
00218     _lastX = x;
00219     _lastY = y;
00220 }
00221
00224 void Navigator::idle(Int16 buttons, Int16 x, Int16 y)
00225 {
00226     _engine->idle(buttons, x, y, this);
00227 }
00228
00232 void Navigator::updateCameraTransformation()
00233 {
00234     theMatrix.setIdentity();
00235     if(_absolute && _cartN != NULL && _cartN->getParent() != NULL)
00236     {
00237         _cartN->getParent()->getToWorld(theMatrix);
00238         theMatrix.inverse(theMatrix);
00239     }
00240
00241     _engine->onUpdateCameraTransformation(this);
00242     theMatrix.mult(_engine->getMatrix());
00243
00244     if(_cartN != NULL)
00245     {
00246         Transform *t = dynamic_cast<Transform *>(_cartN->getCore());
00247         if(t == NULL)
00248         {
00249             FWARNING (("Navigator: updateCamTrans, core is not TransformPtr\n"));
00250         }
00251         else
00252         {
00253             if(t->getMatrix() != theMatrix)
00254             {
00255                 t->setMatrix(theMatrix);
00256             }
00257         }
00258     }
00259     else
00260     {
00261         FFATAL (("!_cartN in Navigator::updateCameraTrans\n"));
00262     }
00263 }
00264
00265 /*------------------------------ set --------------------------------------*/
00266
00269 void Navigator::setMode(Navigator::Mode new_mode, bool copyViewParams)
00270 {
00271     NavigatorEngine* engine = _trackballEngine;
00272
00273     switch (new_mode) {
00274         case TRACKBALL: engine = _trackballEngine; break;
00275         case FLY:       engine = _flyEngine;       break;
00276         case WALK:      engine = _walkEngine;      break;
00277         case NONE:      engine = _noneEngine;      break;
00278         case USER:      engine = _userEngine;      break;
00279         default:
00280             FWARNING (("Navigator: unknown mode. Fallback to trackball.\n"));
00281     }
00282
00283     assert(engine);
00284
00285     if (engine != _engine)
00286     {
00287         if (copyViewParams && _engine)
00288             engine->set(_engine->getFrom(),_engine->getAt(),_engine->getUp());
00289
00290         _engine = engine;
00291         _engine->onActivation(this);
00292     }
00293 }
00294
00297 void Navigator::setRotationAngle(Real32 new_angle)
00298 {
00299     _rRotationAngle = new_angle;
00300 }
00301
00304 void Navigator::setMotionFactor(Real32 new_factor)
00305 {
00306     _rMotionFactor = new_factor;
00307 }
00308
00311 void Navigator::setViewport(Viewport *new_viewport)
00312 {
00313     _vp = new_viewport;
00314     _engine->onViewportChanged(this);
00315 }
00316
00319 void Navigator::setFrom(Pnt3f new_from)
00320 {
00321     _engine->setFrom(new_from);
00322 }
00323
00326 void Navigator::setAt(Pnt3f new_at)
00327 {
00328     _engine->setAt(new_at);
00329 }
00330
00333 void Navigator::setDistance(Real32 new_distance)
00334 {
00335     _engine->setDistance(new_distance);
00336 }
00337
00338
00341 void Navigator::setUp(Vec3f new_up)
00342 {
00343     _engine->setUp(new_up);
00344 }
00345
00348 void Navigator::set(Pnt3f new_from, Pnt3f new_at, Vec3f new_up)
00349 {
00350     _engine->set(new_from, new_at, new_up);
00351 }
00352
00355 void Navigator::set(const Matrix & new_matrix)
00356 {
00357     _engine->set(new_matrix);
00358 }
00359
00360
00363 void Navigator::setCameraTransformation(Node * const new_cartn)
00364 {
00365     if (new_cartn == NULL)
00366     {
00367         FWARNING (("Set _cartN in Navigator to NULL\n"));
00368     }
00369
00370     _cartN = new_cartn;
00371 }
00372
00373 /*------------------------------ get --------------------------------------*/
00374
00377 const Matrix &Navigator::getMatrix(void)
00378 {
00379     return _engine->getMatrix();
00380 }
00381
00384 const Pnt3f  &Navigator::getFrom(void)
00385 {
00386     return _engine->getFrom();
00387 }
00388
00391 const Pnt3f  &Navigator::getAt(void)
00392 {
00393     return _engine->getAt();
00394 }
00395
00398 const Vec3f  &Navigator::getUp(void)
00399 {
00400     return _engine->getUp();
00401 }
00402
00403
00406 Real32 Navigator::getDistance(void)
00407 {
00408     return _engine->getDistance();
00409 }
00410
00413 Navigator::State Navigator::getState(void)
00414 {
00415     return _engine->getState();
00416 }
00417
00420 Navigator::Mode Navigator::getMode(void)
00421 {
00422     if (_engine == _trackballEngine) return TRACKBALL;
00423     if (_engine == _flyEngine)       return FLY;
00424     if (_engine == _walkEngine)      return WALK;
00425     if (_engine == _noneEngine)      return NONE;
00426
00427     return USER;
00428 }
00429
00432 Real32 Navigator::getRotationAngle(void)
00433 {
00434     return _rRotationAngle;
00435 }
00436
00439 Real32 Navigator::getMotionFactor(void)
00440 {
00441     return _rMotionFactor;
00442 }
00443
00446 bool Navigator::getAbsolute(void)
00447 {
00448     return _absolute;
00449 }
00450
00453 bool Navigator::getClickCenter(void)
00454 {
00455     return _clickCenter;
00456 }
00457
00460 bool Navigator::getClickNoIntersect(void)
00461 {
00462     return _clickNoIntersect;
00463 }
00464
00465
00466 bool Navigator::getMoved(void)
00467 {
00468     return _moved;
00469 }
00470
00471 Viewport *Navigator::getViewport(void)
00472 {
00473     return _vp;
00474 }
00475
00476 Int16 Navigator::getLastX(void)
00477 {
00478     return _lastX;
00479 }
00480
00481 Int16 Navigator::getLastY(void)
00482 {
00483     return _lastY;
00484 }
00485
00486 TrackballEngine& Navigator::getTrackballEngine(void)
00487 {
00488     return *_trackballEngine;
00489 }
00490
00491 FlyEngine& Navigator::getFlyEngine(void)
00492 {
00493     return *_flyEngine;
00494 }
00495
00496 WalkEngine& Navigator::getWalkEngine(void)
00497 {
00498     return *_walkEngine;
00499 }
00500
00501 NoneEngine &Navigator::getNoneEngine(void)
00502 {
00503     return *_noneEngine;
00504 }
00505
00506 NavigatorEngine& Navigator::getUserEngine(void)
00507 {
00508     return *_userEngine;
00509 }
00510
00511 void Navigator::setUserEngine(NavigatorEngine* userEngine)
00512 {
00513     if (userEngine == NULL) return;
00514
00515     if (userEngine != _userEngine)
00516     {
00517         _userEngine = userEngine;
00518     }
00519
00520     if (getMode() == USER) setMode(USER); // assign userEngine to _engine
00521 }
00522
00523
00526 bool Navigator::setClickCenter(bool state)
00527 {
00528     bool old = _clickCenter;
00529
00530     _clickCenter = state;
00531     return old;
00532 }
00533
00536 bool Navigator::setAbsolute(bool state)
00537 {
00538     bool old = _absolute;
00539
00540     _absolute = state;
00541     return old;
00542 }
00543
00546 bool Navigator::setClickNoIntersect(bool state)
00547 {
00548     bool old = _clickNoIntersect;
00549
00550     _clickNoIntersect = state;
00551     return old;
00552 }
00553
00554 bool Navigator::calcFromTo(Int16   x,     Int16   y,
00555                            Real32& fromX, Real32& fromY,
00556                            Real32& toX,   Real32& toY)
00557 {
00558     Real32 width  = Real32(_vp->getPixelWidth());
00559     Real32 height = Real32(_vp->getPixelHeight());
00560
00561     if(width <= 0 || height <= 0) return false;
00562
00563     Window *par = _vp->getParent();
00564     Real32 winHeight;
00565
00566     if(par != NULL)
00567         winHeight = Real32(par->getHeight());
00568     else
00569         winHeight = height;
00570
00571     fromX = (2.0f * (_lastX - _vp->getPixelLeft())- width) /  width;
00572     fromY = (2.0f * (winHeight-_lastY-_vp->getPixelBottom())-height) / height;
00573
00574     toX   = (2.0f * (x - _vp->getPixelLeft()) - width) / width;
00575     toY   = (2.0f * (winHeight - y - _vp->getPixelBottom()) - height) / height;
00576
00577     return true;
00578 }
00579