1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #include "OSGConfig.h"
40 #include "OSGBaseTypes.h"
41 #include "OSGMatrix.h"
42 #include "OSGMatrixUtility.h"
44 #include "OSGFlyEngine.h"
45 #include "OSGNavigator.h"
47 #include "OSGCamera.h"
48 #include "OSGBackground.h"
52 /***************************************************************************\
54 \***************************************************************************/
56 /*! \class OSG::FlyEngine
57 \ingroup GrpSystemWindowNavigators
59 The FlyEngine models a simple flying navigation model, see \ref
60 PageSystemWindowNavigatorsFly for a description.
64 /*! \var OSG::FlyEngine::_rFrom
66 The from point, i.e. the viewer position.
69 /*! \var OSG::FlyEngine::_rAt
71 The at point, i.e. the target position.
74 /*! \var OSG::FlyEngine::_vUp
79 /*! \var OSG::FlyEngine::_tMatrix
81 The transformation matrix for this navigator.
84 /*------------------------- constructors ----------------------------------*/
86 FlyEngineTransitPtr
FlyEngine::create(void)
88 return FlyEngineTransitPtr(new FlyEngine
);
91 /*------------------------------ get --------------------------------------*/
93 /*! Get the from point.
95 const Pnt3f
&FlyEngine::getFrom(void)
100 /*! Get the at point.
102 const Pnt3f
&FlyEngine::getAt(void)
107 /*! Get the up vector.
109 const Vec3f
&FlyEngine::getUp(void)
114 /*! Get the current transformation matrix.
116 const Matrix
&FlyEngine::getMatrix(void)
118 MatrixLookAt(_tMatrix
, _rFrom
, _rAt
, _vUp
);
123 /*! Not needed by FlyEngine!!!
125 // FIXME: remove getDistance() from NavigatorEngine???
126 Real32
FlyEngine::getDistance(void)
131 /*------------------------------ set --------------------------------------*/
133 /*! Set the from point, the point where the viewer is (i.e the center
134 of all transformations).
136 void FlyEngine::setFrom(Pnt3f new_from
)
141 /*! Sets the target point at which the viewer is looking.
143 void FlyEngine::setAt(Pnt3f new_At
)
148 /*! Sets the up vector, i.e. the direction that point up on the screen.
150 void FlyEngine::setUp(Vec3f new_up
)
155 /*! Set the position and the orientation at once.
157 void FlyEngine::set(Pnt3f new_from
,Pnt3f new_At
,Vec3f new_up
)
164 /*! Set the position and the orientation at once using a matrix.
166 void FlyEngine::set(const Matrix
&new_matrix
)
168 _rFrom
= Pnt3f(new_matrix
[3]);
169 _rAt
= Pnt3f(new_matrix
[3] - new_matrix
[2]);
170 _vUp
= Vec3f(new_matrix
[1]);
172 set(_rFrom
, _rAt
, _vUp
);
175 /*! Moves dist steps forward
177 void FlyEngine::setDistance(Real32 dist
)
182 /*---------------------- navigator engine callbacks ------------------------*/
184 void FlyEngine::buttonPress(Int16 button
, Int16 x
, Int16 y
, Navigator
* nav
)
188 case Navigator::LEFT_MOUSE
:
189 _currentState
= Navigator::TRANSLATING_ZPLUS
;
192 case Navigator::MIDDLE_MOUSE
:
193 _currentState
= Navigator::ROTATING
;
196 case Navigator::RIGHT_MOUSE
:
197 _currentState
= Navigator::TRANSLATING_ZMINUS
;
200 case Navigator::UP_MOUSE
:
201 _currentState
= Navigator::IDLE
;
203 forward(-nav
->getMotionFactor());
207 case Navigator::DOWN_MOUSE
:
208 _currentState
= Navigator::IDLE
;
210 forward(nav
->getMotionFactor());
215 FNOTICE(("FlyEngine: buttonPress, unknown button\n"));
220 void FlyEngine::buttonRelease(Int16 button
, Int16 x
,Int16 y
,Navigator
* nav
)
222 _currentState
= Navigator::IDLE
;
225 void FlyEngine::keyPress(Int16 key
, Int16 x
,Int16 y
,Navigator
* nav
)
229 case Navigator::LEFTROT
:
230 rotate(-nav
->getRotationAngle(), 0);
232 case Navigator::RIGHTROT
:
233 rotate(nav
->getRotationAngle(), 0);
235 case Navigator::LEFT
:
236 right(nav
->getMotionFactor());
238 case Navigator::RIGHT
:
239 right(-nav
->getMotionFactor());
241 case Navigator::FORWARDS
:
242 forward(-nav
->getMotionFactor());
244 case Navigator::BACKWARDS
:
245 forward( nav
->getMotionFactor());
248 FNOTICE(("FlyEngine: keyPress, unknown key\n"));
253 void FlyEngine::moveTo(Int16 x
, Int16 y
, Navigator
*nav
)
255 Real32 fromX
, fromY
, toX
, toY
;
257 nav
->calcFromTo(x
,y
, fromX
,fromY
, toX
,toY
);
259 Real32 distanceX
= -(fromX
- toX
);
260 Real32 distanceY
= (fromY
- toY
);
262 rotate(distanceX
, distanceY
);
264 switch(_currentState
)
266 case Navigator::TRANSLATING_ZPLUS
:
267 forward(-nav
->getMotionFactor());
269 case Navigator::TRANSLATING_ZMINUS
:
270 forward(nav
->getMotionFactor());
272 case Navigator::ROTATING
:
280 void FlyEngine::idle(Int16 buttons
, Int16 x
, Int16 y
, Navigator
*nav
)
284 /*---------------------- Flyer Transformations ----------------------------*/
286 /*! Rotate the viewer \a deltaX around the up axis and deltaY around the
287 left/right axis. \a deltaX and \a deltaY should be between -Pi and Pi.
290 void FlyEngine::rotate(Real32 deltaX
, Real32 deltaY
)
292 // rotate around the up vector
296 q
.setValueAsAxisRad(_vUp
,-deltaX
);
300 final
.setTranslate(_rFrom
);
304 temp
.setTranslate(-_rFrom
[0],-_rFrom
[1],-_rFrom
[2]);
307 final
.mult(_rAt
, _rAt
);
309 // rotate around the side vector
311 Vec3f lv
= _rAt
-_rFrom
;
317 q
.setValueAsAxisRad(sv
,-deltaY
);
321 final
.setTranslate(_rFrom
);
325 temp
.setTranslate(-_rFrom
[0],-_rFrom
[1],-_rFrom
[2]);
328 final
.mult(_rAt
, _rAt
);
331 /*! Flies forward, i.e. translation \a step units along the view vector.
333 Real32
FlyEngine::forward(Real32 step
)
343 transl
.setIdentity();
344 transl
.setTranslate(lv
);
346 transl
.mult(_rAt
, _rAt
);
347 transl
.mult(_rFrom
, _rFrom
);
352 /*! Strafes to the right, i.e. translates along the side vector.
354 Real32
FlyEngine::right(Real32 step
)
362 transl
.setIdentity();
363 transl
.setTranslate(sv
);
364 transl
.mult(_rAt
, _rAt
);
365 transl
.mult(_rFrom
, _rFrom
);
369 /*-------------------------- constructors ---------------------------------*/
371 FlyEngine::FlyEngine(void) :
378 _rFrom
.setValues(0, 0, 0);
379 _rAt
.setValues(0, 0, 1);
380 _vUp
.setValues(0, 1, 0);
382 _tMatrix
.setIdentity();
385 /*-------------------------- destructors ----------------------------------*/
387 FlyEngine::~FlyEngine()