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