Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dperspectivecamera.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7
8#include <QtQuick3DRuntimeRender/private/qssgrendercamera_p.h>
9
10#include <QtMath>
11#include <QtQuick3DUtils/private/qssgutils_p.h>
12
14
16
18
19/*!
20 \qmltype PerspectiveCamera
21 \inherits Camera
22 \inqmlmodule QtQuick3D
23 \brief Defines a Perspective Camera for viewing the content of a 3D scene.
24
25 A \l Camera defines how the content of the 3D scene is projected onto a 2D surface,
26 such as a View3D. A scene needs at least one \l Camera in order to visualize its
27 contents.
28
29 It is possible to position and rotate the \l Camera like any other spatial \l{QtQuick3D::Node}{Node} in
30 the scene. The \l{QtQuick3D::Node}{Node}'s location and orientation determines where the \l Camera is in
31 the scene, and what direction it is facing. The default orientation of the \l Camera
32 has its forward vector pointing along the negative Z axis and its up vector along
33 the positive Y axis.
34
35 \image perspectivecamera.png {Perspective camera projection diagram}
36
37 PerspectiveCamera is the standard \l Camera type. It gives a realistic projection of the
38 scene, where distant objects are perceived as smaller. The frustum is defined by
39 the fieldOfView property as well as near and far clip planes.
40
41 The following example creates a PerspectiveCamera at position [0, 200, 300] in the scene, a
42 field of view of 90 degrees and with a 30 degree downward pitch.
43 \code
44 PerspectiveCamera {
45 position: Qt.vector3d(0, 200, 300)
46 eulerRotation.x: -30
47 fieldOfView: 90
48 }
49 \endcode
50
51 \sa {Qt Quick 3D - View3D Example}, OrthographicCamera, FrustumCamera, CustomCamera
52*/
53
54/*!
55 \internal
56*/
57QQuick3DPerspectiveCamera::QQuick3DPerspectiveCamera(QQuick3DNodePrivate &dd, QQuick3DNode *parent)
58 : QQuick3DCamera(dd, parent)
59{}
60
61/*!
62 \internal
63*/
64QQuick3DPerspectiveCamera::QQuick3DPerspectiveCamera(QQuick3DNode *parent)
65 : QQuick3DPerspectiveCamera(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::PerspectiveCamera, QQuick3DContentLayer::LayerAll)), parent)
66{}
67
68/*!
69 \qmlproperty real PerspectiveCamera::clipNear
70
71 This property defines the near clip plane of the PerspectiveCamera's frustum. Geometry which
72 is closer to the \l Camera than the near clip plane will not be visible.
73
74 The default value is 10.0. The unit depends on the user's geometry units,
75 and the value is relative to the global camera position.
76*/
77
78float QQuick3DPerspectiveCamera::clipNear() const
79{
80 return m_clipNear;
81}
82
83/*!
84 \qmlproperty real PerspectiveCamera::clipFar
85
86 This property defines the far clip plane of the PerspectiveCamera's frustum. Geometry which
87 is further away from the \l Camera than the far clip plane will not be visible.
88
89 The default value is 10000.0. The unit depends on the user's geometry units,
90 and the value is relative to the global camera position.
91*/
92
93float QQuick3DPerspectiveCamera::clipFar() const
94{
95 return m_clipFar;
96}
97
98/*!
99 \qmlproperty enumeration PerspectiveCamera::fieldOfViewOrientation
100
101 This property holds the orientation in which camera field of view is given.
102
103 \value PerspectiveCamera.Vertical
104 The provided field of view is vertical, meaning the field of view is the angle between
105 the line traced from the camera to the center top of the viewport and the line from
106 the camera to the center bottom of the viewport. The horizontal aspect ratio will be
107 adjusted to maintain aspect ratio.
108 \value PerspectiveCamera.Horizontal
109 The provided field of view is horizontal, meaning the field of view is the angle between
110 the line traced from the camera to the center left side of the viewport and the line from
111 the camera to the center right side of the viewport. The vertical aspect ratio will be
112 adjusted to maintain aspect ratio.
113
114
115 The default value is \c {PerspectiveCamera.Vertical}.
116 */
117
118/*!
119 \qmlproperty real PerspectiveCamera::fieldOfView
120
121 This property holds the field of view of the camera in degrees. This can be either the
122 vertical or horizontal field of view depending on whether the fieldOfViewOrientation property
123 is set to \c {PerspectiveCamera.Vertical} or \c {PerspectiveCamera.Horizontal}.
124
125 The default value is 60.0.
126 */
127
128float QQuick3DPerspectiveCamera::fieldOfView() const
129{
130 return m_fieldOfView;
131}
132
133QQuick3DPerspectiveCamera::FieldOfViewOrientation QQuick3DPerspectiveCamera::fieldOfViewOrientation() const
134{
135 return m_fieldOfViewOrientation;
136}
137
138void QQuick3DPerspectiveCamera::setClipNear(float clipNear)
139{
140 if (qFuzzyCompare(m_clipNear, clipNear))
141 return;
142
143 m_clipNear = clipNear;
144 emit clipNearChanged();
145 update();
146}
147
148void QQuick3DPerspectiveCamera::setClipFar(float clipFar)
149{
150 if (qFuzzyCompare(m_clipFar, clipFar))
151 return;
152
153 m_clipFar = clipFar;
154 emit clipFarChanged();
155 update();
156}
157
158void QQuick3DPerspectiveCamera::setFieldOfView(float fieldOfView)
159{
160 if (qFuzzyCompare(m_fieldOfView, fieldOfView))
161 return;
162
163 m_fieldOfView = fieldOfView;
164 emit fieldOfViewChanged();
165 update();
166}
167
168void QQuick3DPerspectiveCamera::setFieldOfViewOrientation(QQuick3DPerspectiveCamera::FieldOfViewOrientation
169 fieldOfViewOrientation)
170{
171 if (m_fieldOfViewOrientation == fieldOfViewOrientation)
172 return;
173
174 m_fieldOfViewOrientation = fieldOfViewOrientation;
175 emit fieldOfViewOrientationChanged();
176 update();
177}
178
179QSSGRenderGraphObject *QQuick3DPerspectiveCamera::updateSpatialNode(QSSGRenderGraphObject *node)
180{
181 QSSGRenderCamera *camera = static_cast<QSSGRenderCamera *>(QQuick3DCamera::updateSpatialNode(node));
182 if (camera) {
183 QSSGRenderCamera::FieldOfView fov;
184 switch (m_fieldOfViewOrientation) {
185 case QQuick3DPerspectiveCamera::Vertical:
186 fov = QSSGRenderCamera::FieldOfView::fromDegrees(m_fieldOfView);
187 break;
188 case QQuick3DPerspectiveCamera::Horizontal:
189 fov = QSSGRenderCamera::FieldOfView::fromDegrees<QSSGRenderCamera::FieldOfView::Orientation::Horizontal>(m_fieldOfView);
190 break;
191 }
192
193 const bool changed = ((int(qUpdateIfNeeded(camera->clipPlanes, {m_clipNear, m_clipFar}))
194 | int(qUpdateIfNeeded(camera->fov, fov))) != 0);
195 if (changed)
196 camera->markDirty(QSSGRenderCamera::DirtyFlag::CameraDirty);
197 }
198
199 return camera;
200}
201
202QT_END_NAMESPACE
Combined button and popup list for selecting options.