| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquick3dcustomcamera_p.h" |
| 5 | |
| 6 | #include <QtQuick3DRuntimeRender/private/qssgrendercamera_p.h> |
| 7 | |
| 8 | #include <QtMath> |
| 9 | #include <QtQuick3DUtils/private/qssgutils_p.h> |
| 10 | |
| 11 | #include "qquick3dnode_p_p.h" |
| 12 | |
| 13 | #include "qquick3dutils_p.h" |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | /*! |
| 18 | \qmltype CustomCamera |
| 19 | \inherits Camera |
| 20 | \inqmlmodule QtQuick3D |
| 21 | \brief Defines a Camera with a custom projection matrix. |
| 22 | |
| 23 | A \l Camera defines how the content of the 3D scene is projected onto a 2D surface, |
| 24 | such as a View3D. A scene needs at least one \l Camera in order to visualize its |
| 25 | contents. |
| 26 | |
| 27 | It is possible to position and rotate the \l Camera like any other spatial \l{QtQuick3D::Node}{Node} in |
| 28 | the scene. The \l{QtQuick3D::Node}{Node}'s location and orientation determines where the \l Camera is in |
| 29 | the scene, and what direction it is facing. The default orientation of the \l Camera |
| 30 | has its forward vector pointing along the negative Z axis and its up vector along |
| 31 | the positive Y axis. |
| 32 | |
| 33 | The CustomCamera type provides a \l Camera where the projection matrix can be customized |
| 34 | freely. |
| 35 | |
| 36 | The following example creates a CustomCamera at position [0, 200, 300] in the scene, with |
| 37 | a 30 degree downward pitch, and a custom projection matrix based on custom near and far plane |
| 38 | distances, and a custom field of view. |
| 39 | \code |
| 40 | CustomCamera { |
| 41 | position: Qt.vector3d(0, 200, 300) |
| 42 | eulerRotation.x: -30 |
| 43 | |
| 44 | property real near: 10.0 |
| 45 | property real far: 10000.0 |
| 46 | property real fov: 60.0 * Math.PI / 180.0 |
| 47 | projection: Qt.matrix4x4(Math.cos(fov / 2) / Math.sin(fov / 2) * (window.height / window.width), 0, 0, 0, |
| 48 | 0, Math.cos(fov / 2) / Math.sin(fov / 2), 0, 0, |
| 49 | 0, 0, -(near + far) / (far - near), -(2.0 * near * far) / (far - near), |
| 50 | 0, 0, -1, 0); |
| 51 | } |
| 52 | \endcode |
| 53 | |
| 54 | \note When using CustomCamera, some anti-aliasing modes(Temporal AA and Progressive AA) cannot be applied correctly. |
| 55 | |
| 56 | \sa PerspectiveCamera, OrthographicCamera, FrustumCamera |
| 57 | */ |
| 58 | |
| 59 | /*! |
| 60 | * \internal |
| 61 | */ |
| 62 | QQuick3DCustomCamera::QQuick3DCustomCamera(QQuick3DNode *parent) |
| 63 | : QQuick3DCamera(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::CustomCamera)), parent){} |
| 64 | |
| 65 | /*! |
| 66 | \qmlproperty matrix4x4 CustomCamera::projection |
| 67 | |
| 68 | This property defines the CustomCamera's projection matrix. |
| 69 | */ |
| 70 | QMatrix4x4 QQuick3DCustomCamera::projection() const |
| 71 | { |
| 72 | return m_projection; |
| 73 | } |
| 74 | |
| 75 | void QQuick3DCustomCamera::setProjection(const QMatrix4x4 &projection) |
| 76 | { |
| 77 | if (m_projection == projection) |
| 78 | return; |
| 79 | |
| 80 | m_projection = projection; |
| 81 | emit projectionChanged(); |
| 82 | update(); |
| 83 | } |
| 84 | |
| 85 | /*! |
| 86 | * \internal |
| 87 | */ |
| 88 | QSSGRenderGraphObject *QQuick3DCustomCamera::updateSpatialNode(QSSGRenderGraphObject *node) |
| 89 | { |
| 90 | QSSGRenderCamera *camera = static_cast<QSSGRenderCamera *>(QQuick3DCamera::updateSpatialNode(node)); |
| 91 | if (camera) { |
| 92 | if (qUpdateIfNeeded(orig&: camera->projection, updated: m_projection)) |
| 93 | camera->markDirty(dirtyFlag: QSSGRenderCamera::DirtyFlag::CameraDirty); |
| 94 | } |
| 95 | |
| 96 | return camera; |
| 97 | } |
| 98 | |
| 99 | QT_END_NAMESPACE |
| 100 | |