|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt3D module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "material.h" |
|
43 |
|
44 /*! |
|
45 \class Material |
|
46 \brief The Material class describes material properties for OpenGL drawing. |
|
47 \since 4.7 |
|
48 \ingroup qt3d |
|
49 \ingroup qt3d::painting |
|
50 */ |
|
51 |
|
52 class MaterialPrivate |
|
53 { |
|
54 public: |
|
55 explicit MaterialPrivate(); |
|
56 |
|
57 QColor basicColor; |
|
58 QColor ambientColor; |
|
59 QColor diffuseColor; |
|
60 QColor specularColor; |
|
61 QColor emittedLight; |
|
62 qreal shininess; |
|
63 }; |
|
64 |
|
65 MaterialPrivate::MaterialPrivate() |
|
66 : basicColor(255, 255, 255, 255), |
|
67 specularColor(0, 0, 0, 255), |
|
68 emittedLight(0, 0, 0, 255), |
|
69 shininess(0) |
|
70 { |
|
71 ambientColor.setRgbF(0.2f, 0.2f, 0.2f, 1.0f); |
|
72 diffuseColor.setRgbF(0.8f, 0.8f, 0.8f, 1.0f); |
|
73 } |
|
74 |
|
75 /*! |
|
76 Constructs a Material object with its default values, |
|
77 and attaches it to \a parent. |
|
78 */ |
|
79 Material::Material(QObject *parent) |
|
80 : QObject(parent), d_ptr(new MaterialPrivate) |
|
81 { |
|
82 } |
|
83 |
|
84 /*! |
|
85 Destroys this Material object. |
|
86 */ |
|
87 Material::~Material() |
|
88 { |
|
89 } |
|
90 |
|
91 /*! |
|
92 \property Material::basicColor |
|
93 \brief the basic color of the material. The default value |
|
94 is (1.0, 1.0, 1.0, 1.0). |
|
95 |
|
96 The basic color is used by effects that don't implement |
|
97 material-based lighting. It is ignored by material-based |
|
98 lighting effects. |
|
99 |
|
100 \sa ambientColor(), diffuseColor(), basicColorChanged(), setColor() |
|
101 */ |
|
102 QColor Material::basicColor() const |
|
103 { |
|
104 Q_D(const Material); |
|
105 return d->basicColor; |
|
106 } |
|
107 |
|
108 void Material::setBasicColor(const QColor& value) |
|
109 { |
|
110 Q_D(Material); |
|
111 if (d->basicColor != value) { |
|
112 d->basicColor = value; |
|
113 emit basicColorChanged(); |
|
114 emit materialChanged(); |
|
115 } |
|
116 } |
|
117 |
|
118 /*! |
|
119 \property Material::ambientColor |
|
120 \brief the ambient color of the material. The default value |
|
121 is (0.2, 0.2, 0.2, 1.0). |
|
122 |
|
123 \sa diffuseColor(), specularColor(), ambientColorChanged() |
|
124 */ |
|
125 QColor Material::ambientColor() const |
|
126 { |
|
127 Q_D(const Material); |
|
128 return d->ambientColor; |
|
129 } |
|
130 |
|
131 void Material::setAmbientColor(const QColor& value) |
|
132 { |
|
133 Q_D(Material); |
|
134 if (d->ambientColor != value) { |
|
135 d->ambientColor = value; |
|
136 emit ambientColorChanged(); |
|
137 emit materialChanged(); |
|
138 } |
|
139 } |
|
140 |
|
141 /*! |
|
142 \property Material::diffuseColor |
|
143 \brief the diffuse color of the material. The default value |
|
144 is (0.8, 0.8, 0.8, 1.0). |
|
145 |
|
146 \sa ambientColor(), specularColor(), diffuseColorChanged() |
|
147 */ |
|
148 QColor Material::diffuseColor() const |
|
149 { |
|
150 Q_D(const Material); |
|
151 return d->diffuseColor; |
|
152 } |
|
153 |
|
154 void Material::setDiffuseColor(const QColor& value) |
|
155 { |
|
156 Q_D(Material); |
|
157 if (d->diffuseColor != value) { |
|
158 d->diffuseColor = value; |
|
159 emit diffuseColorChanged(); |
|
160 emit materialChanged(); |
|
161 } |
|
162 } |
|
163 |
|
164 /*! |
|
165 \property Material::specularColor |
|
166 \brief the specular color of the material. The default value |
|
167 is (0, 0, 0, 1). |
|
168 |
|
169 \sa ambientColor(), diffuseColor(), specularColorChanged() |
|
170 */ |
|
171 QColor Material::specularColor() const |
|
172 { |
|
173 Q_D(const Material); |
|
174 return d->specularColor; |
|
175 } |
|
176 |
|
177 void Material::setSpecularColor(const QColor& value) |
|
178 { |
|
179 Q_D(Material); |
|
180 if (d->specularColor != value) { |
|
181 d->specularColor = value; |
|
182 emit specularColorChanged(); |
|
183 emit materialChanged(); |
|
184 } |
|
185 } |
|
186 |
|
187 /*! |
|
188 \property Material::emittedLight |
|
189 \brief the emitted light intensity of the material. |
|
190 The default value is (0.0, 0.0, 0.0, 1.0), which indicates |
|
191 that the material does not emit any light. |
|
192 |
|
193 \sa emittedLightChanged() |
|
194 */ |
|
195 QColor Material::emittedLight() const |
|
196 { |
|
197 Q_D(const Material); |
|
198 return d->emittedLight; |
|
199 } |
|
200 |
|
201 void Material::setEmittedLight(const QColor& value) |
|
202 { |
|
203 Q_D(Material); |
|
204 if (d->emittedLight != value) { |
|
205 d->emittedLight = value; |
|
206 emit emittedLightChanged(); |
|
207 emit materialChanged(); |
|
208 } |
|
209 } |
|
210 |
|
211 /*! |
|
212 \property Material::color |
|
213 \brief the overall color of the material. The default value |
|
214 is (1.0, 1.0, 1.0, 1.0). |
|
215 |
|
216 This is a convenience property. When read it returns basicColor(). |
|
217 When written, it sets basicColor() to the value, ambientColor() |
|
218 to 20% of the value, and diffuseColor() to 80% of the value. |
|
219 The result is that regardless of whether lighting is used or not, |
|
220 the material will appear to have a similar color. |
|
221 |
|
222 \sa basicColor(), ambientColor(), diffuseColor() |
|
223 */ |
|
224 QColor Material::color() const |
|
225 { |
|
226 Q_D(const Material); |
|
227 return d->basicColor; |
|
228 } |
|
229 |
|
230 void Material::setColor(const QColor& value) |
|
231 { |
|
232 Q_D(Material); |
|
233 d->basicColor = value; |
|
234 d->ambientColor.setRgbF |
|
235 (value.redF() * 0.2f, value.greenF() * 0.2f, |
|
236 value.blueF() * 0.2f, value.alphaF()); |
|
237 d->diffuseColor.setRgbF |
|
238 (value.redF() * 0.8f, value.greenF() * 0.8f, |
|
239 value.blueF() * 0.8f, value.alphaF()); |
|
240 emit basicColorChanged(); |
|
241 emit ambientColorChanged(); |
|
242 emit diffuseColorChanged(); |
|
243 emit materialChanged(); |
|
244 } |
|
245 |
|
246 /*! |
|
247 \property Material::shininess |
|
248 \brief the specular exponent of the material, or how shiny it is. |
|
249 Must be between 0 and 128. The default value is 0. A value outside |
|
250 this range will be clamped to the range when the property is set. |
|
251 |
|
252 \sa shininessChanged() |
|
253 */ |
|
254 qreal Material::shininess() const |
|
255 { |
|
256 Q_D(const Material); |
|
257 return d->shininess; |
|
258 } |
|
259 |
|
260 void Material::setShininess(qreal value) |
|
261 { |
|
262 Q_D(Material); |
|
263 if (value < 0) |
|
264 value = 0; |
|
265 else if (value > 128) |
|
266 value = 128; |
|
267 if (d->shininess != value) { |
|
268 d->shininess = value; |
|
269 emit shininessChanged(); |
|
270 emit materialChanged(); |
|
271 } |
|
272 } |
|
273 |
|
274 /*! |
|
275 \fn void Material::basicColorChanged() |
|
276 |
|
277 This signal is emitted when basicColor() changes. |
|
278 |
|
279 \sa basicColor(), setBasicColor(), materialChanged() |
|
280 */ |
|
281 |
|
282 /*! |
|
283 \fn void Material::ambientColorChanged() |
|
284 |
|
285 This signal is emitted when ambientColor() changes. |
|
286 |
|
287 \sa ambientColor(), setAmbientColor(), materialChanged() |
|
288 */ |
|
289 |
|
290 /*! |
|
291 \fn void Material::diffuseColorChanged() |
|
292 |
|
293 This signal is emitted when diffuseColor() changes. |
|
294 |
|
295 \sa diffuseColor(), setDiffuseColor(), materialChanged() |
|
296 */ |
|
297 |
|
298 /*! |
|
299 \fn void Material::specularColorChanged() |
|
300 |
|
301 This signal is emitted when specularColor() changes. |
|
302 |
|
303 \sa specularColor(), setSpecularColor(), materialChanged() |
|
304 */ |
|
305 |
|
306 /*! |
|
307 \fn void Material::emittedLightChanged() |
|
308 |
|
309 This signal is emitted when emittedLight() changes. |
|
310 |
|
311 \sa emittedLight(), setEmittedLight(), materialChanged() |
|
312 */ |
|
313 |
|
314 /*! |
|
315 \fn void Material::shininessChanged() |
|
316 |
|
317 This signal is emitted when shininess() changes. |
|
318 |
|
319 \sa shininess(), setShininess(), materialChanged() |
|
320 */ |
|
321 |
|
322 /*! |
|
323 \fn void Material::materialChanged() |
|
324 |
|
325 This signal is emitted when one of basicColor(), ambientColor(), |
|
326 diffuseColor(), specularColor(), emittedLight(), shiniess(), |
|
327 or texture() changes. |
|
328 |
|
329 \sa basicColorChanged(), ambientColorChanged(), diffuseColorChanged() |
|
330 \sa specularColorChanged(), emittedLightChanged(), shininessChanged() |
|
331 \sa texturesChanged() |
|
332 */ |