GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_flymover.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  *****************************************************************************/
23 
24 #include "glc_flymover.h"
25 #include "glc_viewport.h"
26 #include "../maths/glc_utils_maths.h"
27 
28 GLC_FlyMover::GLC_FlyMover(GLC_Viewport* pViewport, const QList<GLC_RepMover*>& repsList)
29 : GLC_Mover(pViewport, repsList)
30 , m_TurnRate(glc::toRadian(6))
31 , m_TimerId(0)
32 , m_TimerInterval(66)
33 , m_Velocity(1.0)
34 {
37 }
38 
40 : GLC_Mover(flyMover)
41 , m_TurnRate(flyMover.m_TurnRate)
42 , m_TimerId(0)
43 , m_TimerInterval(flyMover.m_TimerInterval)
44 , m_Velocity(flyMover.m_Velocity)
45 {
46 
47 }
48 
50 {
51  if (0 != m_TimerId)
52  {
53  QObject::killTimer(m_TimerId);
54  }
55 }
56 
58 //Get Functions
60 
62 {
63  return new GLC_FlyMover(*this);
64 }
65 
66 
68 //Set Functions
70 
71 void GLC_FlyMover::init(const GLC_UserInput& userInput)
72 {
73  m_PreviousVector= mapForFlying(static_cast<double>(userInput.x()), static_cast<double>(userInput.y()));
74  GLC_Point3d point= m_pViewport->unProject(userInput.x(), userInput.y());
75  const double distance= (point - m_pViewport->cameraHandle()->eye()).length();
77  // 5 secondes to travel
78  m_Velocity= distance / 5000;
79 
81 
82  // Start the timer
83  m_TimerId= QObject::startTimer(m_TimerInterval);
84 }
85 
86 bool GLC_FlyMover::move(const GLC_UserInput& userInput)
87 {
88  m_PreviousVector= mapForFlying(static_cast<double>(userInput.x()), static_cast<double>(userInput.y()));
89  GLC_Point3d point= m_pViewport->unProject(userInput.x(), userInput.y());
90  const double distance= (point - m_pViewport->cameraHandle()->eye()).length();
92 
93  return false;
94 }
96 {
97  QObject::killTimer(m_TimerId);
98  m_TimerId= 0;
99 }
100 
101 void GLC_FlyMover::setFlyingVelocity(double velocity)
102 {
103  m_Velocity= velocity;
105 }
106 
108 {
109  m_Velocity*= factor;
111 }
112 
113 void GLC_FlyMover::timerEvent(QTimerEvent*)
114 {
115  fly();
116  GLC_Vector3d direction(m_pViewport->cameraHandle()->forward());
117  direction.normalize();
118  direction= direction * m_Velocity * m_TimerInterval;
119  const GLC_Matrix4x4 translation(direction);
120  m_pViewport->cameraHandle()->move(translation);
121 
122  emit updated();
123 }
124 
126 {
127  double AspectRatio;
128  const double winHSize= static_cast<double>(GLC_Mover::m_pViewport->viewHSize());
129  const double winVSize= static_cast<double>(GLC_Mover::m_pViewport->viewVSize());
130 
131 
132  // Change origin and cover
133  if (winHSize < winVSize)
134  {
135  AspectRatio= winVSize / winHSize;
136  x= ( (x - winHSize / 2.0 ) / ( winHSize / 2.0) ) / AspectRatio;
137  y= ( ( winVSize / 2.0 - y) / ( winVSize / 2.0 ) );
138  }
139  else
140  {
141  AspectRatio= winHSize / winVSize;
142  x= ( (x - winHSize / 2.0 ) / ( winHSize / 2.0) );
143  y= ( (winVSize / 2.0 - y) / (winVSize / 2.0 ) ) / AspectRatio;
144  }
145 
146  GLC_Vector3d pos(x, y, 0.0);
147 
148  if (pos.length() > 1.0)
149  {
150  pos.normalize();
151  }
154 
155  double z= -cos(m_TurnRate) / sin(m_TurnRate);
156  pos.setZ(z);
157  pos.normalize();
158  return pos;
159 }
161 {
162  const GLC_Matrix4x4 viewMatrix(m_pViewport->cameraHandle()->viewMatrix());
163  const GLC_Vector3d newPos= viewMatrix.inverted() * m_PreviousVector;
165 
166  // Compute rotation matrix
167  const GLC_Vector3d axe(forward ^ newPos);
168  if (!axe.isNull())
169  {
170  const double angle= acos(forward * newPos);
171  const GLC_Matrix4x4 rotation(axe, angle);
172  const GLC_Matrix4x4 trans1(-m_pViewport->cameraHandle()->eye());
173  const GLC_Matrix4x4 trans2(m_pViewport->cameraHandle()->eye());
174  const GLC_Matrix4x4 composition(trans2 * rotation * trans1);
175  m_pViewport->cameraHandle()->move(composition);
176  }
177 }

©2005-2013 Laurent Ribon