Panda3D
Loading...
Searching...
No Matches
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 */
17INLINE Material::
18Material(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 */
34INLINE Material::
35Material(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 */
52INLINE Material::
53~Material() {
54}
55
56/**
57 * Returns the default material.
58 */
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 */
71INLINE bool Material::
72has_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 */
80INLINE const LColor &Material::
81get_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 */
93INLINE bool Material::
94has_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 */
102INLINE const LColor &Material::
103get_ambient() const {
104 return _ambient;
105}
106
107/**
108 * Removes the explicit ambient color from the material.
109 */
110INLINE void Material::
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 */
123INLINE bool Material::
124has_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 */
132INLINE const LColor &Material::
133get_diffuse() const {
134 return _diffuse;
135}
136
137/**
138 * Removes the explicit diffuse color from the material.
139 */
140INLINE void Material::
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 */
153INLINE bool Material::
154has_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 */
162INLINE const LColor &Material::
163get_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 */
171INLINE bool Material::
172has_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 */
180INLINE const LColor &Material::
181get_emission() const {
182 return _emission;
183}
184
185/**
186 * Removes the explicit emission color from the material.
187 */
188INLINE void Material::
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 */
200INLINE PN_stdfloat Material::
201get_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 */
209INLINE bool Material::
210has_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 */
218INLINE bool Material::
219has_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 */
227INLINE PN_stdfloat Material::
228get_metallic() const {
229 return _metallic;
230}
231
232/**
233 * Returns true if a refractive index has explicitly been specified for this
234 * material.
235 */
236INLINE bool Material::
237has_refractive_index() const {
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 */
245INLINE PN_stdfloat Material::
246get_refractive_index() const {
247 return _refractive_index;
248}
249
250/**
251 * Returns the local viewer flag. Set set_local().
252 */
253INLINE bool Material::
254get_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 */
264INLINE void Material::
265set_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 */
279INLINE bool Material::
280get_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 */
289INLINE void Material::
290set_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 */
304INLINE bool Material::
305operator == (const Material &other) const {
306 return compare_to(other) == 0;
307}
308
309/**
310 *
311 */
312INLINE bool Material::
313operator != (const Material &other) const {
314 return compare_to(other) != 0;
315}
316
317/**
318 *
319 */
320INLINE bool Material::
321operator < (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 */
328INLINE bool Material::
329is_attrib_locked() const {
330 return (_flags & F_attrib_lock) != 0;
331}
332
333/**
334 * @deprecated This no longer has any meaning in 1.10.
335 */
336INLINE void Material::
338 _flags |= F_attrib_lock;
339}
340
341/**
342 * Internal. Returns true if a shader has been generated that uses this.
343 */
344INLINE bool Material::
345is_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 */
353INLINE void Material::
355 _flags |= F_used_by_auto_shader;
356}
357
358/**
359 *
360 */
361INLINE int Material::
362get_flags() const {
363 // F_used_by_auto_shader is an internal flag, ignore it.
364 return _flags & ~F_used_by_auto_shader;
365}
Defines the way an object appears in the presence of lighting.
Definition material.h:43
get_ambient
Returns the ambient color setting, if it has been set.
Definition material.h:114
has_specular
Returns true if the specular color has been explicitly set for this material, false otherwise.
Definition material.h:118
bool has_metallic() const
Returns true if the metallic has been explicitly set for this material, false otherwise.
Definition material.I:219
get_emission
Returns the emission color setting, if it has been set.
Definition material.h:120
has_diffuse
Returns true if the diffuse color has been explicitly set for this material, false otherwise.
Definition material.h:116
has_base_color
Returns true if the base color has been explicitly set for this material, false otherwise.
Definition material.h:112
get_base_color
Returns the base_color color setting, if it has been set.
Definition material.h:112
get_refractive_index
Returns the index of refraction, or 1 if none has been set for this material.
Definition material.h:126
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
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
clear_diffuse
Removes the explicit diffuse color from the material.
Definition material.h:116
clear_emission
Removes the explicit emission color from the material.
Definition material.h:120
get_specular
Returns the specular color setting, if it has been set.
Definition material.h:118
has_emission
Returns true if the emission color has been explicitly set for this material, false otherwise.
Definition material.h:120
has_ambient
Returns true if the ambient color has been explicitly set for this material, false otherwise.
Definition material.h:114
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
get_metallic
Returns the metallic setting, if it has been set.
Definition material.h:124
bool is_attrib_locked() const
Definition material.I:329
get_shininess
Returns the shininess exponent of the material.
Definition material.h:122
static Material * get_default()
Returns the default material.
Definition material.I:60
clear_ambient
Removes the explicit ambient color from the material.
Definition material.h:114
bool has_refractive_index() const
Returns true if a refractive index has explicitly been specified for this material.
Definition material.I:237
set_local
Sets the local viewer flag.
Definition material.h:128
get_diffuse
Returns the diffuse color setting, if it has been set.
Definition material.h:116
bool has_roughness() const
Returns true if the roughness has been explicitly set for this material, false otherwise.
Definition material.I:210
get_local
Returns the local viewer flag.
Definition material.h:128
void set_attrib_lock()
Definition material.I:337
A base class for all things which can have a name.
Definition namable.h:26