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