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

source code of qt3d/src/extras/defaults/qnormaldiffusemapmaterial.cpp