| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qspotlight.h" |
| 41 | #include "qspotlight_p.h" |
| 42 | #include "shaderdata_p.h" |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | namespace Qt3DRender { |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | Expected Shader struct |
| 51 | |
| 52 | \code |
| 53 | |
| 54 | struct SpotLight |
| 55 | { |
| 56 | vec3 position; |
| 57 | vec3 localDirection; |
| 58 | vec4 color; |
| 59 | float intensity; |
| 60 | float cutOffAngle; |
| 61 | }; |
| 62 | |
| 63 | uniform SpotLight spotLights[10]; |
| 64 | |
| 65 | \endcode |
| 66 | */ |
| 67 | |
| 68 | QSpotLightPrivate::QSpotLightPrivate() |
| 69 | : QAbstractLightPrivate(QAbstractLight::SpotLight) |
| 70 | { |
| 71 | m_shaderData->setProperty(name: "constantAttenuation" , value: 1.0f); |
| 72 | m_shaderData->setProperty(name: "linearAttenuation" , value: 0.0f); |
| 73 | m_shaderData->setProperty(name: "quadraticAttenuation" , value: 0.0f); |
| 74 | m_shaderData->setProperty(name: "direction" , value: QVector3D(0.0f, -1.0f, 0.0f)); |
| 75 | m_shaderData->setProperty(name: "directionTransformed" , value: Render::ShaderData::ModelToWorldDirection); |
| 76 | m_shaderData->setProperty(name: "cutOffAngle" , value: 45.0f); |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | \class Qt3DRender::QSpotLight |
| 81 | \inmodule Qt3DRender |
| 82 | \since 5.5 |
| 83 | \brief Encapsulate a Spot Light object in a Qt 3D scene. |
| 84 | |
| 85 | A spotlight is a light source that emits a cone of light in a particular direction. |
| 86 | |
| 87 | A spotlight uses three attenuation factors to describe how the intensity of the light |
| 88 | decreases over distance. These factors are designed to be used together in calcuating total |
| 89 | attenuation. For the materials in Qt3D Extras the following formula is used, where distance |
| 90 | is the distance from the light to the surface being rendered: |
| 91 | |
| 92 | \code |
| 93 | totalAttenuation = 1.0 / (constantAttenuation + (linearAttenuation * distance) + (quadraticAttenuation * distance * distance)); |
| 94 | \endcode |
| 95 | |
| 96 | Custom materials may choose to interpret these factors differently. |
| 97 | */ |
| 98 | |
| 99 | /*! |
| 100 | \qmltype SpotLight |
| 101 | \instantiates Qt3DRender::QSpotLight |
| 102 | \inherits AbstractLight |
| 103 | \inqmlmodule Qt3D.Render |
| 104 | \since 5.5 |
| 105 | \brief Encapsulate a Spot Light object in a Qt 3D scene. |
| 106 | |
| 107 | A spotlight is a light source that emits a cone of light in a particular direction. |
| 108 | |
| 109 | A spotlight uses three attenuation factors to describe how the intensity of the light |
| 110 | decreases over distance. These factors are designed to be used together in calcuating total |
| 111 | attenuation. For the materials in Qt3D Extras the following formula is used, where distance |
| 112 | is the distance from the light to the surface being rendered: |
| 113 | |
| 114 | \code |
| 115 | totalAttenuation = 1.0 / (constantAttenuation + (linearAttenuation * distance) + (quadraticAttenuation * distance * distance)); |
| 116 | \endcode |
| 117 | |
| 118 | Custom materials may choose to interpret these factors differently. |
| 119 | */ |
| 120 | |
| 121 | /*! |
| 122 | \fn Qt3DRender::QSpotLight::QSpotLight(Qt3DCore::QNode *parent) |
| 123 | Constructs a new QSpotLight with the specified \a parent. |
| 124 | */ |
| 125 | QSpotLight::QSpotLight(QNode *parent) |
| 126 | : QAbstractLight(*new QSpotLightPrivate, parent) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | /*! \internal */ |
| 131 | QSpotLight::~QSpotLight() |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | /*! \internal */ |
| 136 | QSpotLight::QSpotLight(QSpotLightPrivate &dd, QNode *parent) |
| 137 | : QAbstractLight(dd, parent) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | /*! |
| 142 | \qmlproperty float Qt3D.Render::SpotLight::constantAttenuation |
| 143 | Specifies the constant attenuation of the spot light. |
| 144 | |
| 145 | \note The exact meaning and use of this property is up to the |
| 146 | material implementation. |
| 147 | */ |
| 148 | |
| 149 | /*! |
| 150 | \property Qt3DRender::QSpotLight::constantAttenuation |
| 151 | Specifies the constant attenuation of the spot light. |
| 152 | |
| 153 | \note The exact meaning and use of this property is up to the |
| 154 | material implementation. |
| 155 | */ |
| 156 | float QSpotLight::constantAttenuation() const |
| 157 | { |
| 158 | Q_D(const QSpotLight); |
| 159 | return d->m_shaderData->property(name: "constantAttenuation" ).toFloat(); |
| 160 | } |
| 161 | |
| 162 | void QSpotLight::setConstantAttenuation(float value) |
| 163 | { |
| 164 | Q_D(QSpotLight); |
| 165 | if (constantAttenuation() != value) { |
| 166 | d->m_shaderData->setProperty(name: "constantAttenuation" , value); |
| 167 | emit constantAttenuationChanged(constantAttenuation: value); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /*! |
| 172 | \qmlproperty float Qt3D.Render::SpotLight::linearAttenuation |
| 173 | Specifies the linear attenuation of the spot light. |
| 174 | |
| 175 | \note The exact meaning and use of this property is up to the |
| 176 | material implementation. |
| 177 | */ |
| 178 | |
| 179 | /*! |
| 180 | \property Qt3DRender::QSpotLight::linearAttenuation |
| 181 | Specifies the linear attenuation of the spot light. |
| 182 | |
| 183 | \note The exact meaning and use of this property is up to the |
| 184 | material implementation. |
| 185 | */ |
| 186 | float QSpotLight::linearAttenuation() const |
| 187 | { |
| 188 | Q_D(const QSpotLight); |
| 189 | return d->m_shaderData->property(name: "linearAttenuation" ).toFloat(); |
| 190 | } |
| 191 | |
| 192 | void QSpotLight::setLinearAttenuation(float value) |
| 193 | { |
| 194 | Q_D(QSpotLight); |
| 195 | if (linearAttenuation() != value) { |
| 196 | d->m_shaderData->setProperty(name: "linearAttenuation" , value); |
| 197 | emit linearAttenuationChanged(linearAttenuation: value); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /*! |
| 202 | \qmlproperty float Qt3D.Render::SpotLight::quadraticAttenuation |
| 203 | Specifies the quadratic attenuation of the spot light. |
| 204 | |
| 205 | \note The exact meaning and use of this property is up to the |
| 206 | material implementation. |
| 207 | */ |
| 208 | |
| 209 | /*! |
| 210 | \property Qt3DRender::QSpotLight::quadraticAttenuation |
| 211 | Specifies the quadratic attenuation of the spot light. |
| 212 | |
| 213 | \note The exact meaning and use of this property is up to the |
| 214 | material implementation. |
| 215 | */ |
| 216 | float QSpotLight::quadraticAttenuation() const |
| 217 | { |
| 218 | Q_D(const QSpotLight); |
| 219 | return d->m_shaderData->property(name: "quadraticAttenuation" ).toFloat(); |
| 220 | } |
| 221 | |
| 222 | void QSpotLight::setQuadraticAttenuation(float value) |
| 223 | { |
| 224 | Q_D(QSpotLight); |
| 225 | if (quadraticAttenuation() != value) { |
| 226 | d->m_shaderData->setProperty(name: "quadraticAttenuation" , value); |
| 227 | emit quadraticAttenuationChanged(quadraticAttenuation: value); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /*! |
| 232 | \qmlproperty vector3d Qt3D.Render::SpotLight::localDirection |
| 233 | Specifies the local direction of the spot light. |
| 234 | |
| 235 | \note The exact meaning and use of this property is up to the |
| 236 | material implementation. |
| 237 | */ |
| 238 | |
| 239 | /*! |
| 240 | \property Qt3DRender::QSpotLight::localDirection |
| 241 | Specifies the local direction of the spot light. |
| 242 | |
| 243 | \note The exact meaning and use of this property is up to the |
| 244 | material implementation. |
| 245 | */ |
| 246 | QVector3D QSpotLight::localDirection() const |
| 247 | { |
| 248 | Q_D(const QSpotLight); |
| 249 | return d->m_shaderData->property(name: "direction" ).value<QVector3D>(); |
| 250 | } |
| 251 | |
| 252 | /*! |
| 253 | \qmlproperty float Qt3D.Render::SpotLight::cutOffAngle |
| 254 | Specifies the cut off angle of the spot light. |
| 255 | |
| 256 | \note The exact meaning and use of this property is up to the |
| 257 | material implementation. |
| 258 | */ |
| 259 | |
| 260 | /*! |
| 261 | \property Qt3DRender::QSpotLight::cutOffAngle |
| 262 | Specifies the cut off angle of the spot light. |
| 263 | |
| 264 | \note The exact meaning and use of this property is up to the |
| 265 | material implementation. |
| 266 | */ |
| 267 | float QSpotLight::cutOffAngle() const |
| 268 | { |
| 269 | Q_D(const QSpotLight); |
| 270 | return d->m_shaderData->property(name: "cutOffAngle" ).toFloat(); |
| 271 | } |
| 272 | |
| 273 | void QSpotLight::setLocalDirection(const QVector3D &direction) |
| 274 | { |
| 275 | Q_D(QSpotLight); |
| 276 | if (localDirection() != direction) { |
| 277 | const QVector3D dir = direction.normalized(); |
| 278 | d->m_shaderData->setProperty(name: "direction" , value: dir); |
| 279 | emit localDirectionChanged(localDirection: dir); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void QSpotLight::setCutOffAngle(float value) |
| 284 | { |
| 285 | Q_D(QSpotLight); |
| 286 | if (cutOffAngle() != value) { |
| 287 | d->m_shaderData->setProperty(name: "cutOffAngle" , value); |
| 288 | emit cutOffAngleChanged(cutOffAngle: value); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | } // namespace Qt3DRender |
| 293 | |
| 294 | QT_END_NAMESPACE |
| 295 | |