Panda3D
material.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file material.I
10  * @author mike
11  * @date 1999-02-05
12  */
13 
14 /**
15  *
16  */
17 INLINE Material::
18 Material(const std::string &name) : Namable(name) {
19  _base_color.set(1.0f, 1.0f, 1.0f, 1.0f);
20  _ambient.set(1.0f, 1.0f, 1.0f, 1.0f);
21  _diffuse.set(1.0f, 1.0f, 1.0f, 1.0f);
22  _specular.set(0.0f, 0.0f, 0.0f, 1.0f);
23  _emission.set(0.0f, 0.0f, 0.0f, 1.0f);
24  _shininess = 0;
25  _roughness = 1;
26  _metallic = 0;
27  _refractive_index = 1;
28  _flags = 0;
29 }
30 
31 /**
32  *
33  */
34 INLINE Material::
35 Material(const Material &copy) :
36  Namable(copy) ,
37  _base_color(copy._base_color),
38  _ambient(copy._ambient),
39  _diffuse(copy._diffuse),
40  _specular(copy._specular),
41  _emission(copy._emission),
42  _shininess(copy._shininess),
43  _roughness(copy._roughness),
44  _metallic(copy._metallic),
45  _refractive_index(copy._refractive_index),
46  _flags(copy._flags & ~(F_attrib_lock | F_used_by_auto_shader)) {
47 }
48 
49 /**
50  *
51  */
52 INLINE Material::
53 ~Material() {
54 }
55 
56 /**
57  * Returns the default material.
58  */
59 INLINE Material *Material::
61  if (_default == 0) {
62  _default = new Material("default");
63  }
64  return _default;
65 }
66 
67 /**
68  * Returns true if the base color has been explicitly set for this material,
69  * false otherwise.
70  */
71 INLINE bool Material::
72 has_base_color() const {
73  return (_flags & F_base_color) != 0;
74 }
75 
76 /**
77  * Returns the base_color color setting, if it has been set. If neither the
78  * base color nor the metallic have been set, this returns the diffuse color.
79  */
80 INLINE const LColor &Material::
81 get_base_color() const {
82  if (!has_base_color() && !has_metallic()) {
83  return _diffuse;
84  } else {
85  return _base_color;
86  }
87 }
88 
89 /**
90  * Returns true if the ambient color has been explicitly set for this
91  * material, false otherwise.
92  */
93 INLINE bool Material::
94 has_ambient() const {
95  return (_flags & F_ambient) != 0;
96 }
97 
98 /**
99  * Returns the ambient color setting, if it has been set. Returns (0,0,0,0)
100  * if the ambient color has not been set.
101  */
102 INLINE const LColor &Material::
103 get_ambient() const {
104  return _ambient;
105 }
106 
107 /**
108  * Removes the explicit ambient color from the material.
109  */
110 INLINE void Material::
111 clear_ambient() {
112  if (has_ambient() && is_used_by_auto_shader()) {
113  GraphicsStateGuardianBase::mark_rehash_generated_shaders();
114  }
115  _flags &= ~F_ambient;
116  _ambient = _base_color;
117 }
118 
119 /**
120  * Returns true if the diffuse color has been explicitly set for this
121  * material, false otherwise.
122  */
123 INLINE bool Material::
124 has_diffuse() const {
125  return (_flags & F_diffuse) != 0;
126 }
127 
128 /**
129  * Returns the diffuse color setting, if it has been set. Returns (1,1,1,1)
130  * if the diffuse color has not been set.
131  */
132 INLINE const LColor &Material::
133 get_diffuse() const {
134  return _diffuse;
135 }
136 
137 /**
138  * Removes the explicit diffuse color from the material.
139  */
140 INLINE void Material::
141 clear_diffuse() {
142  if (has_diffuse() && is_used_by_auto_shader()) {
143  GraphicsStateGuardianBase::mark_rehash_generated_shaders();
144  }
145  _flags &= ~F_diffuse;
146  _diffuse = _base_color * (1 - _metallic);
147 }
148 
149 /**
150  * Returns true if the specular color has been explicitly set for this
151  * material, false otherwise.
152  */
153 INLINE bool Material::
154 has_specular() const {
155  return (_flags & F_specular) != 0;
156 }
157 
158 /**
159  * Returns the specular color setting, if it has been set. Returns (0,0,0,0)
160  * if the specular color has not been set.
161  */
162 INLINE const LColor &Material::
163 get_specular() const {
164  return _specular;
165 }
166 
167 /**
168  * Returns true if the emission color has been explicitly set for this
169  * material, false otherwise.
170  */
171 INLINE bool Material::
172 has_emission() const {
173  return (_flags & F_emission) != 0;
174 }
175 
176 /**
177  * Returns the emission color setting, if it has been set. Returns (0,0,0,0)
178  * if the emission color has not been set.
179  */
180 INLINE const LColor &Material::
181 get_emission() const {
182  return _emission;
183 }
184 
185 /**
186  * Removes the explicit emission color from the material.
187  */
188 INLINE void Material::
189 clear_emission() {
190  if (has_emission() && is_used_by_auto_shader()) {
191  GraphicsStateGuardianBase::mark_rehash_generated_shaders();
192  }
193  _flags &= ~F_emission;
194  _emission.set(0.0f, 0.0f, 0.0f, 0.0f);
195 }
196 
197 /**
198  * Returns the shininess exponent of the material.
199  */
200 INLINE PN_stdfloat Material::
201 get_shininess() const {
202  return _shininess;
203 }
204 
205 /**
206  * Returns true if the roughness has been explicitly set for this material,
207  * false otherwise.
208  */
209 INLINE bool Material::
210 has_roughness() const {
211  return (_flags & F_roughness) != 0;
212 }
213 
214 /**
215  * Returns true if the metallic has been explicitly set for this material,
216  * false otherwise.
217  */
218 INLINE bool Material::
219 has_metallic() const {
220  return (_flags & F_metallic) != 0;
221 }
222 
223 /**
224  * Returns the metallic setting, if it has been set. Returns 0 if it has not
225  * been set.
226  */
227 INLINE PN_stdfloat Material::
228 get_metallic() const {
229  return _metallic;
230 }
231 
232 /**
233  * Returns true if a refractive index has explicitly been specified for this
234  * material.
235  */
236 INLINE bool Material::
238  return (_flags & F_refractive_index) != 0;
239 }
240 
241 /**
242  * Returns the index of refraction, or 1 if none has been set for this
243  * material.
244  */
245 INLINE PN_stdfloat Material::
246 get_refractive_index() const {
247  return _refractive_index;
248 }
249 
250 /**
251  * Returns the local viewer flag. Set set_local().
252  */
253 INLINE bool Material::
254 get_local() const {
255  return (_flags & F_local) != 0;
256 }
257 
258 /**
259  * Sets the local viewer flag. Set this true to enable camera-relative
260  * specular highlights, or false to use orthogonal specular highlights. The
261  * default value is true. Applications that use orthogonal projection should
262  * specify false.
263  */
264 INLINE void Material::
265 set_local(bool local) {
266  if (is_used_by_auto_shader() && get_local() != local) {
267  GraphicsStateGuardianBase::mark_rehash_generated_shaders();
268  }
269  if (local) {
270  _flags |= F_local;
271  } else {
272  _flags &= ~F_local;
273  }
274 }
275 
276 /**
277  * Returns the state of the two-sided lighting flag. See set_twoside().
278  */
279 INLINE bool Material::
280 get_twoside() const {
281  return (_flags & F_twoside) != 0;
282 }
283 
284 /**
285  * Set this true to enable two-sided lighting. When two-sided lighting is on,
286  * both sides of a polygon will be lit by this material. The default is for
287  * two-sided lighting to be off, in which case only the front surface is lit.
288  */
289 INLINE void Material::
290 set_twoside(bool twoside) {
291  if (is_used_by_auto_shader() && get_twoside() != twoside) {
292  GraphicsStateGuardianBase::mark_rehash_generated_shaders();
293  }
294  if (twoside) {
295  _flags |= F_twoside;
296  } else {
297  _flags &= ~F_twoside;
298  }
299 }
300 
301 /**
302  *
303  */
304 INLINE bool Material::
305 operator == (const Material &other) const {
306  return compare_to(other) == 0;
307 }
308 
309 /**
310  *
311  */
312 INLINE bool Material::
313 operator != (const Material &other) const {
314  return compare_to(other) != 0;
315 }
316 
317 /**
318  *
319  */
320 INLINE bool Material::
321 operator < (const Material &other) const {
322  return compare_to(other) < 0;
323 }
324 
325 /**
326  * @deprecated This no longer has any meaning in 1.10.
327  */
328 INLINE bool Material::
330  return (_flags & F_attrib_lock) != 0;
331 }
332 
333 /**
334  * @deprecated This no longer has any meaning in 1.10.
335  */
336 INLINE void Material::
338  _flags |= F_attrib_lock;
339 }
340 
341 /**
342  * Internal. Returns true if a shader has been generated that uses this.
343  */
344 INLINE bool Material::
345 is_used_by_auto_shader() const {
346  return (_flags & F_attrib_lock) != 0;
347 }
348 
349 /**
350  * Called by the shader generator to indicate that a shader has been generated
351  * that uses this material.
352  */
353 INLINE void Material::
355  _flags |= F_used_by_auto_shader;
356 }
357 
358 /**
359  *
360  */
361 INLINE int Material::
362 get_flags() const {
363  // F_used_by_auto_shader is an internal flag, ignore it.
364  return _flags & ~F_used_by_auto_shader;
365 }
has_ambient
Returns true if the ambient color has been explicitly set for this material, false otherwise.
Definition: material.h:114
bool has_metallic() const
Returns true if the metallic has been explicitly set for this material, false otherwise.
Definition: material.I:219
get_twoside
Returns the state of the two-sided lighting flag.
Definition: material.h:129
set_twoside
Set this true to enable two-sided lighting.
Definition: material.h:129
static Material * get_default()
Returns the default material.
Definition: material.I:60
get_local
Returns the local viewer flag.
Definition: material.h:128
has_base_color
Returns true if the base color has been explicitly set for this material, false otherwise.
Definition: material.h:112
has_diffuse
Returns true if the diffuse color has been explicitly set for this material, false otherwise.
Definition: material.h:116
int compare_to(const Material &other) const
Returns a number less than zero if this material sorts before the other one, greater than zero if it ...
Definition: material.cxx:349
void set_attrib_lock()
Definition: material.I:337
A base class for all things which can have a name.
Definition: namable.h:26
bool has_roughness() const
Returns true if the roughness has been explicitly set for this material, false otherwise.
Definition: material.I:210
set_local
Sets the local viewer flag.
Definition: material.h:128
bool is_attrib_locked() const
Definition: material.I:329
bool has_refractive_index() const
Returns true if a refractive index has explicitly been specified for this material.
Definition: material.I:237
Defines the way an object appears in the presence of lighting.
Definition: material.h:43
void mark_used_by_auto_shader()
Called by the shader generator to indicate that a shader has been generated that uses this material.
Definition: material.I:354
has_emission
Returns true if the emission color has been explicitly set for this material, false otherwise.
Definition: material.h:120