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

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