| 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 "qpointlight.h" | 
| 41 | #include "qpointlight_p.h" | 
| 42 |  | 
| 43 | QT_BEGIN_NAMESPACE | 
| 44 |  | 
| 45 | namespace Qt3DRender { | 
| 46 |  | 
| 47 | /* | 
| 48 |   Expected Shader struct | 
| 49 |  | 
| 50 |   \code | 
| 51 |  | 
| 52 |   struct PointLight | 
| 53 |   { | 
| 54 |    vec3 position; | 
| 55 |    vec4 color; | 
| 56 |    float intensity; | 
| 57 |   }; | 
| 58 |  | 
| 59 |   uniform PointLight pointLights[10]; | 
| 60 |  | 
| 61 |   \endcode | 
| 62 |  */ | 
| 63 |  | 
| 64 | QPointLightPrivate::QPointLightPrivate() | 
| 65 |     : QAbstractLightPrivate(QAbstractLight::PointLight) | 
| 66 | { | 
| 67 |     m_shaderData->setProperty(name: "constantAttenuation" , value: 1.0f); | 
| 68 |     m_shaderData->setProperty(name: "linearAttenuation" , value: 0.0f); | 
| 69 |     m_shaderData->setProperty(name: "quadraticAttenuation" , value: 0.0f); | 
| 70 | } | 
| 71 |  | 
| 72 | /*! | 
| 73 |   \class Qt3DRender::QPointLight | 
| 74 |   \inmodule Qt3DRender | 
| 75 |   \since 5.5 | 
| 76 |     \brief Encapsulate a Point Light object in a Qt 3D scene. | 
| 77 |  | 
| 78 |     A point light is a light source that emits light in all directions, from a single point. | 
| 79 |     Conceptually, this is similar to light given off by a standard light bulb. | 
| 80 |  | 
| 81 |     A point light uses three attenuation factors to describe how the intensity of the light | 
| 82 |     decreases over distance. These factors are designed to be used together in calcuating total | 
| 83 |     attenuation. For the materials in Qt3D Extras the following formula is used, where distance | 
| 84 |     is the distance from the light to the surface being rendered: | 
| 85 |  | 
| 86 |     \code | 
| 87 |     totalAttenuation = 1.0 / (constantAttenuation + (linearAttenuation * distance) + (quadraticAttenuation * distance * distance)); | 
| 88 |     \endcode | 
| 89 |  | 
| 90 |     Custom materials may choose to interpret these factors differently. | 
| 91 |  */ | 
| 92 |  | 
| 93 | /*! | 
| 94 |     \qmltype PointLight | 
| 95 |     \instantiates Qt3DRender::QPointLight | 
| 96 |     \inherits AbstractLight | 
| 97 |     \inqmlmodule Qt3D.Render | 
| 98 |     \since 5.5 | 
| 99 |     \brief Encapsulate a Point Light object in a Qt 3D scene. | 
| 100 |  | 
| 101 |     A point light is a light source that emits light in all directions, from a single point. | 
| 102 |     Conceptually, this is similar to light given off by a standard light bulb. | 
| 103 |  | 
| 104 |     A point light uses three attenuation factors to describe how the intensity of the light | 
| 105 |     decreases over distance. These factors are designed to be used together in calcuating total | 
| 106 |     attenuation. For the materials in Qt3D Extras the following formula is used, where distance | 
| 107 |     is the distance from the light to the surface being rendered: | 
| 108 |  | 
| 109 |     \code | 
| 110 |     totalAttenuation = 1.0 / (constantAttenuation + (linearAttenuation * distance) + (quadraticAttenuation * distance * distance)); | 
| 111 |     \endcode | 
| 112 |  | 
| 113 |     Custom materials may choose to interpret these factors differently. | 
| 114 | */ | 
| 115 |  | 
| 116 | /*! | 
| 117 |   \fn Qt3DRender::QPointLight::QPointLight(Qt3DCore::QNode *parent) | 
| 118 |   Constructs a new QPointLight with the specified \a parent. | 
| 119 |  */ | 
| 120 | QPointLight::QPointLight(QNode *parent) | 
| 121 |     : QAbstractLight(*new QPointLightPrivate, parent) | 
| 122 | { | 
| 123 | } | 
| 124 |  | 
| 125 | /*! \internal */ | 
| 126 | QPointLight::~QPointLight() | 
| 127 | { | 
| 128 | } | 
| 129 |  | 
| 130 | /*! \internal */ | 
| 131 | QPointLight::QPointLight(QPointLightPrivate &dd, QNode *parent) | 
| 132 |     : QAbstractLight(dd, parent) | 
| 133 | { | 
| 134 | } | 
| 135 |  | 
| 136 | /*! | 
| 137 |   \qmlproperty float Qt3D.Render::PointLight::constantAttenuation | 
| 138 |     Specifies the constant attenuation of the point light. | 
| 139 |  | 
| 140 |     \note The exact meaning and use of this property is up to the | 
| 141 |           material implementation. | 
| 142 | */ | 
| 143 |  | 
| 144 | /*! | 
| 145 |   \property Qt3DRender::QPointLight::constantAttenuation | 
| 146 |     Specifies the constant attenuation of the point light. | 
| 147 |  | 
| 148 |     \note The exact meaning and use of this property is up to the | 
| 149 |           material implementation. | 
| 150 |  */ | 
| 151 | float QPointLight::constantAttenuation() const | 
| 152 | { | 
| 153 |     Q_D(const QPointLight); | 
| 154 |     return d->m_shaderData->property(name: "constantAttenuation" ).toFloat(); | 
| 155 | } | 
| 156 |  | 
| 157 | void QPointLight::setConstantAttenuation(float value) | 
| 158 | { | 
| 159 |     Q_D(QPointLight); | 
| 160 |     if (constantAttenuation() != value) { | 
| 161 |         d->m_shaderData->setProperty(name: "constantAttenuation" , value); | 
| 162 |         emit constantAttenuationChanged(constantAttenuation: value); | 
| 163 |     } | 
| 164 | } | 
| 165 |  | 
| 166 | /*! | 
| 167 |   \qmlproperty float Qt3D.Render::PointLight::linearAttenuation | 
| 168 |     Specifies the linear attenuation of the point light. | 
| 169 |  | 
| 170 |     \note The exact meaning and use of this property is up to the | 
| 171 |           material implementation. | 
| 172 | */ | 
| 173 |  | 
| 174 | /*! | 
| 175 |   \property Qt3DRender::QPointLight::linearAttenuation | 
| 176 |     Specifies the linear attenuation of the point light. | 
| 177 |  | 
| 178 |     \note The exact meaning and use of this property is up to the | 
| 179 |           material implementation. | 
| 180 |  */ | 
| 181 | float QPointLight::linearAttenuation() const | 
| 182 | { | 
| 183 |     Q_D(const QPointLight); | 
| 184 |     return d->m_shaderData->property(name: "linearAttenuation" ).toFloat(); | 
| 185 | } | 
| 186 |  | 
| 187 | void QPointLight::setLinearAttenuation(float value) | 
| 188 | { | 
| 189 |     Q_D(QPointLight); | 
| 190 |     if (linearAttenuation() != value) { | 
| 191 |         d->m_shaderData->setProperty(name: "linearAttenuation" , value); | 
| 192 |         emit linearAttenuationChanged(linearAttenuation: value); | 
| 193 |     } | 
| 194 | } | 
| 195 |  | 
| 196 | /*! | 
| 197 |   \qmlproperty float Qt3D.Render::PointLight::quadraticAttenuation | 
| 198 |     Specifies the quadratic attenuation of the point light. | 
| 199 |  | 
| 200 |     \note The exact meaning and use of this property is up to the | 
| 201 |           material implementation. | 
| 202 | */ | 
| 203 |  | 
| 204 | /*! | 
| 205 |   \property Qt3DRender::QPointLight::quadraticAttenuation | 
| 206 |     Specifies the quadratic attenuation of the point light. | 
| 207 |  | 
| 208 |     \note The exact meaning and use of this property is up to the | 
| 209 |           material implementation. | 
| 210 |  */ | 
| 211 | float QPointLight::quadraticAttenuation() const | 
| 212 | { | 
| 213 |     Q_D(const QPointLight); | 
| 214 |     return d->m_shaderData->property(name: "quadraticAttenuation" ).toFloat(); | 
| 215 | } | 
| 216 |  | 
| 217 | void QPointLight::setQuadraticAttenuation(float value) | 
| 218 | { | 
| 219 |     Q_D(QPointLight); | 
| 220 |     if (quadraticAttenuation() != value) { | 
| 221 |         d->m_shaderData->setProperty(name: "quadraticAttenuation" , value); | 
| 222 |         emit quadraticAttenuationChanged(quadraticAttenuation: value); | 
| 223 |     } | 
| 224 | } | 
| 225 |  | 
| 226 | } // namespace Qt3DRender | 
| 227 |  | 
| 228 | QT_END_NAMESPACE | 
| 229 |  |