1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qquick3dorthographiccamera_p.h"
5
6#include <QtQuick3DRuntimeRender/private/qssgrendercamera_p.h>
7
8#include <QtMath>
9#include <QtQuick3DUtils/private/qssgutils_p.h>
10
11#include "qquick3dutils_p.h"
12
13#include "qquick3dnode_p_p.h"
14
15QT_BEGIN_NAMESPACE
16
17/*!
18 \qmltype OrthographicCamera
19 \inherits Camera
20 \inqmlmodule QtQuick3D
21 \brief Defines an Camera with an orthographic 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 \image orthographiccamera.png
34
35 The OrthographicCamera is a parallel projection \l Camera, in which parallel lines remain
36 parallel and an object's perceived scale is unaffected by its distance from the \l Camera.
37 Typical use cases for this type of \l Camera are CAD (Computer-Assisted Design) applications
38 and cartography.
39
40 The following example creates a OrthographicCamera at position [0, 200, 300] in the scene, and
41 with a 30 degree downward pitch.
42 \code
43 OrthographicCamera {
44 position: Qt.vector3d(0, 200, 300)
45 eulerRotation.x: -30
46 }
47 \endcode
48
49 \sa {Qt Quick 3D - View3D Example}, PerspectiveCamera, FrustumCamera, CustomCamera
50*/
51
52/*!
53 * \internal
54 */
55QQuick3DOrthographicCamera::QQuick3DOrthographicCamera(QQuick3DNode *parent)
56 : QQuick3DCamera(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::OrthographicCamera)), parent) {}
57
58/*!
59 \qmlproperty real OrthographicCamera::clipNear
60
61 This property defines the near clip plane of the OrthographicCamera's frustum. Geometry which
62 is closer to the \l Camera than the near clip plane will not be visible.
63
64 The default value is 10.0.
65
66 \sa clipFar
67 */
68float QQuick3DOrthographicCamera::clipNear() const
69{
70 return m_clipNear;
71}
72
73/*!
74 \qmlproperty real OrthographicCamera::clipFar
75
76 This property defines the far clip plane of the OrthographicCamera's frustum. Geometry which
77 is further away from the \l Camera than the far clip plane will not be visible.
78
79 The default value is 10000.0.
80
81 \sa clipNear
82 */
83float QQuick3DOrthographicCamera::clipFar() const
84{
85 return m_clipFar;
86}
87
88/*!
89 \qmlproperty real OrthographicCamera::horizontalMagnification
90
91 This property holds the horizontal magnification of the OrthographicCamera's frustum.
92
93 The default value is 1.0.
94
95 \sa verticalMagnification
96 */
97float QQuick3DOrthographicCamera::horizontalMagnification() const
98{
99 return m_horizontalMagnification;
100}
101
102/*!
103 \qmlproperty real OrthographicCamera::verticalMagnification
104
105 This property holds the vertical magnification of the OrthographicCamera's frustum.
106
107 The default value is 1.0.
108
109 \sa horizontalMagnification
110 */
111float QQuick3DOrthographicCamera::verticalMagnification() const
112{
113 return m_verticalMagnification;
114}
115
116void QQuick3DOrthographicCamera::setClipNear(float clipNear)
117{
118 if (qFuzzyCompare(p1: m_clipNear, p2: clipNear))
119 return;
120
121 m_clipNear = clipNear;
122 emit clipNearChanged();
123 update();
124}
125
126void QQuick3DOrthographicCamera::setClipFar(float clipFar)
127{
128 if (qFuzzyCompare(p1: m_clipFar, p2: clipFar))
129 return;
130
131 m_clipFar = clipFar;
132 emit clipFarChanged();
133 update();
134}
135
136void QQuick3DOrthographicCamera::setHorizontalMagnification(float horizontalMagnification)
137{
138 if (horizontalMagnification <= 0.0) {
139 qWarning(msg: "OrthographicCamera: magnification must be greater than zero.");
140 return;
141 }
142
143 if (qFuzzyCompare(p1: m_horizontalMagnification, p2: horizontalMagnification))
144 return;
145
146 m_horizontalMagnification = horizontalMagnification;
147 emit horizontalMagnificationChanged();
148 update();
149}
150
151void QQuick3DOrthographicCamera::setVerticalMagnification(float verticalMagnification)
152{
153 if (verticalMagnification <= 0.0) {
154 qWarning(msg: "OrthographicCamera: magnification must be greater than zero.");
155 return;
156 }
157
158 if (qFuzzyCompare(p1: m_verticalMagnification, p2: verticalMagnification))
159 return;
160
161 m_verticalMagnification = verticalMagnification;
162 emit verticalMagnificationChanged();
163 update();
164}
165
166QSSGRenderGraphObject *QQuick3DOrthographicCamera::updateSpatialNode(QSSGRenderGraphObject *node)
167{
168 QSSGRenderCamera *camera = static_cast<QSSGRenderCamera *>(QQuick3DCamera::updateSpatialNode(node));
169 if (camera) {
170 const bool changed = ((int(qUpdateIfNeeded(orig&: camera->clipNear, updated: m_clipNear))
171 | int(qUpdateIfNeeded(orig&: camera->clipFar, updated: m_clipFar))
172 | int(qUpdateIfNeeded(orig&: camera->horizontalMagnification, updated: m_horizontalMagnification))
173 | int(qUpdateIfNeeded(orig&: camera->verticalMagnification, updated: m_verticalMagnification))) != 0);
174 if (changed)
175 camera->markDirty(dirtyFlag: QSSGRenderCamera::DirtyFlag::CameraDirty);
176 }
177
178 return camera;
179}
180
181QT_END_NAMESPACE
182

source code of qtquick3d/src/quick3d/qquick3dorthographiccamera.cpp