Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OGLCamera.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 /*
17  *
18  */
19 
20 #include "OGLCamera.h"
21 #include <qmatrix4x4.h>
22 #include <qvector3d.h>
23 
24 #if _WIN32
25  #include <math.h>
26  #include <windows.h>
27 #endif //_WIN32
28 
29 #ifndef MIN
30  #define MIN(a, b) (((a) < (b)) ? (a) : (b))
31 #endif
32 
33 #ifndef MAX
34  #define MAX(a, b) (((a) > (b)) ? (a) : (b))
35 #endif
36 
37 OGLCamera::OGLCamera(const QVector3D& from, const QVector3D& to, const QVector3D& up, int w, int h,
38  CameraType eCameraType)
39 {
40  m_w = w;
41  m_h = h;
42  setFromToUp(from, to, up);
43  setAllAngleStep(0.1);
44  setModeLock(false, false, false);
45  m_zoomFactor = 1.0;
46  m_eCameraType = eCameraType;
47 
48  switch (m_eCameraType)
49  {
50  case PARALLEL:
51  {
52  m_defaultZoomFactor = 0.29; // le facteur par defaut est pour un monde de 400 de MAX(larg,haut) et
53  // un viewport 700 de MIN(larg,haut)
54  }
55  break;
56  case PERSPECTIVE:
57  {
58  m_defaultZoomFactor = 0.5; // le facteur par defaut est pour un monde de 400 de MAX(larg,haut) et
59  // un viewport 700 de MIN(larg,haut)
60  }
61  break;
62  case FREE:
63  {
64  m_defaultZoomFactor = 1; // le facteur par defaut est pour un monde de 400 de MAX(larg,haut) et un
65  // viewport 700 de MIN(larg,haut)
66  }
67  break;
68  }
69 
70  m_angleAz = 0;
71  m_angleEl = 0;
72  m_angleRo = 0;
73  m_translate = QVector3D(0, 0, 0);
76 }
77 
78 void OGLCamera::setSize(int w, int h)
79 {
80  m_w = w;
81  m_h = h;
83 }
84 
86 {
87  m_eCameraType = eCameraType;
90 }
91 
93 {
94  return m_eCameraType;
95 }
96 
97 void OGLCamera::move(const QVector3D& _direction)
98 {
100  _from += _direction;
101  _to += _direction;
103 }
104 
106 {
108  if (m_eCameraType == FREE)
109  {
112  }
113  else
114  {
115  _from += stepFront;
116  _to += stepFront;
117  }
118 }
119 
121 {
123  if (m_eCameraType == FREE)
124  {
127  }
128  else
129  {
130  _from -= stepFront;
131  _to -= stepFront;
132  }
133 }
134 
136 {
138  if (m_eCameraType == FREE)
139  {
140  m_translate += stepUp;
142  }
143  else
144  {
145  _from += stepUp;
146  _to += stepUp;
147  }
148 }
149 
151 {
153  if (m_eCameraType == FREE)
154  {
155  m_translate -= stepUp;
157  }
158  else
159  {
160  _from -= stepUp;
161  _to -= stepUp;
162  }
163 }
164 
166 {
168  if (m_eCameraType == FREE)
169  {
172  }
173  else
174  {
175  _from += stepLeft;
176  _to += stepLeft;
177  }
178 }
179 
181 {
183  if (m_eCameraType == FREE)
184  {
187  }
188  else
189  {
190  _from -= stepLeft;
191  _to -= stepLeft;
192  }
193 }
194 
195 void OGLCamera::moveUp(NxReal _distance)
196 {
198  _from += _up * _distance;
199  _to += _up * _distance;
200 }
201 
203 {
205  _from -= _up * _distance;
206  _to -= _up * _distance;
207 }
208 
210 {
212  _from += _front * _distance;
213  _to += _front * _distance;
214 }
215 
217 {
219  _from -= _front * _distance;
220  _to -= _front * _distance;
221 }
222 
224 {
226  _from += _left * _distance;
227  _to += _left * _distance;
228 }
229 
231 {
233  _from -= _left * _distance;
234  _to -= _left * _distance;
235 }
236 
237 void OGLCamera::setDistanceStep(NxReal _magnitudeStepUp, NxReal _magnitudeStepFront,
238  NxReal _magnitudeStepLeft)
239 {
241  magnitudeStepUp = _magnitudeStepUp;
244  magnitudeStepFront = _magnitudeStepFront;
247  magnitudeStepLeft = _magnitudeStepLeft;
249 }
250 
252 {
254 }
255 
256 void OGLCamera::setAngleStep(NxReal _stepAngleUpDown, NxReal _stepAngleLeftRight, NxReal _stepAngleSide)
257 {
258  stepAngleUpDown = _stepAngleUpDown;
259  stepAngleLeftRight = _stepAngleLeftRight;
260  stepAngleSide = _stepAngleSide;
261 }
262 
263 const QVector3D& OGLCamera::from() const
264 {
265  return _from;
266 }
267 
268 void OGLCamera::setFrom(const QVector3D& from)
269 {
271  _to += from - _from;
272  _from = from;
274 }
275 
276 const QVector3D& OGLCamera::to() const
277 {
278  return _to;
279 }
280 
281 void OGLCamera::setTo(const QVector3D& to)
282 {
284  _from += to - _to;
285  _to = to;
287 }
288 
289 void OGLCamera::setUp(const QVector3D& up)
290 {
292  _up = up;
294 }
295 
296 void OGLCamera::setFromToUp(const QVector3D& from, const QVector3D& to, const QVector3D& up)
297 {
299  _from = from;
300  _to = to;
301  _up = up.normalized();
302  _front = (_to - _from).normalized();
303  fromTo = _to - _from;
304  _left = QVector3D::crossProduct(_up, _front).normalized();
305 
306  distanceFromTo = fromTo.length();
307  magnitudeStepUp = 1;
308  magnitudeStepFront = 1;
309  magnitudeStepLeft = 1;
311 }
312 
313 const QVector3D& OGLCamera::front() const
314 {
315  return _front;
316 }
317 
319 {
321  fromTo *= _distanceFromTo / distanceFromTo;
322  distanceFromTo = _distanceFromTo;
323  _to = _from + fromTo;
324 }
325 
327 {
329  fromTo *= _distanceFromTo / distanceFromTo;
330  distanceFromTo = _distanceFromTo;
331  _from = _to - fromTo;
332 }
333 
334 void OGLCamera::setModeLock(bool _modeLockUpDown, bool _modeLockLeftRight, bool _modeLockSide)
335 {
336  modeLockUpDown = _modeLockUpDown;
337  modeLockLeftRight = _modeLockLeftRight;
338  modeLockSide = _modeLockSide;
339 }
340 
342 {
343  if (m_eCameraType != FREE)
344  {
345  double dist = _from.distanceToPoint(QVector3D(0, 0, 0));
346  stepLeft = _left * magnitudeStepLeft * ((dist + 1) / 1000);
347  stepFront = _front * magnitudeStepFront * ((dist + 1) / 1000);
348  stepUp = _up * magnitudeStepUp * ((dist + 1) / 1000);
349  }
350  else
351  {
352  flyTo = _to + m_translate;
354  flyFromTo = flyTo - flyFrom;
355  flyUp = _up; // + m_translate;
356  flyFront = QVector3D(flyTo - flyFrom).normalized();
357 
358  flyLeft = QVector3D::crossProduct(flyUp, flyFront).normalized();
359  QMatrix4x4 rotationAzimuth;
360  rotationAzimuth.rotate(m_angleAz, flyUp);
361  flyLeft = rotationAzimuth.mapVector(flyLeft);
362  flyFront = rotationAzimuth.mapVector(flyFront);
363  QMatrix4x4 rotationElevation;
364  rotationElevation.rotate(m_angleEl, flyLeft);
365  flyFront = rotationElevation.mapVector(flyFront);
366 
370  }
371 }
372 
373 void OGLCamera::zoom(double zoomFactor)
374 {
375  if (m_eCameraType != PARALLEL)
376  {
379  if (zoomFactor < 1)
380  {
381  moveFront();
382  }
383  else
384  {
385  moveBack();
386  }
388  }
389  else
390  {
392  m_zoomFactor *= zoomFactor;
393  }
394 }
395 
397 {
399  m_angleAz += (_angle * 500);
401 }
402 
404 {
406  m_angleEl += (_angle * 500);
408 }
409 
411 {
413  m_angleRo += _angle;
415 }
416 
417 void OGLCamera::resetZoom(int w /*= -1*/, int h /*= -1*/)
418 {
421  if ((w <= 0) && (h <= 0))
422  {
424  }
425  else
426  {
427  double minSizeViewport = MIN(m_w, m_h);
428  double maxSizeBoundingBox = MAX(w, h);
429 
430  if (m_eCameraType == PARALLEL)
431  {
432  m_zoomFactor = m_defaultZoomFactor * (maxSizeBoundingBox / 400) * (700 / minSizeViewport);
433  }
434  else
435  {
436  QVector3D fromPersp(
437  0, 1000 * (maxSizeBoundingBox / 400) * (700 / minSizeViewport) * m_defaultZoomFactor,
438  1000 * (maxSizeBoundingBox / 400) * (700 / minSizeViewport) * m_defaultZoomFactor);
439  setFromToUp(fromPersp, _to, _up);
440  }
441  }
442  m_translate = QVector3D(0, 0, 0);
444 }
445 
447 {
449  m_translate = QVector3D(0, 0, 0);
451 }
452 
453 void OGLCamera::setDefaultZoomFactor(double defaultZoomFactor)
454 {
455  m_defaultZoomFactor = defaultZoomFactor;
456 }
457 
459 {
461  m_angleAz = 0;
462  m_angleEl = 0;
463  m_angleRo = 0;
465 }
466 
467 void OGLCamera::setTranslation(double x, double y, double z)
468 {
470  m_translate = QVector3D(x, y, z);
472 }
473 
474 void OGLCamera::getTranslation(double& x, double& y, double& z)
475 {
476  x = m_translate.x();
477  y = m_translate.y();
478  z = m_translate.z();
479 }
480 
481 void OGLCamera::getPosition(double& x, double& y, double& z) const
482 {
483  x = _from.x();
484  y = _from.y();
485  z = _from.z();
486  if (m_angleAz != 0)
487  {
488  double d = _from.z(); // from.magnitude();
489  x = d * sin(m_angleAz * M_PI / 180.0f);
490  z = d * cos(m_angleAz * M_PI / 180.0f);
491  }
492 
493  x += m_translate.x();
494  y += m_translate.y();
495  z += m_translate.z();
496 }
497 
499 {
500  _viewMatrix.setToIdentity();
501  _viewMatrix.lookAt(_from, _to, _up);
502  switch (m_eCameraType)
503  {
504  case PARALLEL:
505  break;
506  case PERSPECTIVE:
507  {
508  // Roll
509  _viewMatrix.rotate(m_angleRo, _front);
510  // Elevation
511  QVector3D elevationVector = QVector3D::crossProduct(_up, fromTo);
512  _viewMatrix.rotate(-m_angleEl, elevationVector); /* orbit the UP axis */
513  // Azhimut
514  _viewMatrix.rotate(-m_angleAz, _up); /* orbit the UP axis */
515  }
516  break;
517  case FREE:
518  {
519  _viewMatrix.translate(-flyFromTo);
520 
521  // Roll
522  _viewMatrix.rotate(m_angleRo, _front);
523  // Elevation
524  QVector3D elevationVector = QVector3D::crossProduct(_up, fromTo);
525  _viewMatrix.rotate(-m_angleEl, elevationVector); /* orbit the UP axis */
526  // Azhimut
527  _viewMatrix.rotate(-m_angleAz, _up); /* orbit the UP axis */
528 
529  _viewMatrix.translate(flyFromTo);
530  _viewMatrix.translate(-m_translate);
531  }
532  break;
533  }
534  _viewMatrixDirty = false;
535 }
536 
538 {
539  _projectionMatrix.setToIdentity();
540  switch (m_eCameraType)
541  {
542  case PARALLEL:
544  m_h * m_zoomFactor, 1, 20000);
545  break;
546  case PERSPECTIVE:
547  _projectionMatrix.perspective(30 * m_zoomFactor, (double)m_w / m_h, 10,
548  50000); // Initialement 1, 50000
549  break;
550  case FREE:
551  _projectionMatrix.perspective(30 * m_zoomFactor, (double)m_w / m_h, 1,
552  5000); // Initialement 1, 50000
553  break;
554  }
555  _projectionMatrixDirty = false;
556 }
557 
558 const QMatrix4x4& OGLCamera::viewMatrix() const
559 {
560  if (_viewMatrixDirty)
561  {
563  }
564  return _viewMatrix;
565 }
566 
567 const QMatrix4x4& OGLCamera::projectionMatrix() const
568 {
570  {
572  }
573  return _projectionMatrix;
574 }
#define NxReal
Definition: NxVec3.h:20
#define MIN(a, b)
Definition: OGLCamera.cpp:30
#define MAX(a, b)
Definition: OGLCamera.cpp:34
CameraType
Definition: OGLCamera.h:40
@ PERSPECTIVE
Definition: OGLCamera.h:42
@ PARALLEL
Definition: OGLCamera.h:41
@ FREE
Definition: OGLCamera.h:43
QVector3D flyFrom
Definition: OGLCamera.h:105
void setTo(const QVector3D &_to)
Definition: OGLCamera.cpp:281
QVector3D flyTo
Definition: OGLCamera.h:105
void setUp(const QVector3D &_up)
Definition: OGLCamera.cpp:289
bool _viewMatrixDirty
Definition: OGLCamera.h:121
double m_angleAz
Definition: OGLCamera.h:116
QVector3D stepUp
Definition: OGLCamera.h:105
void setDefaultZoomFactor(double defaultZoomFactor)
Definition: OGLCamera.cpp:453
void moveDown()
Definition: OGLCamera.cpp:150
void setFromToUp(const QVector3D &_from, const QVector3D &_to, const QVector3D &_up)
Definition: OGLCamera.cpp:296
double m_angleEl
Definition: OGLCamera.h:116
double m_zoomFactor
Definition: OGLCamera.h:116
void setAngleStep(NxReal _stepAngleUpDown, NxReal _stepAngleLeftRight, NxReal _stepAngleSide)
Definition: OGLCamera.cpp:256
void move(const QVector3D &_direction)
Definition: OGLCamera.cpp:97
void elevation(NxReal _angle)
Definition: OGLCamera.cpp:403
void azimuth(NxReal _angle)
Definition: OGLCamera.cpp:396
QMatrix4x4 _projectionMatrix
Definition: OGLCamera.h:124
QVector3D flyFront
Definition: OGLCamera.h:105
void setAllAngleStep(NxReal _angle)
Definition: OGLCamera.cpp:251
double m_angleRo
Definition: OGLCamera.h:116
NxReal stepAngleLeftRight
Definition: OGLCamera.h:106
void roll(NxReal _angle)
Definition: OGLCamera.cpp:410
void setModeLock(bool _modeLockUpDown, bool _modeLockLeftRight, bool _modeLockSide)
Definition: OGLCamera.cpp:334
NxReal magnitudeStepLeft
Definition: OGLCamera.h:113
void updateViewMatrix() const
Definition: OGLCamera.cpp:498
void zoom(double zoomFactor)
Definition: OGLCamera.cpp:373
void getTranslation(double &x, double &y, double &z)
Definition: OGLCamera.cpp:474
void resetTranslation()
Definition: OGLCamera.cpp:446
void moveRight()
Definition: OGLCamera.cpp:180
void invalidateProjectionMatrix() const
Definition: OGLCamera.h:126
const QVector3D & front() const
Definition: OGLCamera.cpp:313
NxReal stepAngleSide
Definition: OGLCamera.h:106
void setFrom(const QVector3D &_from)
Definition: OGLCamera.cpp:268
void setCameraType(CameraType eCameraType)
Definition: OGLCamera.cpp:85
NxReal magnitudeStepUp
Definition: OGLCamera.h:113
void calculateStepVectors()
Definition: OGLCamera.cpp:341
bool modeLockSide
Definition: OGLCamera.h:111
void resetZoom(int w=-1, int h=-1)
Definition: OGLCamera.cpp:417
void resetRotations()
Definition: OGLCamera.cpp:458
void moveLeft()
Definition: OGLCamera.cpp:165
void getPosition(double &x, double &y, double &z) const
Definition: OGLCamera.cpp:481
OGLCamera(const QVector3D &_from, const QVector3D &_to, const QVector3D &_up, int w, int h, CameraType eCameraType)
Definition: OGLCamera.cpp:37
QVector3D stepFront
Definition: OGLCamera.h:105
NxReal stepAngleUpDown
Definition: OGLCamera.h:106
QVector3D flyLeft
Definition: OGLCamera.h:105
QVector3D _front
Definition: OGLCamera.h:103
void setTranslation(double x, double y, double z)
Definition: OGLCamera.cpp:467
QMatrix4x4 _viewMatrix
Definition: OGLCamera.h:123
const QVector3D & from() const
Definition: OGLCamera.cpp:263
QVector3D _from
Definition: OGLCamera.h:99
CameraType cameraType() const
Definition: OGLCamera.cpp:92
CameraType m_eCameraType
Definition: OGLCamera.h:117
void setSize(int w, int h)
Definition: OGLCamera.cpp:78
QVector3D stepLeft
Definition: OGLCamera.h:105
QVector3D flyFromTo
Definition: OGLCamera.h:105
bool modeLockLeftRight
Definition: OGLCamera.h:111
QVector3D m_translate
Definition: OGLCamera.h:118
QVector3D _up
Definition: OGLCamera.h:103
double m_defaultZoomFactor
Definition: OGLCamera.h:116
NxReal distanceFromTo
Definition: OGLCamera.h:108
void setDistanceStep(NxReal _magnitudeStepUp, NxReal _magnitudeStepFront, NxReal _magnitudeStepLeft)
Definition: OGLCamera.cpp:237
NxReal magnitudeStepFront
Definition: OGLCamera.h:113
QVector3D _left
Definition: OGLCamera.h:103
bool _projectionMatrixDirty
Definition: OGLCamera.h:122
QVector3D flyUp
Definition: OGLCamera.h:105
const QVector3D & to() const
Definition: OGLCamera.cpp:276
void setDistanceFromToLockTo(NxReal _distanceFromTo)
Definition: OGLCamera.cpp:326
void setDistanceFromToLockFrom(NxReal _distanceFromTo)
Definition: OGLCamera.cpp:318
const QMatrix4x4 & projectionMatrix() const
Definition: OGLCamera.cpp:567
void updateProjectionMatrix() const
Definition: OGLCamera.cpp:537
void moveBack()
Definition: OGLCamera.cpp:120
void moveFront()
Definition: OGLCamera.cpp:105
const QMatrix4x4 & viewMatrix() const
Definition: OGLCamera.cpp:558
QVector3D _to
Definition: OGLCamera.h:100
QVector3D fromTo
Definition: OGLCamera.h:109
void moveUp()
Definition: OGLCamera.cpp:135
void invalidateViewMatrix() const
Definition: OGLCamera.h:130
bool modeLockUpDown
Definition: OGLCamera.h:111
#define M_PI
Pi.
Definition: color.cpp:25