| 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 "qdiffusemapmaterial.h" | 
| 41 | #include "qdiffusemapmaterial_p.h" | 
| 42 |  | 
| 43 | #include <Qt3DRender/qfilterkey.h> | 
| 44 | #include <Qt3DRender/qmaterial.h> | 
| 45 | #include <Qt3DRender/qeffect.h> | 
| 46 | #include <Qt3DRender/qtexture.h> | 
| 47 | #include <Qt3DRender/qtechnique.h> | 
| 48 | #include <Qt3DRender/qshaderprogram.h> | 
| 49 | #include <Qt3DRender/qshaderprogrambuilder.h> | 
| 50 | #include <Qt3DRender/qparameter.h> | 
| 51 | #include <Qt3DRender/qrenderpass.h> | 
| 52 | #include <Qt3DRender/qgraphicsapifilter.h> | 
| 53 | #include <QtCore/QUrl> | 
| 54 | #include <QtGui/QVector3D> | 
| 55 | #include <QtGui/QVector4D> | 
| 56 |  | 
| 57 | QT_BEGIN_NAMESPACE | 
| 58 |  | 
| 59 | using namespace Qt3DRender; | 
| 60 |  | 
| 61 | namespace Qt3DExtras { | 
| 62 |  | 
| 63 | QDiffuseMapMaterialPrivate::() | 
| 64 |     : QMaterialPrivate() | 
| 65 |     , m_diffuseMapEffect(new QEffect()) | 
| 66 |     , m_diffuseTexture(new QTexture2D()) | 
| 67 |     , m_ambientParameter(new QParameter(QStringLiteral("ka" ), QColor::fromRgbF(r: 0.05f, g: 0.05f, b: 0.05f, a: 1.0f))) | 
| 68 |     , m_diffuseParameter(new QParameter(QStringLiteral("diffuseTexture" ), m_diffuseTexture)) | 
| 69 |     , m_specularParameter(new QParameter(QStringLiteral("ks" ), QColor::fromRgbF(r: 0.01f, g: 0.01f, b: 0.01f, a: 1.0f))) | 
| 70 |     , m_shininessParameter(new QParameter(QStringLiteral("shininess" ), 150.0f)) | 
| 71 |     , m_textureScaleParameter(new QParameter(QStringLiteral("texCoordScale" ), 1.0f)) | 
| 72 |     , m_diffuseMapGL3Technique(new QTechnique()) | 
| 73 |     , m_diffuseMapGL2Technique(new QTechnique()) | 
| 74 |     , m_diffuseMapES2Technique(new QTechnique()) | 
| 75 |     , m_diffuseMapRHITechnique(new QTechnique()) | 
| 76 |     , m_diffuseMapGL3RenderPass(new QRenderPass()) | 
| 77 |     , m_diffuseMapGL2RenderPass(new QRenderPass()) | 
| 78 |     , m_diffuseMapES2RenderPass(new QRenderPass()) | 
| 79 |     , m_diffuseMapRHIRenderPass(new QRenderPass()) | 
| 80 |     , m_diffuseMapGL3Shader(new QShaderProgram()) | 
| 81 |     , m_diffuseMapGL3ShaderBuilder(new QShaderProgramBuilder()) | 
| 82 |     , m_diffuseMapGL2ES2Shader(new QShaderProgram()) | 
| 83 |     , m_diffuseMapGL2ES2ShaderBuilder(new QShaderProgramBuilder()) | 
| 84 |     , m_diffuseMapRHIShader(new QShaderProgram()) | 
| 85 |     , m_diffuseMapRHIShaderBuilder(new QShaderProgramBuilder()) | 
| 86 |     , m_filterKey(new QFilterKey) | 
| 87 | { | 
| 88 |     m_diffuseTexture->setMagnificationFilter(QAbstractTexture::Linear); | 
| 89 |     m_diffuseTexture->setMinificationFilter(QAbstractTexture::LinearMipMapLinear); | 
| 90 |     m_diffuseTexture->setWrapMode(QTextureWrapMode(QTextureWrapMode::Repeat)); | 
| 91 |     m_diffuseTexture->setGenerateMipMaps(true); | 
| 92 |     m_diffuseTexture->setMaximumAnisotropy(16.0f); | 
| 93 | } | 
| 94 |  | 
| 95 | void QDiffuseMapMaterialPrivate::() | 
| 96 | { | 
| 97 |     Q_Q(QDiffuseMapMaterial); | 
| 98 |  | 
| 99 |     connect(sender: m_ambientParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 100 |             receiverPrivate: this, slot: &QDiffuseMapMaterialPrivate::handleAmbientChanged); | 
| 101 |     connect(sender: m_diffuseParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 102 |             receiverPrivate: this, slot: &QDiffuseMapMaterialPrivate::handleDiffuseChanged); | 
| 103 |     connect(sender: m_specularParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 104 |             receiverPrivate: this, slot: &QDiffuseMapMaterialPrivate::handleSpecularChanged); | 
| 105 |     connect(sender: m_shininessParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 106 |             receiverPrivate: this, slot: &QDiffuseMapMaterialPrivate::handleShininessChanged); | 
| 107 |     connect(sender: m_textureScaleParameter, signal: &Qt3DRender::QParameter::valueChanged, | 
| 108 |             receiverPrivate: this, slot: &QDiffuseMapMaterialPrivate::handleTextureScaleChanged); | 
| 109 |  | 
| 110 |     m_diffuseMapGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/default.vert" )))); | 
| 111 |     m_diffuseMapGL3ShaderBuilder->setParent(q); | 
| 112 |     m_diffuseMapGL3ShaderBuilder->setShaderProgram(m_diffuseMapGL3Shader); | 
| 113 |     m_diffuseMapGL3ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json" ))); | 
| 114 |     m_diffuseMapGL3ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture" ), | 
| 115 |                                                     QStringLiteral("specular" ), | 
| 116 |                                                     QStringLiteral("normal" )}); | 
| 117 |  | 
| 118 |     m_diffuseMapGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/default.vert" )))); | 
| 119 |     m_diffuseMapGL2ES2ShaderBuilder->setParent(q); | 
| 120 |     m_diffuseMapGL2ES2ShaderBuilder->setShaderProgram(m_diffuseMapGL2ES2Shader); | 
| 121 |     m_diffuseMapGL2ES2ShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json" ))); | 
| 122 |     m_diffuseMapGL2ES2ShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture" ), | 
| 123 |                                                        QStringLiteral("specular" ), | 
| 124 |                                                        QStringLiteral("normal" )}); | 
| 125 |  | 
| 126 |     m_diffuseMapRHIShader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/rhi/default.vert" )))); | 
| 127 |     m_diffuseMapRHIShaderBuilder->setParent(q); | 
| 128 |     m_diffuseMapRHIShaderBuilder->setShaderProgram(m_diffuseMapRHIShader); | 
| 129 |     m_diffuseMapRHIShaderBuilder->setFragmentShaderGraph(QUrl(QStringLiteral("qrc:/shaders/graphs/phong.frag.json" ))); | 
| 130 |     m_diffuseMapRHIShaderBuilder->setEnabledLayers({QStringLiteral("diffuseTexture" ), | 
| 131 |                                                     QStringLiteral("specular" ), | 
| 132 |                                                     QStringLiteral("normal" )}); | 
| 133 |  | 
| 134 |     m_diffuseMapGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); | 
| 135 |     m_diffuseMapGL3Technique->graphicsApiFilter()->setMajorVersion(3); | 
| 136 |     m_diffuseMapGL3Technique->graphicsApiFilter()->setMinorVersion(1); | 
| 137 |     m_diffuseMapGL3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile); | 
| 138 |  | 
| 139 |     m_diffuseMapGL2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL); | 
| 140 |     m_diffuseMapGL2Technique->graphicsApiFilter()->setMajorVersion(2); | 
| 141 |     m_diffuseMapGL2Technique->graphicsApiFilter()->setMinorVersion(0); | 
| 142 |     m_diffuseMapGL2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); | 
| 143 |  | 
| 144 |     m_diffuseMapES2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGLES); | 
| 145 |     m_diffuseMapES2Technique->graphicsApiFilter()->setMajorVersion(2); | 
| 146 |     m_diffuseMapES2Technique->graphicsApiFilter()->setMinorVersion(0); | 
| 147 |     m_diffuseMapES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile); | 
| 148 |  | 
| 149 |     m_diffuseMapRHITechnique->graphicsApiFilter()->setApi(QGraphicsApiFilter::RHI); | 
| 150 |     m_diffuseMapRHITechnique->graphicsApiFilter()->setMajorVersion(1); | 
| 151 |     m_diffuseMapRHITechnique->graphicsApiFilter()->setMinorVersion(0); | 
| 152 |  | 
| 153 |     m_filterKey->setParent(q); | 
| 154 |     m_filterKey->setName(QStringLiteral("renderingStyle" )); | 
| 155 |     m_filterKey->setValue(QStringLiteral("forward" )); | 
| 156 |  | 
| 157 |     m_diffuseMapGL3Technique->addFilterKey(filterKey: m_filterKey); | 
| 158 |     m_diffuseMapGL2Technique->addFilterKey(filterKey: m_filterKey); | 
| 159 |     m_diffuseMapES2Technique->addFilterKey(filterKey: m_filterKey); | 
| 160 |     m_diffuseMapRHITechnique->addFilterKey(filterKey: m_filterKey); | 
| 161 |  | 
| 162 |     m_diffuseMapGL3RenderPass->setShaderProgram(m_diffuseMapGL3Shader); | 
| 163 |     m_diffuseMapGL2RenderPass->setShaderProgram(m_diffuseMapGL2ES2Shader); | 
| 164 |     m_diffuseMapES2RenderPass->setShaderProgram(m_diffuseMapGL2ES2Shader); | 
| 165 |     m_diffuseMapRHIRenderPass->setShaderProgram(m_diffuseMapRHIShader); | 
| 166 |  | 
| 167 |     m_diffuseMapGL3Technique->addRenderPass(pass: m_diffuseMapGL3RenderPass); | 
| 168 |     m_diffuseMapGL2Technique->addRenderPass(pass: m_diffuseMapGL2RenderPass); | 
| 169 |     m_diffuseMapES2Technique->addRenderPass(pass: m_diffuseMapES2RenderPass); | 
| 170 |     m_diffuseMapRHITechnique->addRenderPass(pass: m_diffuseMapRHIRenderPass); | 
| 171 |  | 
| 172 |     m_diffuseMapEffect->addTechnique(t: m_diffuseMapGL3Technique); | 
| 173 |     m_diffuseMapEffect->addTechnique(t: m_diffuseMapGL2Technique); | 
| 174 |     m_diffuseMapEffect->addTechnique(t: m_diffuseMapES2Technique); | 
| 175 |     m_diffuseMapEffect->addTechnique(t: m_diffuseMapRHITechnique); | 
| 176 |  | 
| 177 |     m_diffuseMapEffect->addParameter(parameter: m_ambientParameter); | 
| 178 |     m_diffuseMapEffect->addParameter(parameter: m_diffuseParameter); | 
| 179 |     m_diffuseMapEffect->addParameter(parameter: m_specularParameter); | 
| 180 |     m_diffuseMapEffect->addParameter(parameter: m_shininessParameter); | 
| 181 |     m_diffuseMapEffect->addParameter(parameter: m_textureScaleParameter); | 
| 182 |  | 
| 183 |     q->setEffect(m_diffuseMapEffect); | 
| 184 | } | 
| 185 |  | 
| 186 | void QDiffuseMapMaterialPrivate::handleAmbientChanged(const QVariant &var) | 
| 187 | { | 
| 188 |     Q_Q(QDiffuseMapMaterial); | 
| 189 |     emit q->ambientChanged(ambient: var.value<QColor>()); | 
| 190 | } | 
| 191 |  | 
| 192 | void QDiffuseMapMaterialPrivate::handleDiffuseChanged(const QVariant &var) | 
| 193 | { | 
| 194 |     Q_Q(QDiffuseMapMaterial); | 
| 195 |     emit q->diffuseChanged(diffuse: var.value<QAbstractTexture *>()); | 
| 196 | } | 
| 197 |  | 
| 198 | void QDiffuseMapMaterialPrivate::handleSpecularChanged(const QVariant &var) | 
| 199 | { | 
| 200 |     Q_Q(QDiffuseMapMaterial); | 
| 201 |     emit q->specularChanged(specular: var.value<QColor>()); | 
| 202 | } | 
| 203 |  | 
| 204 | void QDiffuseMapMaterialPrivate::handleShininessChanged(const QVariant &var) | 
| 205 | { | 
| 206 |     Q_Q(QDiffuseMapMaterial); | 
| 207 |     emit q->shininessChanged(shininess: var.toFloat()); | 
| 208 | } | 
| 209 |  | 
| 210 | void QDiffuseMapMaterialPrivate::handleTextureScaleChanged(const QVariant &var) | 
| 211 | { | 
| 212 |     Q_Q(QDiffuseMapMaterial); | 
| 213 |     emit q->textureScaleChanged(textureScale: var.toFloat()); | 
| 214 | } | 
| 215 |  | 
| 216 | /*! | 
| 217 |     \class Qt3DExtras::QDiffuseMapMaterial | 
| 218 |     \brief The QDiffuseMapMaterial provides a default implementation of the phong lighting effect | 
| 219 |     where the diffuse light component is read from a texture map. | 
| 220 |     \inmodule Qt3DExtras | 
| 221 |     \since 5.7 | 
| 222 |     \inherits Qt3DRender::QMaterial | 
| 223 |  | 
| 224 |     \deprecated | 
| 225 |     This class is deprecated; use Qt3DExtras::QDiffuseSpecularMaterial instead. | 
| 226 |  | 
| 227 |     The specular lighting effect is based on the combination of 3 lighting components ambient, | 
| 228 |     diffuse and specular. The relative strengths of these components are controlled by means of | 
| 229 |     their reflectivity coefficients which are modelled as RGB triplets: | 
| 230 |  | 
| 231 |     \list | 
| 232 |     \li Ambient is the color that is emitted by an object without any other light source. | 
| 233 |     \li Diffuse is the color that is emitted for rought surface reflections with the lights. | 
| 234 |     \li Specular is the color emitted for shiny surface reflections with the lights. | 
| 235 |     \li The shininess of a surface is controlled by a float property. | 
| 236 |     \endlist | 
| 237 |  | 
| 238 |     This material uses an effect with a single render pass approach and performs per fragment | 
| 239 |     lighting. Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2. | 
| 240 | */ | 
| 241 |  | 
| 242 | /*! | 
| 243 |     Constructs a new QDiffuseMapMaterial instance with parent object \a parent. | 
| 244 |  */ | 
| 245 | QDiffuseMapMaterial::(QNode *parent) | 
| 246 |     : QMaterial(*new QDiffuseMapMaterialPrivate, parent) | 
| 247 | { | 
| 248 |     Q_D(QDiffuseMapMaterial); | 
| 249 |     d->init(); | 
| 250 | } | 
| 251 |  | 
| 252 | /*! | 
| 253 |     Destroys the QDiffuseMapMaterial instance. | 
| 254 | */ | 
| 255 | QDiffuseMapMaterial::() | 
| 256 | { | 
| 257 | } | 
| 258 |  | 
| 259 | /*! | 
| 260 |     \property QDiffuseMapMaterial::ambient | 
| 261 |  | 
| 262 |     Holds the current ambient color. | 
| 263 | */ | 
| 264 |  | 
| 265 | QColor QDiffuseMapMaterial::() const | 
| 266 | { | 
| 267 |     Q_D(const QDiffuseMapMaterial); | 
| 268 |     return d->m_ambientParameter->value().value<QColor>(); | 
| 269 | } | 
| 270 |  | 
| 271 | /*! | 
| 272 |     \property QDiffuseMapMaterial::specular | 
| 273 |  | 
| 274 |     Holds the current specular color. | 
| 275 | */ | 
| 276 | QColor QDiffuseMapMaterial::() const | 
| 277 | { | 
| 278 |     Q_D(const QDiffuseMapMaterial); | 
| 279 |     return d->m_specularParameter->value().value<QColor>(); | 
| 280 | } | 
| 281 |  | 
| 282 | /*! | 
| 283 |     \property QDiffuseMapMaterial::shininess | 
| 284 |  | 
| 285 |     Holds the current shininess as a float value. | 
| 286 | */ | 
| 287 | float QDiffuseMapMaterial::() const | 
| 288 | { | 
| 289 |     Q_D(const QDiffuseMapMaterial); | 
| 290 |     return d->m_shininessParameter->value().toFloat(); | 
| 291 | } | 
| 292 |  | 
| 293 | /*! | 
| 294 |     \property QDiffuseMapMaterial::diffuse | 
| 295 |  | 
| 296 |     Holds the current texture used as the diffuse map. | 
| 297 |  | 
| 298 |     By default, the diffuse texture has the following properties: | 
| 299 |  | 
| 300 |     \list | 
| 301 |         \li Linear minification and magnification filters | 
| 302 |         \li Linear mipmap with mipmapping enabled | 
| 303 |         \li Repeat wrap mode | 
| 304 |         \li Maximum anisotropy of 16.0 | 
| 305 |     \endlist | 
| 306 | */ | 
| 307 | QAbstractTexture *QDiffuseMapMaterial::() const | 
| 308 | { | 
| 309 |     Q_D(const QDiffuseMapMaterial); | 
| 310 |     return d->m_diffuseParameter->value().value<QAbstractTexture *>(); | 
| 311 | } | 
| 312 |  | 
| 313 | /*! | 
| 314 |     \property QDiffuseMapMaterial::textureScale | 
| 315 |  | 
| 316 |     Holds the current texture scale. It is applied as a multiplier to texture | 
| 317 |     coordinates at render time. Defaults to 1.0. | 
| 318 |  | 
| 319 |     When used in conjunction with QTextureWrapMode::Repeat, textureScale provides a simple | 
| 320 |     way to tile a texture across a surface. For example, a texture scale of \c 4.0 | 
| 321 |     would result in 16 (4x4) tiles. | 
| 322 | */ | 
| 323 | float QDiffuseMapMaterial::() const | 
| 324 | { | 
| 325 |     Q_D(const QDiffuseMapMaterial); | 
| 326 |     return d->m_textureScaleParameter->value().toFloat(); | 
| 327 | } | 
| 328 |  | 
| 329 | void QDiffuseMapMaterial::(const QColor &ambient) | 
| 330 | { | 
| 331 |     Q_D(const QDiffuseMapMaterial); | 
| 332 |     d->m_ambientParameter->setValue(ambient); | 
| 333 | } | 
| 334 |  | 
| 335 | void QDiffuseMapMaterial::(const QColor &specular) | 
| 336 | { | 
| 337 |     Q_D(QDiffuseMapMaterial); | 
| 338 |     d->m_specularParameter->setValue(specular); | 
| 339 | } | 
| 340 |  | 
| 341 | void QDiffuseMapMaterial::(float shininess) | 
| 342 | { | 
| 343 |     Q_D(QDiffuseMapMaterial); | 
| 344 |     d->m_shininessParameter->setValue(shininess); | 
| 345 | } | 
| 346 |  | 
| 347 | void QDiffuseMapMaterial::(QAbstractTexture *diffuseMap) | 
| 348 | { | 
| 349 |     Q_D(QDiffuseMapMaterial); | 
| 350 |     d->m_diffuseParameter->setValue(QVariant::fromValue(value: diffuseMap)); | 
| 351 | } | 
| 352 |  | 
| 353 | void QDiffuseMapMaterial::(float textureScale) | 
| 354 | { | 
| 355 |     Q_D(QDiffuseMapMaterial); | 
| 356 |     d->m_textureScaleParameter->setValue(textureScale); | 
| 357 | } | 
| 358 |  | 
| 359 | } // namespace Qt3DExtras | 
| 360 |  | 
| 361 | QT_END_NAMESPACE | 
| 362 |  |