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
qquick3dpointlight.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
8
9#include <QtQuick3DRuntimeRender/private/qssgrenderlight_p.h>
10
12
14
15/*!
16 \qmltype PointLight
17 \inherits Light
18 \inqmlmodule QtQuick3D
19 \brief Defines a point light in the scene.
20
21 The point light can be described as a sphere, emitting light with equal strength in all
22 directions from the center of the light. This is similar to the way a light bulb emits light.
23
24 Rotating or scaling a point light does not have any effect. Moving a point light will change
25 the position from where the light is emitted.
26
27 By default, a point light intensity diminishes according to inverse-square-law. However, the fade-off
28 (and range) can be controlled with the \l {constantFade}, \l {linearFade}, and
29 \l quadraticFade properties. Light attenuation is calculated using the formula:
30 \l {constantFade} + \c distance * (\l {linearFade} * 0.01) + \c distance^2 * (\l {quadraticFade} * 0.0001)
31
32 \section2 A simple example: shading a sphere in three different ways
33
34 Take a scene containing a sphere in front of a scaled up rectangle in the
35 background. The default base color of the PrincipledMaterial of the
36 rectangle is white.
37
38 Without any lights and disabling light-related shading for the two meshes,
39 we get the following:
40
41 \qml
42 import QtQuick
43 import QtQuick3D
44 View3D {
45 anchors.fill: parent
46
47 PerspectiveCamera { z: 600 }
48
49 Model {
50 source: "#Rectangle"
51 scale: Qt.vector3d(10, 10, 10)
52 z: -100
53 materials: PrincipledMaterial {
54 lighting: PrincipledMaterial.NoLighting
55 }
56 }
57
58 Model {
59 source: "#Sphere"
60 scale: Qt.vector3d(2, 2, 2)
61 materials: PrincipledMaterial {
62 lighting: PrincipledMaterial.NoLighting
63 baseColor: "#40c060"
64 roughness: 0.1
65 }
66 }
67 }
68 \endqml
69
70 \image pointlight-1.png
71 {Green circle with white background}
72
73 Adding a directional light, emitting down the Z axis by default, leads to the following:
74
75 \qml
76 import QtQuick
77 import QtQuick3D
78 View3D {
79 anchors.fill: parent
80
81 PerspectiveCamera { z: 600 }
82
83 DirectionalLight { }
84
85 Model {
86 source: "#Rectangle"
87 scale: Qt.vector3d(10, 10, 10)
88 z: -100
89 materials: PrincipledMaterial { }
90 }
91
92 Model {
93 source: "#Sphere"
94 scale: Qt.vector3d(2, 2, 2)
95 materials: PrincipledMaterial {
96 baseColor: "#40c060"
97 roughness: 0.1
98 }
99 }
100 }
101 \endqml
102
103 \image pointlight-2.png
104 {Green circle with black edge}
105
106 What if we now replace DirectionalLight with:
107
108 \qml
109 PointLight {
110 z: 300
111 }
112 \endqml
113
114 The white colored PointLight here is moved on the Z axis so that it is
115 halfway between the camera and the center of the scene. Unlike
116 DirectionalLight, the rotation of the PointLight does not matter, whereas
117 its position is significant. The diminishing intensity is visible here,
118 especially on the rectangle mesh in the background.
119
120 \image pointlight-3.png
121 {Green gradient circle with black edge and dark gray gradient
122 background showing point light attenuation}
123
124 For more usage examples, see \l{Qt Quick 3D - Lights Example}.
125
126 \sa DirectionalLight, SpotLight, {Shadow Mapping}
127*/
128
129/*!
130 \qmlproperty real PointLight::constantFade
131
132 This property is constant factor of the attenuation term of the light.
133 The default value is 1.0.
134 */
135
136/*!
137 \qmlproperty real PointLight::linearFade
138
139 This property increases the rate at which the lighting effect dims the light
140 in proportion to the distance to the light. The default value is \c 0.0, meaning the light doesn't
141 have linear fade. The value used here is multiplied by \c 0.01 before being used to
142 calculate light attenuation.
143*/
144
145/*!
146 \qmlproperty real PointLight::quadraticFade
147
148 This property increases the rate at which the lighting effect dims the light
149 in proportion to the inverse square law. The default value is 1.0 meaning the point light
150 fade exactly follows the inverse square law i.e. when distance to an object doubles the
151 light intensity decreases to 1/4th. The value used here is multiplied by \c 0.0001 before
152 being used to calculate light attenuation.
153*/
154
155QQuick3DPointLight::QQuick3DPointLight(QQuick3DNode *parent)
156 : QQuick3DAbstractLight(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::PointLight)), parent) {}
157
158float QQuick3DPointLight::constantFade() const
159{
160 return m_constantFade;
161}
162
163float QQuick3DPointLight::linearFade() const
164{
165 return m_linearFade;
166}
167
168float QQuick3DPointLight::quadraticFade() const
169{
170 return m_quadraticFade;
171}
172
173void QQuick3DPointLight::setConstantFade(float constantFade)
174{
175 if (qFuzzyCompare(m_constantFade, constantFade))
176 return;
177
178 m_constantFade = constantFade;
179 m_dirtyFlags.setFlag(DirtyFlag::FadeDirty);
180 emit constantFadeChanged();
181 update();
182}
183
184void QQuick3DPointLight::setLinearFade(float linearFade)
185{
186 if (qFuzzyCompare(m_linearFade, linearFade))
187 return;
188
189 m_linearFade = linearFade;
190 m_dirtyFlags.setFlag(DirtyFlag::FadeDirty);
191 emit linearFadeChanged();
192 update();
193}
194
195void QQuick3DPointLight::setQuadraticFade(float quadraticFade)
196{
197 if (qFuzzyCompare(m_quadraticFade, quadraticFade))
198 return;
199
200 m_quadraticFade = quadraticFade;
201 m_dirtyFlags.setFlag(DirtyFlag::FadeDirty);
202 emit quadraticFadeChanged();
203 update();
204}
205
206QSSGRenderGraphObject *QQuick3DPointLight::updateSpatialNode(QSSGRenderGraphObject *node)
207{
208 if (!node) {
209 markAllDirty();
210 node = new QSSGRenderLight(QSSGRenderLight::Type::PointLight);
211 }
212
213 QQuick3DAbstractLight::updateSpatialNode(node); // Marks the light node dirty if m_dirtyFlags != 0
214
215 QSSGRenderLight *light = static_cast<QSSGRenderLight *>(node);
216
217 if (m_dirtyFlags.testFlag(DirtyFlag::FadeDirty)) {
218 m_dirtyFlags.setFlag(DirtyFlag::FadeDirty, false);
219 light->m_constantFade = m_constantFade;
220 light->m_linearFade = m_linearFade;
221 light->m_quadraticFade = m_quadraticFade;
222 }
223
224 return node;
225}
226
227QT_END_NAMESPACE
Combined button and popup list for selecting options.