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