1// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qcamera.h"
5#include "qcamera_p.h"
6
7#include <QtMath>
8
9QT_BEGIN_NAMESPACE
10
11namespace Qt3DRender {
12
13/*!
14 * \internal
15 */
16QCameraPrivate::QCameraPrivate()
17 : Qt3DCore::QEntityPrivate()
18 , m_position(0.0f, 0.0f, 0.0f)
19 , m_viewCenter(0.0f, 0.0f, -100.0f)
20 , m_upVector(0.0f, 1.0f, 0.0f)
21 , m_cameraToCenter(m_viewCenter - m_position)
22 , m_viewMatrixDirty(false)
23 , m_lens(new QCameraLens())
24 , m_transform(new Qt3DCore::QTransform())
25{
26 updateViewMatrixAndTransform(doEmit: false);
27}
28
29void QCameraPrivate::updateViewMatrixAndTransform(bool doEmit)
30{
31 Q_Q(QCamera);
32
33 const QVector3D viewDirection = (m_viewCenter - m_position).normalized();
34
35 QMatrix4x4 transformMatrix;
36 transformMatrix.translate(vector: m_position);
37
38 // Negative viewDirection because OpenGL convention is looking down -Z
39 transformMatrix.rotate(quaternion: QQuaternion::fromDirection(direction: -viewDirection, up: m_upVector.normalized()));
40
41 m_transform->setMatrix(transformMatrix);
42
43 QMatrix4x4 viewMatrix;
44 viewMatrix.lookAt(eye: m_position, center: m_viewCenter, up: m_upVector);
45 m_viewMatrix = viewMatrix;
46 if (doEmit)
47 emit q->viewMatrixChanged();
48}
49
50/*!
51 * \class Qt3DRender::QCamera
52 * \inheaderfile Qt3DRender/QCamera
53 * \brief The QCamera class defines a view point through which the scene will be
54 * rendered.
55 * \inmodule Qt3DRender
56 * \since 5.5
57 */
58
59/*!
60 * \qmltype Camera
61 * \nativetype Qt3DRender::QCamera
62 * \inherits Entity
63 * \inqmlmodule Qt3D.Render
64 * \since 5.5
65 * \brief Defines a view point through which the scene will be rendered.
66 */
67
68/*!
69 * \enum Qt3DRender::QCamera::CameraTranslationOption
70 *
71 * This enum specifies how camera view center is translated
72 * \value TranslateViewCenter Translate the view center causing the view direction to remain the same
73 * \value DontTranslateViewCenter Don't translate the view center causing the view direction to change
74 */
75
76/*!
77 * \qmlmethod quaternion Qt3D.Render::Camera::tiltRotation(real angle)
78 *
79 * Returns the calculated tilt rotation in relation to the \a angle in degrees taken in
80 * to adjust the camera's tilt or up/down rotation on the X axis.
81 */
82
83/*!
84 * \qmlmethod quaternion Qt3D.Render::Camera::panRotation(real angle)
85 *
86 * Returns the calculated pan rotation in relation to the \a angle in degrees taken in
87 * to adjust the camera's pan or left/right rotation on the Y axis.
88 */
89
90/*!
91 * \qmlmethod quaternion Qt3D.Render::Camera::rollRotation(real angle)
92 *
93 * Returns the calculated roll rotation in relation to the \a angle in degrees taken in
94 * to adjust the camera's roll or lean left/right rotation on the Z axis.
95 */
96
97/*!
98 * \qmlmethod quaternion Qt3D.Render::Camera::rotation(real angle, vector3d axis)
99 *
100 * Returns the calculated rotation in relation to the \a angle in degrees and
101 * chosen \a axis taken in.
102 */
103
104/*!
105 * \qmlmethod void Qt3D.Render::Camera::translate(vector3d vLocal, enumeration option)
106 *
107 * Translates the camera's position and its view vector by \a vLocal in local coordinates.
108 * The \a option allows for toggling whether the view center should be translated.
109 * \list
110 * \li Camera.TranslateViewCenter
111 * \li Camera.DontTranslateViewCenter
112 * \endlist
113 * \sa Qt3DRender::QCamera::CameraTranslationOption
114 */
115
116/*!
117 * \qmlmethod void Qt3D.Render::Camera::translateWorld(vector3d vWorld, enumeration option)
118 *
119 * Translates the camera's position and its view vector by \a vWorld in world coordinates.
120 * The \a option allows for toggling whether the view center should be translated.
121 * \list
122 * \li Camera.TranslateViewCenter
123 * \li Camera.DontTranslateViewCenter
124 * \endlist
125 * \sa Qt3DRender::QCamera::CameraTranslationOption
126 */
127
128/*!
129 * \qmlmethod void Qt3D.Render::Camera::tilt(real angle)
130 *
131 * Adjusts the tilt angle of the camera by \a angle in degrees.
132 */
133
134/*!
135 * \qmlmethod void Qt3D.Render::Camera::pan(real angle)
136 *
137 * Adjusts the pan angle of the camera by \a angle in degrees.
138 */
139
140/*!
141 * \qmlmethod void Qt3D.Render::Camera::pan(real angle, vector3d axis)
142 *
143 * Adjusts the camera pan about view center by \a angle in degrees on \a axis.
144 */
145
146/*!
147 * \qmlmethod void Qt3D.Render::Camera::roll(real angle)
148 *
149 * Adjusts the camera roll by \a angle in degrees.
150 */
151
152/*!
153 * \qmlmethod void Qt3D.Render::Camera::tiltAboutViewCenter(real angle)
154 *
155 * Adjusts the camera tilt about view center by \a angle in degrees.
156 */
157
158/*!
159 * \qmlmethod void Qt3D.Render::Camera::panAboutViewCenter(real angle)
160 *
161 * Adjusts the camera pan about view center by \a angle in degrees.
162 */
163
164/*!
165 * \qmlmethod void Qt3D.Render::Camera::panAboutViewCenter(real angle, vector3d axis)
166 *
167 * Adjusts the camera pan about view center by \a angle in degrees on \a axis.
168 */
169
170/*!
171 * \qmlmethod void Qt3D.Render::Camera::rollAboutViewCenter(real angle)
172 *
173 * Adjusts the camera roll about view center by \a angle in degrees.
174 */
175
176/*!
177 * \qmlmethod void Qt3D.Render::Camera::rotate(quaternion q)
178 *
179 * Rotates the camera with the use of a Quaternion in \a q.
180 */
181
182/*!
183 * \qmlmethod void Qt3D.Render::Camera::rotateAboutViewCenter(quaternion q)
184 *
185 * Rotates the camera about the view center with the use of a Quaternion in \a q.
186 */
187
188/*!
189 * \qmlmethod void Qt3D.Render::Camera::viewAll()
190 *
191 * Rotates and moves the camera so that it's viewCenter is the center of the scene's bounding volume
192 * and the entire scene fits in the view port.
193 *
194 * \note Only works if the lens is in perspective or orthographic projection mode.
195 * \sa Qt3D.Render::Camera::projectionType
196 */
197
198/*!
199 * \qmlmethod void Qt3D.Render::Camera::viewEntity(Entity entity)
200 *
201 * Rotates and moves the camera so that it's viewCenter is the center of the entity's bounding volume
202 * and the entire \a entity fits in the view port.
203 *
204 * \note Only works if the lens is in perspective or orthographic projection mode.
205 * \sa Qt3D.Render::Camera::projectionType
206 */
207
208/*!
209 * \qmlmethod void Qt3D.Render::Camera::viewSphere(vector3d center, real radius)
210 *
211 * Rotates and moves the camera so that it's viewCenter is \a center
212 * and a sphere of \a radius fits in the view port.
213 *
214 * \note Only works if the lens is in perspective or orthographic projection mode.
215 * \sa Qt3D.Render::Camera::projectionType
216 */
217
218/*!
219 * \qmlproperty enumeration Qt3D.Render::Camera::projectionType
220 *
221 * Holds the type of the camera projection. The default value is
222 * CameraLens.PerspectiveProjection.
223 *
224 * \list
225 * \li CameraLens.OrthographicProjection - Parallel lines appear parallel. Objects appear
226 * the same size regardless of distance.
227 * \li CameraLens.PerspectiveProjection - Parallel lines appear to meet in the distance.
228 * Objects appear to shrink the farther they are from the camera.
229 * \li CameraLens.FrustumProjection
230 * \li CameraLens.CustomProjection
231 * \endlist
232 * \sa Qt3DRender::QCameraLens::ProjectionType
233 */
234
235/*!
236 * \qmlproperty real Qt3D.Render::Camera::nearPlane
237 * Holds the current camera near plane of the camera. Objects that
238 * are closer to the camera than the nearPlane will not be rendered.
239 */
240
241/*!
242 * \qmlproperty real Qt3D.Render::Camera::farPlane
243 * Holds the current camera far plane of the camera. Objects that
244 * are farther from the camera than the farPlane will not be rendered.
245 */
246
247/*!
248 * \qmlproperty CameraLens Qt3D.Render::Camera::lens
249 * Holds the CameraLens component of the camera.
250 * \since 5.14
251 */
252
253/*!
254 * \qmlproperty Transform Qt3D.Render::Camera::transform
255 * Holds the Transform component of the camera.
256 * \since 5.14
257 */
258
259/*!
260 * \qmlproperty real Qt3D.Render::Camera::fieldOfView
261 * Holds the current vertical field of view of the camera in degrees.
262 *
263 * Along with \l aspectRatio, this property determines how much of
264 * the scene is visible to the camera. In that respect you might
265 * think of it as analogous to choosing a wide angle (wide horizontal
266 * field of view) or telephoto (narrow horizontal field of view) lens,
267 * depending on how much of a scene you want to capture.
268 *
269 * fieldOfView is only relevant when \l projectionType is
270 * CameraLens.PerspectiveProjection.
271 */
272
273/*!
274 * \qmlproperty real Qt3D.Render::Camera::aspectRatio
275 * Holds the current aspect ratio of the camera.
276 */
277
278/*!
279 *\qmlproperty real Qt3D.Render::Camera::left
280 * Holds the current left of the camera.
281 *
282 * This property is only relevant when \l projectionType is
283 * CameraLens.OrthographicProjection.
284 */
285
286/*!
287 * \qmlproperty real Qt3D.Render::Camera::right
288 * Holds the current right of the camera.
289 *
290 * This property is only relevant when \l projectionType is
291 * CameraLens.OrthographicProjection.
292 */
293
294/*!
295 * \qmlproperty real Qt3D.Render::Camera::bottom
296 * Holds the current bottom of the camera.
297 *
298 * This property is only relevant when \l projectionType is
299 * CameraLens.OrthographicProjection.
300 */
301
302/*!
303 * \qmlproperty real Qt3D.Render::Camera::top
304 * Holds the current top of the camera.
305 *
306 * This property is only relevant when \l projectionType is
307 * CameraLens.OrthographicProjection.
308 */
309
310/*!
311 * \qmlproperty matrix4x4 Qt3D.Render::Camera::projectionMatrix
312 * Holds the current projection matrix of the camera.
313 */
314
315/*!
316 * \qmlproperty real Qt3D.Render::Camera::exposure
317 * Holds the current exposure of the camera.
318 *
319 * The default value is 0.0.
320 *
321 * The MetalRoughMaterial in Qt 3D Extras is currently the only provided
322 * material that makes use of camera exposure. Negative values will cause
323 * the material to be darker, and positive values will cause it to be lighter.
324 *
325 * Custom materials may choose to interpret the value differently.
326 */
327
328/*!
329 * \qmlproperty vector3d Qt3D.Render::Camera::position
330 * Holds the current position of the camera in coordinates relative to
331 * the parent entity.
332 */
333
334/*!
335 * \qmlproperty vector3d Qt3D.Render::Camera::upVector
336 * Holds the current up vector of the camera in coordinates relative to
337 * the parent entity.
338 *
339 * The up vector indicates which direction the top of the camera is
340 * facing. Think of taking a picture: after positioning yourself
341 * and pointing the camera at your target, you might rotate the camera
342 * left or right, giving you a portrait or landscape (or angled!)
343 * shot. upVector allows you to control this type of movement.
344 */
345
346/*!
347 * \qmlproperty vector3d Qt3D.Render::Camera::viewCenter
348 * Holds the current view center of the camera in coordinates relative to
349 * the parent entity.
350 *
351 * Intuitively, the viewCenter is the location the camera is pointing at.
352 */
353
354/*!
355 * \qmlproperty vector3d Qt3D.Render::Camera::viewVector
356 * Holds the camera's view vector in coordinates relative to
357 * the parent entity.
358 *
359 * This vector decribes the displacement from the camera (\l position)
360 * to its target (\l viewCenter).
361 * \readonly
362 */
363
364/*!
365 * \qmlproperty matrix4x4 Qt3D.Render::Camera::viewMatrix
366 * \deprecated
367 * Holds the camera's view matrix in coordinates relative
368 * to the parent entity.
369 * \readonly
370 */
371
372/*!
373 * \property Qt3DRender::QCamera::projectionType
374 *
375 * Holds the type of the camera projection. The default value is
376 * QCameraLens::PerspectiveProjection.
377 *
378 * \list
379 * \li QCameraLens::OrthographicProjection - Parallel lines appear parallel. Objects appear
380 * the same size regardless of distance.
381 * \li QCameraLens::PerspectiveProjection - Parallel lines appear to meet in the distance.
382 * Objects appear to shrink the farther they are from the camera.
383 * \li QCameraLens::FrustumProjection
384 * \li QCameraLens::CustomProjection
385 * \endlist
386 * \sa Qt3DRender::QCameraLens::ProjectionType
387 */
388
389/*!
390 * \property Qt3DRender::QCamera::nearPlane
391 * Holds the current camera near plane. Objects that are closer to the
392 * camera than the nearPlane will not be rendered.
393 */
394
395/*!
396 * \property Qt3DRender::QCamera::farPlane
397 * Holds the current camera far plane. Objects that are farther from the
398 * camera than the farPlane will not be rendered.
399 */
400
401/*!
402 * \property Qt3DRender::QCamera::lens
403 * Holds the Qt3DRender::QCameraLens component of the camera.
404 * \since 5.14
405 */
406
407/*!
408 * \property Qt3DRender::QCamera::transform
409 * Holds the Qt3DCore::QTransform component of the camera.
410 * \since 5.14
411 */
412
413/*!
414 * \property Qt3DRender::QCamera::fieldOfView
415 * Holds the current vertical field of view in degrees.
416 *
417 * Along with \l aspectRatio, this property determines how much of
418 * the scene is visible to the camera. In that respect you might
419 * think of it as analogous to choosing a wide angle (wide horizontal
420 * field of view) or telephoto (narrow horizontal field of view) lens
421 * depending on how much of a scene you want to capture.
422 *
423 * fieldOfView is only relevant when \l projectionType is
424 * QCameraLens::PerspectiveProjection.
425 */
426
427/*!
428 * \property Qt3DRender::QCamera::aspectRatio
429 * Holds the current aspect ratio.
430 */
431
432/*!
433 *\property Qt3DRender::QCamera::left
434 * Holds the current left of the camera.
435 *
436 * This property is only relevant when \l projectionType is
437 * QCameraLens::OrthographicProjection.
438 */
439
440/*!
441 * \property Qt3DRender::QCamera::right
442 * Holds the current right of the camera.
443 *
444 * This property is only relevant when \l projectionType is
445 * QCameraLens::OrthographicProjection.
446 */
447
448/*!
449 * \property Qt3DRender::QCamera::bottom
450 * Holds the current bottom of the camera.
451 *
452 * This property is only relevant when \l projectionType is
453 * QCameraLens::OrthographicProjection.
454 */
455
456/*!
457 * \property Qt3DRender::QCamera::top
458 * Holds the current top of the camera.
459 *
460 * This property is only relevant when \l projectionType is
461 * QCameraLens::OrthographicProjection.
462 */
463
464/*!
465 * \property Qt3DRender::QCamera::projectionMatrix
466 * Holds the current projection matrix of the camera.
467 */
468
469/*!
470 * \property Qt3DRender::QCamera::exposure
471 * Holds the current exposure of the camera.
472 *
473 * The default value is 0.0.
474 *
475 * The MetalRoughMaterial in Qt 3D Extras is currently the only provided
476 * material that makes use of camera exposure. Negative values will cause
477 * the material to be darker, and positive values will cause it to be lighter.
478 *
479 * Custom materials may choose to interpret the value differently.
480 */
481
482/*!
483 * \property Qt3DRender::QCamera::position
484 * Holds the camera's position in coordinates relative to
485 * the parent entity.
486 */
487
488/*!
489 * \property Qt3DRender::QCamera::upVector
490 * Holds the camera's up vector in coordinates relative to
491 * the parent entity.
492 *
493 * The up vector indicates which direction the top of the camera is
494 * facing. Think of taking a picture: after positioning yourself
495 * and pointing the camera at your target, you might rotate the camera
496 * left or right, giving you a portrait or landscape (or angled!)
497 * shot. upVector allows you to control this type of movement.
498 */
499
500/*!
501 * \property Qt3DRender::QCamera::viewCenter
502 * Holds the camera's view center in coordinates relative to
503 * the parent entity.
504 *
505 * Intuitively, the viewCenter is the location the camera is pointing at.
506 */
507
508/*!
509 * \property Qt3DRender::QCamera::viewVector
510 * Holds the camera's view vector in coordinates relative to
511 * the parent entity.
512 *
513 * This vector decribes the displacement from the camera (\l position)
514 * to its target (\l viewCenter).
515 */
516
517/*!
518 * \property Qt3DRender::QCamera::viewMatrix
519 * \deprecated
520 * Holds the camera's view matrix in coordinates relative to
521 * the parent entity.
522 */
523
524/*!
525 * Creates a new QCamera instance with the
526 * specified \a parent.
527 */
528QCamera::QCamera(Qt3DCore::QNode *parent)
529 : Qt3DCore::QEntity(*new QCameraPrivate, parent)
530{
531 QObject::connect(sender: d_func()->m_lens, SIGNAL(projectionTypeChanged(QCameraLens::ProjectionType)), receiver: this, SIGNAL(projectionTypeChanged(QCameraLens::ProjectionType)));
532 QObject::connect(sender: d_func()->m_lens, SIGNAL(nearPlaneChanged(float)), receiver: this, SIGNAL(nearPlaneChanged(float)));
533 QObject::connect(sender: d_func()->m_lens, SIGNAL(farPlaneChanged(float)), receiver: this, SIGNAL(farPlaneChanged(float)));
534 QObject::connect(sender: d_func()->m_lens, SIGNAL(fieldOfViewChanged(float)), receiver: this, SIGNAL(fieldOfViewChanged(float)));
535 QObject::connect(sender: d_func()->m_lens, SIGNAL(aspectRatioChanged(float)), receiver: this, SIGNAL(aspectRatioChanged(float)));
536 QObject::connect(sender: d_func()->m_lens, SIGNAL(leftChanged(float)), receiver: this, SIGNAL(leftChanged(float)));
537 QObject::connect(sender: d_func()->m_lens, SIGNAL(rightChanged(float)), receiver: this, SIGNAL(rightChanged(float)));
538 QObject::connect(sender: d_func()->m_lens, SIGNAL(bottomChanged(float)), receiver: this, SIGNAL(bottomChanged(float)));
539 QObject::connect(sender: d_func()->m_lens, SIGNAL(topChanged(float)), receiver: this, SIGNAL(topChanged(float)));
540 QObject::connect(sender: d_func()->m_lens, SIGNAL(projectionMatrixChanged(QMatrix4x4)),
541 receiver: this, SIGNAL(projectionMatrixChanged(QMatrix4x4)));
542 QObject::connect(sender: d_func()->m_lens, SIGNAL(exposureChanged(float)), receiver: this, SIGNAL(exposureChanged(float)));
543 QObject::connect(sender: d_func()->m_lens, signal: &QCameraLens::viewSphere, context: this, slot: &QCamera::viewSphere);
544
545 addComponent(comp: d_func()->m_lens);
546 addComponent(comp: d_func()->m_transform);
547}
548
549/*!
550 * \internal
551 */
552QCamera::~QCamera()
553{
554}
555
556/*!
557 * \internal
558 */
559QCamera::QCamera(QCameraPrivate &dd, Qt3DCore::QNode *parent)
560 : Qt3DCore::QEntity(dd, parent)
561{
562 QObject::connect(sender: d_func()->m_lens, SIGNAL(projectionTypeChanged(QCameraLens::ProjectionType)), receiver: this, SIGNAL(projectionTypeChanged(QCameraLens::ProjectionType)));
563 QObject::connect(sender: d_func()->m_lens, SIGNAL(nearPlaneChanged(float)), receiver: this, SIGNAL(nearPlaneChanged(float)));
564 QObject::connect(sender: d_func()->m_lens, SIGNAL(farPlaneChanged(float)), receiver: this, SIGNAL(farPlaneChanged(float)));
565 QObject::connect(sender: d_func()->m_lens, SIGNAL(fieldOfViewChanged(float)), receiver: this, SIGNAL(fieldOfViewChanged(float)));
566 QObject::connect(sender: d_func()->m_lens, SIGNAL(aspectRatioChanged(float)), receiver: this, SIGNAL(aspectRatioChanged(float)));
567 QObject::connect(sender: d_func()->m_lens, SIGNAL(leftChanged(float)), receiver: this, SIGNAL(leftChanged(float)));
568 QObject::connect(sender: d_func()->m_lens, SIGNAL(rightChanged(float)), receiver: this, SIGNAL(rightChanged(float)));
569 QObject::connect(sender: d_func()->m_lens, SIGNAL(bottomChanged(float)), receiver: this, SIGNAL(bottomChanged(float)));
570 QObject::connect(sender: d_func()->m_lens, SIGNAL(topChanged(float)), receiver: this, SIGNAL(topChanged(float)));
571 QObject::connect(sender: d_func()->m_lens, SIGNAL(projectionMatrixChanged(QMatrix4x4)),
572 receiver: this, SIGNAL(projectionMatrixChanged(QMatrix4x4)));
573 QObject::connect(sender: d_func()->m_lens, signal: &QCameraLens::viewSphere, context: this, slot: &QCamera::viewSphere);
574
575 addComponent(comp: d_func()->m_lens);
576 addComponent(comp: d_func()->m_transform);
577}
578
579/*!
580 * Returns the current lens.
581 */
582QCameraLens *QCamera::lens() const
583{
584 Q_D(const QCamera);
585 return d->m_lens;
586}
587
588/*!
589 * Returns the camera's position via transform.
590 */
591Qt3DCore::QTransform *QCamera::transform() const
592{
593 Q_D(const QCamera);
594 return d->m_transform;
595}
596
597/*!
598 * Translates the camera's position and its view vector by \a vLocal in local coordinates.
599 * The \a option allows for toggling whether the view center should be translated.
600 */
601void QCamera::translate(const QVector3D &vLocal, CameraTranslationOption option)
602{
603 QVector3D viewVector = viewCenter() - position(); // From "camera" position to view center
604
605 // Calculate the amount to move by in world coordinates
606 QVector3D vWorld;
607 if (!qFuzzyIsNull(f: vLocal.x())) {
608 // Calculate the vector for the local x axis
609 const QVector3D x = QVector3D::crossProduct(v1: viewVector, v2: upVector()).normalized();
610 vWorld += vLocal.x() * x;
611 }
612
613 if (!qFuzzyIsNull(f: vLocal.y()))
614 vWorld += vLocal.y() * upVector();
615
616 if (!qFuzzyIsNull(f: vLocal.z()))
617 vWorld += vLocal.z() * viewVector.normalized();
618
619 // Update the camera position using the calculated world vector
620 setPosition(position() + vWorld);
621
622 // May be also update the view center coordinates
623 if (option == TranslateViewCenter)
624 setViewCenter(viewCenter() + vWorld);
625
626 // Refresh the camera -> view center vector
627 viewVector = viewCenter() - position();
628
629 // Calculate a new up vector. We do this by:
630 // 1) Calculate a new local x-direction vector from the cross product of the new
631 // camera to view center vector and the old up vector.
632 // 2) The local x vector is the normal to the plane in which the new up vector
633 // must lay. So we can take the cross product of this normal and the new
634 // x vector. The new normal vector forms the last part of the orthonormal basis
635 const QVector3D x = QVector3D::crossProduct(v1: viewVector, v2: upVector()).normalized();
636 setUpVector(QVector3D::crossProduct(v1: x, v2: viewVector).normalized());
637}
638
639/*!
640 * Translates the camera's position and its view vector by \a vWorld in world coordinates.
641 * The \a option allows for toggling whether the view center should be translated.
642 */
643void QCamera::translateWorld(const QVector3D &vWorld, CameraTranslationOption option)
644{
645 // Update the camera position using the calculated world vector
646 setPosition(position() + vWorld);
647
648 // May be also update the view center coordinates
649 if (option == TranslateViewCenter)
650 setViewCenter(viewCenter() + vWorld);
651}
652
653/*!
654 * Returns the calculated tilt rotation in relation to the \a angle in degrees taken in
655 * to adjust the camera's tilt or up/down rotation on the X axis.
656 */
657QQuaternion QCamera::tiltRotation(float angle) const
658{
659 const QVector3D viewVector = viewCenter() - position();
660 const QVector3D xBasis = QVector3D::crossProduct(v1: upVector(), v2: viewVector.normalized()).normalized();
661 return QQuaternion::fromAxisAndAngle(axis: xBasis, angle: -angle);
662}
663
664/*!
665 * Returns the calculated pan rotation in relation to the \a angle in degrees taken in
666 * to adjust the camera's pan or left/right rotation on the Y axis.
667 */
668QQuaternion QCamera::panRotation(float angle) const
669{
670 return QQuaternion::fromAxisAndAngle(axis: upVector(), angle);
671}
672
673/*!
674 * Returns the calculated roll rotation in relation to the \a angle in degrees taken in
675 * to adjust the camera's roll or lean left/right rotation on the Z axis.
676 */
677QQuaternion QCamera::rollRotation(float angle) const
678{
679 QVector3D viewVector = viewCenter() - position();
680 return QQuaternion::fromAxisAndAngle(axis: viewVector, angle: -angle);
681}
682
683/*!
684 * Returns the calculated rotation in relation to the \a angle in degrees and
685 * chosen \a axis taken in.
686 */
687QQuaternion QCamera::rotation(float angle, const QVector3D &axis) const
688{
689 return QQuaternion::fromAxisAndAngle(axis, angle);
690}
691
692/*!
693 * Adjusts the tilt angle of the camera by \a angle in degrees.
694 */
695void QCamera::tilt(float angle)
696{
697 QQuaternion q = tiltRotation(angle);
698 rotate(q);
699}
700
701/*!
702 * Adjusts the pan angle of the camera by \a angle in degrees.
703 */
704void QCamera::pan(float angle)
705{
706 QQuaternion q = panRotation(angle: -angle);
707 rotate(q);
708}
709
710/*!
711 * Adjusts the pan angle of the camera by \a angle in degrees on a chosen \a axis.
712 */
713void QCamera::pan(float angle, const QVector3D &axis)
714{
715 QQuaternion q = rotation(angle: -angle, axis);
716 rotate(q);
717}
718
719/*!
720 * Adjusts the camera roll by \a angle in degrees.
721 */
722void QCamera::roll(float angle)
723{
724 QQuaternion q = rollRotation(angle: -angle);
725 rotate(q);
726}
727
728/*!
729 * Adjusts the camera tilt about view center by \a angle in degrees.
730 */
731void QCamera::tiltAboutViewCenter(float angle)
732{
733 QQuaternion q = tiltRotation(angle: -angle);
734 rotateAboutViewCenter(q);
735}
736
737/*!
738 * Adjusts the camera pan about view center by \a angle in degrees.
739 */
740void QCamera::panAboutViewCenter(float angle)
741{
742 QQuaternion q = panRotation(angle);
743 rotateAboutViewCenter(q);
744}
745
746/*!
747 * Adjusts the camera pan about view center by \a angle in degrees on \a axis.
748 */
749void QCamera::panAboutViewCenter(float angle, const QVector3D &axis)
750{
751 QQuaternion q = rotation(angle, axis);
752 rotateAboutViewCenter(q);
753}
754
755/*!
756 * Adjusts the camera roll about view center by \a angle in degrees.
757 */
758void QCamera::rollAboutViewCenter(float angle)
759{
760 QQuaternion q = rollRotation(angle);
761 rotateAboutViewCenter(q);
762}
763
764/*!
765 * Rotates the camera with the use of a Quaternion in \a q.
766 */
767void QCamera::rotate(const QQuaternion& q)
768{
769 setUpVector(q * upVector());
770 QVector3D viewVector = viewCenter() - position();
771 QVector3D cameraToCenter = q * viewVector;
772 setViewCenter(position() + cameraToCenter);
773}
774
775/*!
776 * Rotates the camera about the view center with the use of a Quaternion
777 * in \a q.
778 */
779void QCamera::rotateAboutViewCenter(const QQuaternion& q)
780{
781 setUpVector(q * upVector());
782 QVector3D viewVector = viewCenter() - position();
783 QVector3D cameraToCenter = q * viewVector;
784 setPosition(viewCenter() - cameraToCenter);
785 setViewCenter(position() + cameraToCenter);
786}
787
788/*!
789 * Rotates and moves the camera so that it's viewCenter is the center of the scene's bounding volume
790 * and the entire scene fits in the view port.
791 *
792 * \note Only works if the lens is in perspective or orthographic projection mode.
793 * \sa Qt3D.Render::Camera::projectionType
794 */
795void QCamera::viewAll()
796{
797 Q_D(QCamera);
798 d->m_lens->viewAll(cameraId: id());
799}
800
801/*!
802 * Rotates and moves the camera so that it's viewCenter is \a center
803 * and a sphere of \a radius fits in the view port.
804 *
805 * \note Only works if the lens is in perspective or orthographic projection mode.
806 * \sa Qt3D.Render::Camera::projectionType
807 */
808void QCamera::viewSphere(const QVector3D &center, float radius)
809{
810 Q_D(QCamera);
811 if ((d->m_lens->projectionType() != QCameraLens::PerspectiveProjection &&
812 d->m_lens->projectionType() != QCameraLens::OrthographicProjection) ||
813 radius <= 0.f)
814 return;
815
816 // Ensure the sphere fits in the view port even if aspect ratio < 1 (i.e. width < height)
817 float height = (1.05f * radius) / (d->m_lens->aspectRatio() < 1.0f ? d->m_lens->aspectRatio() : 1.0f);
818 float dist = 1.0f;
819 if (d->m_lens->projectionType() == QCameraLens::PerspectiveProjection) {
820 dist = height / std::sin(x: qDegreesToRadians(degrees: d->m_lens->fieldOfView()) / 2.0f);
821 }
822 else if (d->m_lens->projectionType() == QCameraLens::OrthographicProjection) {
823 d->m_lens->setOrthographicProjection(left: -height * d->m_lens->aspectRatio(), right: height * d->m_lens->aspectRatio(), bottom: -height, top: height,
824 nearPlane: nearPlane(), farPlane: farPlane());
825 dist = height / std::sin(x: qDegreesToRadians(degrees: d->m_lens->fieldOfView()) / 2.0f);
826 }
827 else {
828 dist = (d->m_viewCenter - d->m_position).length();
829 }
830 QVector3D dir = (d->m_viewCenter - d->m_position).normalized();
831 QVector3D newPos = center - (dir * dist);
832 setViewCenter(center);
833 setPosition(newPos);
834}
835
836/*!
837 * Rotates and moves the camera so that it's viewCenter is the center of the
838 * \a {entity}'s bounding volume and the entire entity fits in the view port.
839 *
840 * \note Only works if the lens is in perspective or orthographic projection mode.
841 * \sa {Qt3D.Render::Camera::projectionType}{Camera.projectionType}
842 */
843void QCamera::viewEntity(Qt3DCore::QEntity *entity)
844{
845 if (!entity)
846 return;
847
848 Q_D(QCamera);
849 d->m_lens->viewEntity(entityId: entity->id(), cameraId: id());
850}
851
852/*!
853 * Sets the camera's projection type to \a type.
854 */
855void QCamera::setProjectionType(QCameraLens::ProjectionType type)
856{
857 Q_D(QCamera);
858 d->m_lens->setProjectionType(type);
859}
860
861QCameraLens::ProjectionType QCamera::projectionType() const
862{
863 Q_D(const QCamera);
864 return d->m_lens->projectionType();
865}
866
867/*!
868 * Sets the camera's near plane to \a nearPlane.
869 */
870void QCamera::setNearPlane(float nearPlane)
871{
872 Q_D(QCamera);
873 d->m_lens->setNearPlane(nearPlane);
874}
875
876float QCamera::nearPlane() const
877{
878 Q_D(const QCamera);
879 return d->m_lens->nearPlane();
880}
881
882/*!
883 * Sets the camera's far plane to \a farPlane
884 */
885void QCamera::setFarPlane(float farPlane)
886{
887 Q_D(QCamera);
888 d->m_lens->setFarPlane(farPlane);
889}
890
891float QCamera::farPlane() const
892{
893 Q_D(const QCamera);
894 return d->m_lens->farPlane();
895}
896
897/*!
898 * Sets the camera's field of view to \a fieldOfView in degrees.
899 */
900void QCamera::setFieldOfView(float fieldOfView)
901{
902 Q_D(QCamera);
903 d->m_lens->setFieldOfView(fieldOfView);
904}
905
906float QCamera::fieldOfView() const
907{
908 Q_D(const QCamera);
909 return d->m_lens->fieldOfView();
910}
911
912/*!
913 * Sets the camera's aspect ratio to \a aspectRatio.
914 */
915void QCamera::setAspectRatio(float aspectRatio)
916{
917 Q_D(QCamera);
918 d->m_lens->setAspectRatio(aspectRatio);
919}
920
921float QCamera::aspectRatio() const
922{
923 Q_D(const QCamera);
924 return d->m_lens->aspectRatio();
925}
926
927/*!
928 * Sets the left of the camera to \a left.
929 */
930void QCamera::setLeft(float left)
931{
932 Q_D(QCamera);
933 d->m_lens->setLeft(left);
934}
935
936float QCamera::left() const
937{
938 Q_D(const QCamera);
939 return d->m_lens->left();
940}
941
942/*!
943 * Sets the right of the camera to \a right.
944 */
945void QCamera::setRight(float right)
946{
947 Q_D(QCamera);
948 d->m_lens->setRight(right);
949}
950
951float QCamera::right() const
952{
953 Q_D(const QCamera);
954 return d->m_lens->right();
955}
956
957/*!
958 * Sets the bottom of the camera to \a bottom.
959 */
960void QCamera::setBottom(float bottom)
961{
962 Q_D(QCamera);
963 d->m_lens->setBottom(bottom);
964}
965
966float QCamera::bottom() const
967{
968 Q_D(const QCamera);
969 return d->m_lens->bottom();
970}
971
972/*!
973 * Sets the top of the camera to \a top.
974 */
975void QCamera::setTop(float top)
976{
977 Q_D(QCamera);
978 d->m_lens->setTop(top);
979}
980
981float QCamera::top() const
982{
983 Q_D(const QCamera);
984 return d->m_lens->top();
985}
986
987/*!
988 * Sets the camera's projection matrix to \a projectionMatrix.
989 */
990void QCamera::setProjectionMatrix(const QMatrix4x4 &projectionMatrix)
991{
992 Q_D(QCamera);
993 d->m_lens->setProjectionMatrix(projectionMatrix);
994}
995
996/*!
997 * Sets the camera's exposure to \a exposure.
998 */
999void QCamera::setExposure(float exposure)
1000{
1001 Q_D(QCamera);
1002 d->m_lens->setExposure(exposure);
1003}
1004
1005QMatrix4x4 QCamera::projectionMatrix() const
1006{
1007 Q_D(const QCamera);
1008 return d->m_lens->projectionMatrix();
1009}
1010
1011float QCamera::exposure() const
1012{
1013 Q_D(const QCamera);
1014 return d->m_lens->exposure();
1015}
1016
1017/*!
1018 * Sets the camera's position in 3D space to \a position.
1019 */
1020void QCamera::setPosition(const QVector3D &position)
1021{
1022 Q_D(QCamera);
1023 if (!qFuzzyCompare(v1: d->m_position, v2: position)) {
1024 d->m_position = position;
1025 d->m_cameraToCenter = d->m_viewCenter - position;
1026 d->m_viewMatrixDirty = true;
1027 emit positionChanged(position);
1028 emit viewVectorChanged(viewVector: d->m_cameraToCenter);
1029 d->updateViewMatrixAndTransform();
1030 }
1031}
1032
1033QVector3D QCamera::position() const
1034{
1035 Q_D(const QCamera);
1036 return d->m_position;
1037}
1038
1039/*!
1040 * Sets the camera's up vector to \a upVector.
1041 */
1042void QCamera::setUpVector(const QVector3D &upVector)
1043{
1044 Q_D(QCamera);
1045 if (!qFuzzyCompare(v1: d->m_upVector, v2: upVector)) {
1046 d->m_upVector = upVector;
1047 d->m_viewMatrixDirty = true;
1048 emit upVectorChanged(upVector);
1049 d->updateViewMatrixAndTransform();
1050 }
1051}
1052
1053QVector3D QCamera::upVector() const
1054{
1055 Q_D(const QCamera);
1056 return d->m_upVector;
1057}
1058
1059/*!
1060 * Sets the camera's view center to \a viewCenter.
1061 */
1062void QCamera::setViewCenter(const QVector3D &viewCenter)
1063{
1064 Q_D(QCamera);
1065 if (!qFuzzyCompare(v1: d->m_viewCenter, v2: viewCenter)) {
1066 d->m_viewCenter = viewCenter;
1067 d->m_cameraToCenter = viewCenter - d->m_position;
1068 d->m_viewMatrixDirty = true;
1069 emit viewCenterChanged(viewCenter);
1070 emit viewVectorChanged(viewVector: d->m_cameraToCenter);
1071 d->updateViewMatrixAndTransform();
1072 }
1073}
1074
1075QVector3D QCamera::viewCenter() const
1076{
1077 Q_D(const QCamera);
1078 return d->m_viewCenter;
1079}
1080
1081QVector3D QCamera::viewVector() const
1082{
1083 Q_D(const QCamera);
1084 return d->m_cameraToCenter;
1085}
1086
1087QMatrix4x4 QCamera::viewMatrix() const
1088{
1089 Q_D(const QCamera);
1090 return d->m_viewMatrix;
1091}
1092
1093} // Qt3DRender
1094
1095QT_END_NAMESPACE
1096
1097#include "moc_qcamera.cpp"
1098

source code of qt3d/src/render/frontend/qcamera.cpp