fixed: compile issue
[opensg.git] / Source / System / Window / Utilities / OSGFlyEngine.cpp
blob21dc89fe9dbddc233e6528f4b474c167429dd78c
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
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"
46 #include "OSGNode.h"
47 #include "OSGCamera.h"
48 #include "OSGBackground.h"
50 OSG_USING_NAMESPACE
52 /***************************************************************************\
53 * Description *
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
76 The up vector.
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)
97 return _rFrom;
100 /*! Get the at point.
102 const Pnt3f &FlyEngine::getAt(void)
104 return _rAt;
107 /*! Get the up vector.
109 const Vec3f &FlyEngine::getUp(void)
111 return _vUp;
114 /*! Get the current transformation matrix.
116 const Matrix &FlyEngine::getMatrix(void)
118 MatrixLookAt(_tMatrix, _rFrom, _rAt, _vUp);
120 return _tMatrix;
123 /*! Not needed by FlyEngine!!!
125 // FIXME: remove getDistance() from NavigatorEngine???
126 Real32 FlyEngine::getDistance(void)
128 return 0.0f;
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)
138 _rFrom = new_from;
141 /*! Sets the target point at which the viewer is looking.
143 void FlyEngine::setAt(Pnt3f new_At)
145 _rAt = new_At;
148 /*! Sets the up vector, i.e. the direction that point up on the screen.
150 void FlyEngine::setUp(Vec3f new_up)
152 _vUp = new_up;
155 /*! Set the position and the orientation at once.
157 void FlyEngine::set(Pnt3f new_from,Pnt3f new_At,Vec3f new_up)
159 _rFrom = new_from;
160 _rAt = new_At;
161 _vUp = 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)
179 forward(dist);
182 /*---------------------- navigator engine callbacks ------------------------*/
184 void FlyEngine::buttonPress(Int16 button, Int16 x, Int16 y, Navigator* nav)
186 switch (button)
188 case Navigator::LEFT_MOUSE:
189 _currentState = Navigator::TRANSLATING_ZPLUS;
190 break;
192 case Navigator::MIDDLE_MOUSE:
193 _currentState = Navigator::ROTATING;
194 break;
196 case Navigator::RIGHT_MOUSE:
197 _currentState = Navigator::TRANSLATING_ZMINUS;
198 break;
200 case Navigator::UP_MOUSE:
201 _currentState = Navigator::IDLE;
203 forward(-nav->getMotionFactor());
205 break;
207 case Navigator::DOWN_MOUSE:
208 _currentState = Navigator::IDLE;
210 forward(nav->getMotionFactor());
212 break;
214 default:
215 FNOTICE(("FlyEngine: buttonPress, unknown button\n"));
216 break;
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)
227 switch (key)
229 case Navigator::LEFTROT:
230 rotate(-nav->getRotationAngle(), 0);
231 break;
232 case Navigator::RIGHTROT:
233 rotate(nav->getRotationAngle(), 0);
234 break;
235 case Navigator::LEFT:
236 right(nav->getMotionFactor());
237 break;
238 case Navigator::RIGHT:
239 right(-nav->getMotionFactor());
240 break;
241 case Navigator::FORWARDS:
242 forward(-nav->getMotionFactor());
243 break;
244 case Navigator::BACKWARDS:
245 forward( nav->getMotionFactor());
246 break;
247 default:
248 FNOTICE(("FlyEngine: keyPress, unknown key\n"));
249 break;
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());
268 break;
269 case Navigator::TRANSLATING_ZMINUS:
270 forward(nav->getMotionFactor());
271 break;
272 case Navigator::ROTATING:
273 break;
274 default:
275 //IDLE
276 break;
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
293 Matrix final,temp;
294 Quaternion q;
296 q.setValueAsAxisRad(_vUp,-deltaX);
297 q.getValue(temp);
299 final.setIdentity();
300 final.setTranslate(_rFrom);
301 final.mult(temp);
303 temp.setIdentity();
304 temp.setTranslate(-_rFrom[0],-_rFrom[1],-_rFrom[2]);
306 final.mult(temp );
307 final.mult(_rAt, _rAt);
309 // rotate around the side vector
311 Vec3f lv = _rAt-_rFrom;
312 lv.normalize();
314 Vec3f sv = lv;
315 sv.crossThis(_vUp);
316 sv.normalize();
317 q.setValueAsAxisRad(sv,-deltaY);
318 q.getValue(temp);
320 final.setIdentity();
321 final.setTranslate(_rFrom);
322 final.mult(temp);
324 temp.setIdentity();
325 temp.setTranslate(-_rFrom[0],-_rFrom[1],-_rFrom[2]);
327 final.mult(temp);
328 final.mult(_rAt, _rAt);
331 /*! Flies forward, i.e. translation \a step units along the view vector.
333 Real32 FlyEngine::forward(Real32 step)
335 Vec3f lv;
337 lv = _rFrom - _rAt;
338 lv.normalize();
340 lv *= (step);
342 Matrix transl;
343 transl.setIdentity();
344 transl.setTranslate(lv);
346 transl.mult(_rAt, _rAt );
347 transl.mult(_rFrom, _rFrom);
349 return 0.0;
352 /*! Strafes to the right, i.e. translates along the side vector.
354 Real32 FlyEngine::right(Real32 step)
356 Vec3f sv;
357 sv = _rFrom-_rAt;
358 sv.crossThis(_vUp);
359 sv.normalize();
360 sv *= (step);
361 Matrix transl;
362 transl.setIdentity();
363 transl.setTranslate(sv);
364 transl.mult(_rAt, _rAt );
365 transl.mult(_rFrom, _rFrom);
366 return 0.0;
369 /*-------------------------- constructors ---------------------------------*/
371 FlyEngine::FlyEngine(void) :
372 Inherited(),
373 _rFrom (),
374 _rAt (),
375 _vUp (),
376 _tMatrix ()
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()