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