1// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qspotlight.h"
5#include "qspotlight_p.h"
6#include "shaderdata_p.h"
7
8QT_BEGIN_NAMESPACE
9
10namespace Qt3DRender {
11
12
13/*
14 Expected Shader struct
15
16 \code
17
18 struct SpotLight
19 {
20 vec3 position;
21 vec3 localDirection;
22 vec4 color;
23 float intensity;
24 float cutOffAngle;
25 };
26
27 uniform SpotLight spotLights[10];
28
29 \endcode
30 */
31
32QSpotLightPrivate::QSpotLightPrivate()
33 : QAbstractLightPrivate(QAbstractLight::SpotLight)
34{
35 m_shaderData->setProperty(name: "constantAttenuation", value: 1.0f);
36 m_shaderData->setProperty(name: "linearAttenuation", value: 0.0f);
37 m_shaderData->setProperty(name: "quadraticAttenuation", value: 0.0f);
38 m_shaderData->setProperty(name: "direction", value: QVector3D(0.0f, -1.0f, 0.0f));
39 m_shaderData->setProperty(name: "directionTransformed", value: Render::ShaderData::ModelToWorldDirection);
40 m_shaderData->setProperty(name: "cutOffAngle", value: 45.0f);
41}
42
43/*!
44 \class Qt3DRender::QSpotLight
45 \inmodule Qt3DRender
46 \since 5.5
47 \brief Encapsulate a Spot Light object in a Qt 3D scene.
48
49 A spotlight is a light source that emits a cone of light in a particular direction.
50
51 A spotlight uses three attenuation factors to describe how the intensity of the light
52 decreases over distance. These factors are designed to be used together in calcuating total
53 attenuation. For the materials in Qt3D Extras the following formula is used, where distance
54 is the distance from the light to the surface being rendered:
55
56 \code
57 totalAttenuation = 1.0 / (constantAttenuation + (linearAttenuation * distance) + (quadraticAttenuation * distance * distance));
58 \endcode
59
60 Custom materials may choose to interpret these factors differently.
61 */
62
63/*!
64 \qmltype SpotLight
65 \instantiates Qt3DRender::QSpotLight
66 \inherits AbstractLight
67 \inqmlmodule Qt3D.Render
68 \since 5.5
69 \brief Encapsulate a Spot Light object in a Qt 3D scene.
70
71 A spotlight is a light source that emits a cone of light in a particular direction.
72
73 A spotlight uses three attenuation factors to describe how the intensity of the light
74 decreases over distance. These factors are designed to be used together in calcuating total
75 attenuation. For the materials in Qt3D Extras the following formula is used, where distance
76 is the distance from the light to the surface being rendered:
77
78 \code
79 totalAttenuation = 1.0 / (constantAttenuation + (linearAttenuation * distance) + (quadraticAttenuation * distance * distance));
80 \endcode
81
82 Custom materials may choose to interpret these factors differently.
83*/
84
85/*!
86 \fn Qt3DRender::QSpotLight::QSpotLight(Qt3DCore::QNode *parent)
87 Constructs a new QSpotLight with the specified \a parent.
88 */
89QSpotLight::QSpotLight(QNode *parent)
90 : QAbstractLight(*new QSpotLightPrivate, parent)
91{
92}
93
94/*! \internal */
95QSpotLight::~QSpotLight()
96{
97}
98
99/*! \internal */
100QSpotLight::QSpotLight(QSpotLightPrivate &dd, QNode *parent)
101 : QAbstractLight(dd, parent)
102{
103}
104
105/*!
106 \qmlproperty float Qt3D.Render::SpotLight::constantAttenuation
107 Specifies the constant attenuation of the spot light.
108
109 \note The exact meaning and use of this property is up to the
110 material implementation.
111*/
112
113/*!
114 \property Qt3DRender::QSpotLight::constantAttenuation
115 Specifies the constant attenuation of the spot light.
116
117 \note The exact meaning and use of this property is up to the
118 material implementation.
119 */
120float QSpotLight::constantAttenuation() const
121{
122 Q_D(const QSpotLight);
123 return d->m_shaderData->property(name: "constantAttenuation").toFloat();
124}
125
126void QSpotLight::setConstantAttenuation(float value)
127{
128 Q_D(QSpotLight);
129 if (constantAttenuation() != value) {
130 d->m_shaderData->setProperty(name: "constantAttenuation", value);
131 emit constantAttenuationChanged(constantAttenuation: value);
132 }
133}
134
135/*!
136 \qmlproperty float Qt3D.Render::SpotLight::linearAttenuation
137 Specifies the linear attenuation of the spot light.
138
139 \note The exact meaning and use of this property is up to the
140 material implementation.
141*/
142
143/*!
144 \property Qt3DRender::QSpotLight::linearAttenuation
145 Specifies the linear attenuation of the spot light.
146
147 \note The exact meaning and use of this property is up to the
148 material implementation.
149 */
150float QSpotLight::linearAttenuation() const
151{
152 Q_D(const QSpotLight);
153 return d->m_shaderData->property(name: "linearAttenuation").toFloat();
154}
155
156void QSpotLight::setLinearAttenuation(float value)
157{
158 Q_D(QSpotLight);
159 if (linearAttenuation() != value) {
160 d->m_shaderData->setProperty(name: "linearAttenuation", value);
161 emit linearAttenuationChanged(linearAttenuation: value);
162 }
163}
164
165/*!
166 \qmlproperty float Qt3D.Render::SpotLight::quadraticAttenuation
167 Specifies the quadratic attenuation of the spot light.
168
169 \note The exact meaning and use of this property is up to the
170 material implementation.
171*/
172
173/*!
174 \property Qt3DRender::QSpotLight::quadraticAttenuation
175 Specifies the quadratic attenuation of the spot light.
176
177 \note The exact meaning and use of this property is up to the
178 material implementation.
179 */
180float QSpotLight::quadraticAttenuation() const
181{
182 Q_D(const QSpotLight);
183 return d->m_shaderData->property(name: "quadraticAttenuation").toFloat();
184}
185
186void QSpotLight::setQuadraticAttenuation(float value)
187{
188 Q_D(QSpotLight);
189 if (quadraticAttenuation() != value) {
190 d->m_shaderData->setProperty(name: "quadraticAttenuation", value);
191 emit quadraticAttenuationChanged(quadraticAttenuation: value);
192 }
193}
194
195/*!
196 \qmlproperty vector3d Qt3D.Render::SpotLight::localDirection
197 Specifies the local direction of the spot light.
198
199 \note The exact meaning and use of this property is up to the
200 material implementation.
201*/
202
203/*!
204 \property Qt3DRender::QSpotLight::localDirection
205 Specifies the local direction of the spot light.
206
207 \note The exact meaning and use of this property is up to the
208 material implementation.
209 */
210QVector3D QSpotLight::localDirection() const
211{
212 Q_D(const QSpotLight);
213 return d->m_shaderData->property(name: "direction").value<QVector3D>();
214}
215
216/*!
217 \qmlproperty float Qt3D.Render::SpotLight::cutOffAngle
218 Specifies the cut off angle of the spot light.
219
220 \note The exact meaning and use of this property is up to the
221 material implementation.
222*/
223
224/*!
225 \property Qt3DRender::QSpotLight::cutOffAngle
226 Specifies the cut off angle of the spot light.
227
228 \note The exact meaning and use of this property is up to the
229 material implementation.
230 */
231float QSpotLight::cutOffAngle() const
232{
233 Q_D(const QSpotLight);
234 return d->m_shaderData->property(name: "cutOffAngle").toFloat();
235}
236
237void QSpotLight::setLocalDirection(const QVector3D &direction)
238{
239 Q_D(QSpotLight);
240 if (localDirection() != direction) {
241 const QVector3D dir = direction.normalized();
242 d->m_shaderData->setProperty(name: "direction", value: dir);
243 emit localDirectionChanged(localDirection: dir);
244 }
245}
246
247void QSpotLight::setCutOffAngle(float value)
248{
249 Q_D(QSpotLight);
250 if (cutOffAngle() != value) {
251 d->m_shaderData->setProperty(name: "cutOffAngle", value);
252 emit cutOffAngleChanged(cutOffAngle: value);
253 }
254}
255
256} // namespace Qt3DRender
257
258QT_END_NAMESPACE
259
260#include "moc_qspotlight.cpp"
261

source code of qt3d/src/render/lights/qspotlight.cpp