| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: http://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ | 
| 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 http://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free | 
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in | 
| 29 | ** the packaging of this file. Please review the following information to | 
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be | 
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. | 
| 32 | ** | 
| 33 | ** $QT_END_LICENSE$ | 
| 34 | ** | 
| 35 | ****************************************************************************/ | 
| 36 |  | 
| 37 | #include "qmorphphongmaterial.h" | 
| 38 | #include "qmorphphongmaterial_p.h" | 
| 39 | #include <Qt3DRender/qfilterkey.h> | 
| 40 | #include <Qt3DRender/qmaterial.h> | 
| 41 | #include <Qt3DRender/qeffect.h> | 
| 42 | #include <Qt3DRender/qtechnique.h> | 
| 43 | #include <Qt3DRender/qshaderprogram.h> | 
| 44 | #include <Qt3DRender/qparameter.h> | 
| 45 | #include <Qt3DRender/qrenderpass.h> | 
| 46 | #include <Qt3DRender/qgraphicsapifilter.h> | 
| 47 | #include <Qt3DRender/qshaderprogrambuilder.h> | 
| 48 | #include <QUrl> | 
| 49 | #include <QVector3D> | 
| 50 | #include <QVector4D> | 
| 51 |  | 
| 52 | QT_BEGIN_NAMESPACE | 
| 53 |  | 
| 54 | using namespace Qt3DRender; | 
| 55 |  | 
| 56 | namespace Qt3DExtras { | 
| 57 |  | 
| 58 | QMorphPhongMaterialPrivate::() | 
| 59 |     : QMaterialPrivate() | 
| 60 |     , m_phongEffect(new QEffect()) | 
| 61 |     , m_ambientParameter(new QParameter(QStringLiteral("ka" ), QColor::fromRgbF(r: 0.05, g: 0.05, b: 0.05, a: 1.0))) | 
| 62 |     , m_diffuseParameter(new QParameter(QStringLiteral("kd" ), QColor::fromRgbF(r: 0.7, g: 0.7, b: 0.7, a: 1.0))) | 
| 63 |     , m_specularParameter(new QParameter(QStringLiteral("ks" ), QColor::fromRgbF(r: 0.01, g: 0.01, b: 0.01, a: 1.0))) | 
| 64 |     , m_shininessParameter(new QParameter(QStringLiteral("shininess" ), 150.0f)) | 
| 65 |     , m_interpolatorParameter(new QParameter(QStringLiteral("interpolator" ), 0.0f)) | 
| 66 |     , m_phongGL3Technique(new QTechnique()) | 
| 67 |     , m_phongGL2Technique(new QTechnique()) | 
| 68 |     , m_phongES2Technique(new QTechnique()) | 
| 69 |     , m_phongRHITechnique(new QTechnique()) | 
| 70 |     , m_phongGL3RenderPass(new QRenderPass()) | 
| 71 |     , m_phongGL2RenderPass(new QRenderPass()) | 
| 72 |     , m_phongES2RenderPass(new QRenderPass()) | 
| 73 |     , m_phongRHIRenderPass(new QRenderPass()) | 
| 74 |     , m_phongGL3Shader(new QShaderProgram()) | 
| 75 |     , m_phongGL2ES2Shader(new QShaderProgram()) | 
| 76 |     , m_phongRHIShader(new QShaderProgram()) | 
| 77 |     , m_phongGL3ShaderBuilder(new QShaderProgramBuilder()) | 
| 78 |     , m_phongGL2ES2ShaderBuilder(new QShaderProgramBuilder()) | 
| 79 |     , m_phongRHIShaderBuilder(new QShaderProgramBuilder()) | 
| 80 |     , m_filterKey(new QFilterKey) | 
| 81 | { | 
| 82 | } | 
| 83 |  | 
| 84 | void QMorphPhongMaterialPrivate::() | 
| 85 | { | 
| 86 |     Q_Q(QMorphPhongMaterial); | 
| 87 |  | 
| 88 |     connect(sender: m_ambientParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 89 |             receiverPrivate: this, slot: &QMorphPhongMaterialPrivate::handleAmbientChanged); | 
| 90 |     connect(sender: m_diffuseParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 91 |             receiverPrivate: this, slot: &QMorphPhongMaterialPrivate::handleDiffuseChanged); | 
| 92 |     connect(sender: m_specularParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 93 |             receiverPrivate: this, slot: &QMorphPhongMaterialPrivate::handleSpecularChanged); | 
| 94 |     connect(sender: m_shininessParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 95 |             receiverPrivate: this, slot: &QMorphPhongMaterialPrivate::handleShininessChanged); | 
| 96 |     connect(sender: m_interpolatorParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 97 |             receiverPrivate: this, slot: &QMorphPhongMaterialPrivate::handleInterpolatorChanged); | 
| 98 |  | 
| 99 |     m_phongGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/morphphong.vert" )))); | 
| 100 |     m_phongGL3ShaderBuilder->setParent(q); | 
| 101 |     m_phongGL3ShaderBuilder->setShaderProgram(m_phongGL3Shader); | 
| 102 |     m_phongGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json" ))); | 
| 103 |     m_phongGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse" ), | 
| 104 |                                                QStringLiteral("specular" ), | 
| 105 |                                                QStringLiteral("normal" )}); | 
| 106 |  | 
| 107 |     m_phongGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/morphphong.vert" )))); | 
| 108 |     m_phongGL2ES2ShaderBuilder->setParent(q); | 
| 109 |     m_phongGL2ES2ShaderBuilder->setShaderProgram(m_phongGL2ES2Shader); | 
| 110 |     m_phongGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json" ))); | 
| 111 |     m_phongGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuse" ), | 
| 112 |                                                   QStringLiteral("specular" ), | 
| 113 |                                                   QStringLiteral("normal" )}); | 
| 114 |  | 
| 115 |     m_phongRHIShader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/rhi/morphphong.vert" )))); | 
| 116 |     m_phongRHIShaderBuilder->setParent(q); | 
| 117 |     m_phongRHIShaderBuilder->setShaderProgram(m_phongRHIShader); | 
| 118 |     m_phongRHIShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json" ))); | 
| 119 |     m_phongRHIShaderBuilder->setEnabledLayers({QStringLiteral("diffuse" ), | 
| 120 |                                                QStringLiteral("specular" ), | 
| 121 |                                                QStringLiteral("normal" )}); | 
| 122 |  | 
| 123 |     m_phongGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); | 
| 124 |     m_phongGL3Technique->graphicsApiFilter()->setMajorVersion(3); | 
| 125 |     m_phongGL3Technique->graphicsApiFilter()->setMinorVersion(1); | 
| 126 |     m_phongGL3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile); | 
| 127 |  | 
| 128 |     m_phongGL2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); | 
| 129 |     m_phongGL2Technique->graphicsApiFilter()->setMajorVersion(2); | 
| 130 |     m_phongGL2Technique->graphicsApiFilter()->setMinorVersion(0); | 
| 131 |     m_phongGL2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); | 
| 132 |  | 
| 133 |     m_phongES2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGLES); | 
| 134 |     m_phongES2Technique->graphicsApiFilter()->setMajorVersion(2); | 
| 135 |     m_phongES2Technique->graphicsApiFilter()->setMinorVersion(0); | 
| 136 |     m_phongES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); | 
| 137 |  | 
| 138 |     m_phongRHITechnique->graphicsApiFilter()->setApi(QGraphicsApiFilter::RHI); | 
| 139 |     m_phongRHITechnique->graphicsApiFilter()->setMajorVersion(1); | 
| 140 |     m_phongRHITechnique->graphicsApiFilter()->setMinorVersion(0); | 
| 141 |  | 
| 142 |     m_phongGL3RenderPass->setShaderProgram(m_phongGL3Shader); | 
| 143 |     m_phongGL2RenderPass->setShaderProgram(m_phongGL2ES2Shader); | 
| 144 |     m_phongES2RenderPass->setShaderProgram(m_phongGL2ES2Shader); | 
| 145 |     m_phongRHIRenderPass->setShaderProgram(m_phongRHIShader); | 
| 146 |  | 
| 147 |     m_phongGL3Technique->addRenderPass(pass: m_phongGL3RenderPass); | 
| 148 |     m_phongGL2Technique->addRenderPass(pass: m_phongGL2RenderPass); | 
| 149 |     m_phongES2Technique->addRenderPass(pass: m_phongES2RenderPass); | 
| 150 |     m_phongRHITechnique->addRenderPass(pass: m_phongRHIRenderPass); | 
| 151 |  | 
| 152 |     m_filterKey->setParent(q); | 
| 153 |     m_filterKey->setName(QStringLiteral("renderingStyle" )); | 
| 154 |     m_filterKey->setValue(QStringLiteral("forward" )); | 
| 155 |  | 
| 156 |     m_phongGL3Technique->addFilterKey(filterKey: m_filterKey); | 
| 157 |     m_phongGL2Technique->addFilterKey(filterKey: m_filterKey); | 
| 158 |     m_phongES2Technique->addFilterKey(filterKey: m_filterKey); | 
| 159 |     m_phongRHITechnique->addFilterKey(filterKey: m_filterKey); | 
| 160 |  | 
| 161 |     m_phongEffect->addTechnique(t: m_phongGL3Technique); | 
| 162 |     m_phongEffect->addTechnique(t: m_phongGL2Technique); | 
| 163 |     m_phongEffect->addTechnique(t: m_phongES2Technique); | 
| 164 |     m_phongEffect->addTechnique(t: m_phongRHITechnique); | 
| 165 |  | 
| 166 |     m_phongEffect->addParameter(parameter: m_ambientParameter); | 
| 167 |     m_phongEffect->addParameter(parameter: m_diffuseParameter); | 
| 168 |     m_phongEffect->addParameter(parameter: m_specularParameter); | 
| 169 |     m_phongEffect->addParameter(parameter: m_shininessParameter); | 
| 170 |     m_phongEffect->addParameter(parameter: m_interpolatorParameter); | 
| 171 |  | 
| 172 |     q->setEffect(m_phongEffect); | 
| 173 | } | 
| 174 |  | 
| 175 | void QMorphPhongMaterialPrivate::handleAmbientChanged(const QVariant &var) | 
| 176 | { | 
| 177 |     Q_Q(QMorphPhongMaterial); | 
| 178 |     emit q->ambientChanged(ambient: var.value<QColor>()); | 
| 179 | } | 
| 180 |  | 
| 181 | void QMorphPhongMaterialPrivate::handleDiffuseChanged(const QVariant &var) | 
| 182 | { | 
| 183 |     Q_Q(QMorphPhongMaterial); | 
| 184 |     emit q->diffuseChanged(diffuse: var.value<QColor>()); | 
| 185 | } | 
| 186 |  | 
| 187 | void QMorphPhongMaterialPrivate::handleSpecularChanged(const QVariant &var) | 
| 188 | { | 
| 189 |     Q_Q(QMorphPhongMaterial); | 
| 190 |     emit q->specularChanged(specular: var.value<QColor>()); | 
| 191 | } | 
| 192 |  | 
| 193 | void QMorphPhongMaterialPrivate::handleShininessChanged(const QVariant &var) | 
| 194 | { | 
| 195 |     Q_Q(QMorphPhongMaterial); | 
| 196 |     emit q->shininessChanged(shininess: var.toFloat()); | 
| 197 | } | 
| 198 |  | 
| 199 | void QMorphPhongMaterialPrivate::handleInterpolatorChanged(const QVariant &var) | 
| 200 | { | 
| 201 |     Q_Q(QMorphPhongMaterial); | 
| 202 |     emit q->interpolatorChanged(interpolator: var.toFloat()); | 
| 203 | } | 
| 204 |  | 
| 205 | /*! | 
| 206 |     \class Qt3DExtras::QMorphPhongMaterial | 
| 207 |     \ingroup qt3d-extras-materials | 
| 208 |     \brief The QMorphPhongMaterial class provides a default implementation of the phong lighting effect. | 
| 209 |     \inmodule Qt3DExtras | 
| 210 |     \since 5.7 | 
| 211 |     \inherits Qt3DRender::QMaterial | 
| 212 |  | 
| 213 |     The phong lighting effect is based on the combination of 3 lighting components ambient, diffuse | 
| 214 |     and specular. The relative strengths of these components are controlled by means of their | 
| 215 |     reflectivity coefficients which are modelled as RGB triplets: | 
| 216 |  | 
| 217 |     \list | 
| 218 |     \li Ambient is the color that is emitted by an object without any other light source. | 
| 219 |     \li Diffuse is the color that is emitted for rought surface reflections with the lights. | 
| 220 |     \li Specular is the color emitted for shiny surface reflections with the lights. | 
| 221 |     \li The shininess of a surface is controlled by a float property. | 
| 222 |     \endlist | 
| 223 |  | 
| 224 |     This material uses an effect with a single render pass approach and performs per fragment | 
| 225 |     lighting. Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. | 
| 226 | */ | 
| 227 |  | 
| 228 | /*! | 
| 229 |     Constructs a new QMorphPhongMaterial instance with parent object \a parent. | 
| 230 | */ | 
| 231 | QMorphPhongMaterial::(QNode *parent) | 
| 232 |     : QMaterial(*new QMorphPhongMaterialPrivate, parent) | 
| 233 | { | 
| 234 |     Q_D(QMorphPhongMaterial); | 
| 235 |     d->init(); | 
| 236 | } | 
| 237 |  | 
| 238 | /*! | 
| 239 |    Destroys the QMorphPhongMaterial. | 
| 240 | */ | 
| 241 | QMorphPhongMaterial::() | 
| 242 | { | 
| 243 | } | 
| 244 |  | 
| 245 | /*! | 
| 246 |     \property QMorphPhongMaterial::ambient | 
| 247 |  | 
| 248 |     Holds the ambient color. | 
| 249 | */ | 
| 250 | QColor QMorphPhongMaterial::() const | 
| 251 | { | 
| 252 |     Q_D(const QMorphPhongMaterial); | 
| 253 |     return d->m_ambientParameter->value().value<QColor>(); | 
| 254 | } | 
| 255 |  | 
| 256 | /*! | 
| 257 |     \property QMorphPhongMaterial::diffuse | 
| 258 |  | 
| 259 |     Holds the diffuse color. | 
| 260 | */ | 
| 261 | QColor QMorphPhongMaterial::() const | 
| 262 | { | 
| 263 |     Q_D(const QMorphPhongMaterial); | 
| 264 |     return d->m_diffuseParameter->value().value<QColor>(); | 
| 265 | } | 
| 266 |  | 
| 267 | /*! | 
| 268 |     \property QMorphPhongMaterial::specular | 
| 269 |  | 
| 270 |     Holds the specular color. | 
| 271 | */ | 
| 272 | QColor QMorphPhongMaterial::() const | 
| 273 | { | 
| 274 |     Q_D(const QMorphPhongMaterial); | 
| 275 |     return d->m_specularParameter->value().value<QColor>(); | 
| 276 | } | 
| 277 |  | 
| 278 | /*! | 
| 279 |     \property QMorphPhongMaterial::shininess | 
| 280 |  | 
| 281 |     Holds the shininess exponent. | 
| 282 | */ | 
| 283 | float QMorphPhongMaterial::() const | 
| 284 | { | 
| 285 |     Q_D(const QMorphPhongMaterial); | 
| 286 |     return d->m_shininessParameter->value().toFloat(); | 
| 287 | } | 
| 288 |  | 
| 289 | /*! | 
| 290 |     \property QMorphPhongMaterial::interpolator | 
| 291 |  | 
| 292 |     Contains the interpolation method of the Phong lighting effect. | 
| 293 | */ | 
| 294 | float QMorphPhongMaterial::() const | 
| 295 | { | 
| 296 |     Q_D(const QMorphPhongMaterial); | 
| 297 |     return d->m_interpolatorParameter->value().toFloat(); | 
| 298 | } | 
| 299 |  | 
| 300 | void QMorphPhongMaterial::(const QColor &ambient) | 
| 301 | { | 
| 302 |     Q_D(QMorphPhongMaterial); | 
| 303 |     d->m_ambientParameter->setValue(ambient); | 
| 304 | } | 
| 305 |  | 
| 306 | void QMorphPhongMaterial::(const QColor &diffuse) | 
| 307 | { | 
| 308 |     Q_D(QMorphPhongMaterial); | 
| 309 |     d->m_diffuseParameter->setValue(diffuse); | 
| 310 | } | 
| 311 |  | 
| 312 | void QMorphPhongMaterial::(const QColor &specular) | 
| 313 | { | 
| 314 |     Q_D(QMorphPhongMaterial); | 
| 315 |     d->m_specularParameter->setValue(specular); | 
| 316 | } | 
| 317 |  | 
| 318 | void QMorphPhongMaterial::(float shininess) | 
| 319 | { | 
| 320 |     Q_D(QMorphPhongMaterial); | 
| 321 |     d->m_shininessParameter->setValue(shininess); | 
| 322 | } | 
| 323 |  | 
| 324 | void QMorphPhongMaterial::(float interpolator) | 
| 325 | { | 
| 326 |     Q_D(QMorphPhongMaterial); | 
| 327 |     d->m_interpolatorParameter->setValue(interpolator); | 
| 328 | } | 
| 329 |  | 
| 330 | } // namespace Qt3DExtras | 
| 331 |  | 
| 332 | QT_END_NAMESPACE | 
| 333 |  |