aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3d/qquick3dorthographiccamera.cpp
blob: 4ca01c7080852270b971d2730183a81842903fe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qquick3dorthographiccamera_p.h"

#include <QtQuick3DRuntimeRender/private/qssgrendercamera_p.h>

#include <QtMath>
#include <QtQuick3DUtils/private/qssgutils_p.h>

#include "qquick3dutils_p.h"

#include "qquick3dnode_p_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype OrthographicCamera
    \inherits Camera
    \inqmlmodule QtQuick3D
    \brief Defines an Camera with an orthographic projection matrix.

    A \l Camera defines how the content of the 3D scene is projected onto a 2D surface,
    such as a View3D. A scene needs at least one \l Camera in order to visualize its
    contents.

    It is possible to position and rotate the \l Camera like any other spatial \l{QtQuick3D::Node}{Node} in
    the scene. The \l{QtQuick3D::Node}{Node}'s location and orientation determines where the \l Camera is in
    the scene, and what direction it is facing. The default orientation of the \l Camera
    has its forward vector pointing along the negative Z axis and its up vector along
    the positive Y axis.

    \image orthographiccamera.png

    The OrthographicCamera is a parallel projection \l Camera, in which parallel lines remain
    parallel and an object's perceived scale is unaffected by its distance from the \l Camera.
    Typical use cases for this type of \l Camera are CAD (Computer-Assisted Design) applications
    and cartography.

    The following example creates a OrthographicCamera at position [0, 200, 300] in the scene, and
    with a 30 degree downward pitch.
    \code
    OrthographicCamera {
        position: Qt.vector3d(0, 200, 300)
        eulerRotation.x: -30
    }
    \endcode

    \sa {Qt Quick 3D - View3D Example}, PerspectiveCamera, FrustumCamera, CustomCamera
*/

/*!
 * \internal
 */
QQuick3DOrthographicCamera::QQuick3DOrthographicCamera(QQuick3DNode *parent)
    : QQuick3DCamera(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::OrthographicCamera)), parent) {}

/*!
    \qmlproperty real OrthographicCamera::clipNear

    This property defines the near clip plane of the OrthographicCamera's frustum. Geometry which
    is closer to the \l Camera than the near clip plane will not be visible.

    The default value is 10.0.

    \sa clipFar
 */
float QQuick3DOrthographicCamera::clipNear() const
{
    return m_clipNear;
}

/*!
    \qmlproperty real OrthographicCamera::clipFar

    This property defines the far clip plane of the OrthographicCamera's frustum. Geometry which
    is further away from the \l Camera than the far clip plane will not be visible.

    The default value is 10000.0.

    \sa clipNear
 */
float QQuick3DOrthographicCamera::clipFar() const
{
    return m_clipFar;
}

/*!
    \qmlproperty real OrthographicCamera::horizontalMagnification

    This property holds the horizontal magnification of the OrthographicCamera's frustum.

    The default value is 1.0.

    \sa verticalMagnification
 */
float QQuick3DOrthographicCamera::horizontalMagnification() const
{
    return m_horizontalMagnification;
}

/*!
    \qmlproperty real OrthographicCamera::verticalMagnification

    This property holds the vertical magnification of the OrthographicCamera's frustum.

    The default value is 1.0.

    \sa horizontalMagnification
 */
float QQuick3DOrthographicCamera::verticalMagnification() const
{
    return m_verticalMagnification;
}

void QQuick3DOrthographicCamera::setClipNear(float clipNear)
{
    if (qFuzzyCompare(m_clipNear, clipNear))
        return;

    m_clipNear = clipNear;
    emit clipNearChanged();
    update();
}

void QQuick3DOrthographicCamera::setClipFar(float clipFar)
{
    if (qFuzzyCompare(m_clipFar, clipFar))
        return;

    m_clipFar = clipFar;
    emit clipFarChanged();
    update();
}

void QQuick3DOrthographicCamera::setHorizontalMagnification(float horizontalMagnification)
{
    if (horizontalMagnification <= 0.0) {
        qWarning("OrthographicCamera: magnification must be greater than zero.");
        return;
    }

    if (qFuzzyCompare(m_horizontalMagnification, horizontalMagnification))
        return;

    m_horizontalMagnification = horizontalMagnification;
    emit horizontalMagnificationChanged();
    update();
}

void QQuick3DOrthographicCamera::setVerticalMagnification(float verticalMagnification)
{
    if (verticalMagnification <= 0.0) {
        qWarning("OrthographicCamera: magnification must be greater than zero.");
        return;
    }

    if (qFuzzyCompare(m_verticalMagnification, verticalMagnification))
        return;

    m_verticalMagnification = verticalMagnification;
    emit verticalMagnificationChanged();
    update();
}

QSSGRenderGraphObject *QQuick3DOrthographicCamera::updateSpatialNode(QSSGRenderGraphObject *node)
{
    QSSGRenderCamera *camera = static_cast<QSSGRenderCamera *>(QQuick3DCamera::updateSpatialNode(node));
    if (camera) {
        const bool changed = ((int(qUpdateIfNeeded(camera->clipPlanes, { m_clipNear, m_clipFar }))
                               | int(qUpdateIfNeeded(camera->magnification, { m_horizontalMagnification, m_verticalMagnification }))) != 0);
        if (changed)
            camera->markDirty(QSSGRenderCamera::DirtyFlag::CameraDirty);
    }

    return camera;
}

QT_END_NAMESPACE