Panda3D
 All Classes Functions Variables Enumerations
eggPrimitive.I
1 // Filename: eggPrimitive.I
2 // Created by: drose (16Jan99)
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: EggPrimitive::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE EggPrimitive::
22 EggPrimitive(const string &name): EggNode(name) {
23  _bface = false;
24  _connected_shading = S_unknown;
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: EggPrimitive::Copy constructor
29 // Access: Published
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 INLINE EggPrimitive::
33 EggPrimitive(const EggPrimitive &copy) :
34  EggNode(copy),
35  EggAttributes(copy),
36  _textures(copy._textures),
37  _material(copy._material),
38  _bface(copy._bface)
39 {
40  copy_vertices(copy);
41  _connected_shading = S_unknown;
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: EggPrimitive::Copy assignment operator
46 // Access: Published
47 // Description:
48 ////////////////////////////////////////////////////////////////////
49 INLINE EggPrimitive &EggPrimitive::
50 operator = (const EggPrimitive &copy) {
51  EggNode::operator = (copy);
52  EggAttributes::operator = (copy);
53  copy_vertices(copy);
54  _textures = copy._textures;
55  _material = copy._material;
56  _bface = copy._bface;
57  _connected_shading = S_unknown;
58  return *this;
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: EggPrimitive::Destructor
63 // Access: Published
64 // Description:
65 ////////////////////////////////////////////////////////////////////
66 INLINE EggPrimitive::
67 ~EggPrimitive() {
68  clear();
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: EggPrimitive::get_sort_name
73 // Access: Published
74 // Description: Returns the name of the primitive for the purposes of
75 // sorting primitives into different groups, if there is
76 // one.
77 //
78 // Presently, this is defined as the primitive name
79 // itself, unless it begins with a digit.
80 ////////////////////////////////////////////////////////////////////
81 INLINE string EggPrimitive::
82 get_sort_name() const {
83  const string &name = get_name();
84  if (!name.empty() && !isdigit(name[0])) {
85  return name;
86  }
87  return string();
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: EggPrimitive::clear_connected_shading
92 // Access: Published
93 // Description: Resets the connected_shading member in this
94 // primitive, so that get_connected_shading() will
95 // recompute a new value.
96 ////////////////////////////////////////////////////////////////////
97 INLINE void EggPrimitive::
99  _connected_shading = S_unknown;
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: EggPrimitive::get_connected_shading
104 // Access: Published
105 // Description: Determines what sort of shading properties this
106 // primitive's connected neighbors have.
107 //
108 // To get the most accurate results, you should first
109 // call clear_connected_shading() on all connected
110 // primitives (or on all primitives in the egg file).
111 // It might also be a good idea to call
112 // remove_unused_vertices() to ensure proper
113 // connectivity.
114 //
115 // You may find it easiest to call these other methods
116 // on the EggData root node (they are defined on
117 // EggGroupNode).
118 ////////////////////////////////////////////////////////////////////
119 INLINE EggPrimitive::Shading EggPrimitive::
121  if (_connected_shading == S_unknown) {
122  ((EggPrimitive *)this)->set_connected_shading(S_unknown, this);
123  }
124 
125  return _connected_shading;
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: EggPrimitive::set_texture
130 // Access: Published
131 // Description: Replaces the current list of textures with the
132 // indicated texture.
133 //
134 // This method is deprecated and is used in support of
135 // single-texturing only. Please use the multitexture
136 // variant add_texture instead.
137 ////////////////////////////////////////////////////////////////////
138 INLINE void EggPrimitive::
140  clear_texture();
141  add_texture(texture);
142 }
143 
144 ////////////////////////////////////////////////////////////////////
145 // Function: EggPrimitive::has_texture
146 // Access: Published
147 // Description: Returns true if the primitive has any textures
148 // specified, false otherwise.
149 //
150 // This method is deprecated and is used in support of
151 // single-texturing only. New code should be written to
152 // use the multitexture variants instead.
153 ////////////////////////////////////////////////////////////////////
154 INLINE bool EggPrimitive::
155 has_texture() const {
156  return get_num_textures() > 0;
157 }
158 
159 ////////////////////////////////////////////////////////////////////
160 // Function: EggPrimitive::has_texture
161 // Access: Published
162 // Description: Returns true if the primitive has the particular
163 // indicated texture, false otherwise.
164 ////////////////////////////////////////////////////////////////////
165 INLINE bool EggPrimitive::
166 has_texture(EggTexture *texture) const {
167  PT_EggTexture t = texture;
168  return (::find(_textures.begin(), _textures.end(), t) != _textures.end());
169 }
170 
171 ////////////////////////////////////////////////////////////////////
172 // Function: EggPrimitive::get_texture
173 // Access: Published
174 // Description: Returns the first texture on the primitive, if any,
175 // or NULL if there are no textures on the primitive.
176 //
177 // This method is deprecated and is used in support of
178 // single-texturing only. New code should be written to
179 // use the multitexture variants instead.
180 ////////////////////////////////////////////////////////////////////
182 get_texture() const {
183  return has_texture() ? get_texture(0) : (EggTexture *)NULL;
184 }
185 
186 ////////////////////////////////////////////////////////////////////
187 // Function: EggPrimitive::add_texture
188 // Access: Published
189 // Description: Applies the indicated texture to the primitive.
190 //
191 // Note that, in the case of multiple textures being
192 // applied to a single primitive, the order in which the
193 // textures are applied does not affect the rendering
194 // order; use EggTexture::set_sort() to specify that.
195 ////////////////////////////////////////////////////////////////////
196 INLINE void EggPrimitive::
198  _textures.push_back(texture);
199 }
200 
201 ////////////////////////////////////////////////////////////////////
202 // Function: EggPrimitive::clear_texture
203 // Access: Published
204 // Description: Removes any texturing from the primitive.
205 ////////////////////////////////////////////////////////////////////
206 INLINE void EggPrimitive::
208  _textures.clear();
209 }
210 
211 ////////////////////////////////////////////////////////////////////
212 // Function: EggPrimitive::get_num_textures
213 // Access: Published
214 // Description: Returns the number of textures applied to the
215 // primitive.
216 ////////////////////////////////////////////////////////////////////
217 INLINE int EggPrimitive::
219  return _textures.size();
220 }
221 
222 ////////////////////////////////////////////////////////////////////
223 // Function: EggPrimitive::get_texture
224 // Access: Published
225 // Description: Returns the nth texture that has been applied to the
226 // primitive.
227 ////////////////////////////////////////////////////////////////////
229 get_texture(int n) const {
230  nassertr(n >= 0 && n < (int)_textures.size(), NULL);
231  return _textures[n];
232 }
233 
234 
235 ////////////////////////////////////////////////////////////////////
236 // Function: EggPrimitive::set_material
237 // Access: Published
238 // Description: Applies the indicated material to the primitive.
239 ////////////////////////////////////////////////////////////////////
240 INLINE void EggPrimitive::
242  _material = material;
243 }
244 
245 ////////////////////////////////////////////////////////////////////
246 // Function: EggPrimitive::clear_material
247 // Access: Published
248 // Description: Removes any material from the primitive.
249 ////////////////////////////////////////////////////////////////////
250 INLINE void EggPrimitive::
252  _material = (EggMaterial *)NULL;
253 }
254 
255 ////////////////////////////////////////////////////////////////////
256 // Function: EggPrimitive::get_material
257 // Access: Published
258 // Description: Returns a pointer to the applied material, or NULL if
259 // there is no material applied.
260 ////////////////////////////////////////////////////////////////////
262 get_material() const {
263  return _material;
264 }
265 
266 
267 ////////////////////////////////////////////////////////////////////
268 // Function: EggPrimitive::has_material
269 // Access: Published
270 // Description: Returns true if the primitive is materiald (and
271 // get_material() will return a real pointer), false
272 // otherwise (and get_material() will return NULL).
273 ////////////////////////////////////////////////////////////////////
274 INLINE bool EggPrimitive::
275 has_material() const {
276  return _material != (EggMaterial *)NULL;
277 }
278 
279 
280 ////////////////////////////////////////////////////////////////////
281 // Function: EggPrimitive::set_bface_flag
282 // Access: Published
283 // Description: Sets the backfacing flag of the polygon. If this is
284 // true, the polygon will be rendered so that both faces
285 // are visible; if it is false, only the front face of
286 // the polygon will be visible.
287 ////////////////////////////////////////////////////////////////////
288 INLINE void EggPrimitive::
289 set_bface_flag(bool flag) {
290  _bface = flag;
291 }
292 
293 
294 ////////////////////////////////////////////////////////////////////
295 // Function: EggPrimitive::get_bface_flag
296 // Access: Published
297 // Description: Retrieves the backfacing flag of the polygon. See
298 // set_bface_flag().
299 ////////////////////////////////////////////////////////////////////
300 INLINE bool EggPrimitive::
301 get_bface_flag() const {
302  return _bface;
303 }
304 
305 
306 ////////////////////////////////////////////////////////////////////
307 // Function: EggPrimitive::begin
308 // Access: Public
309 // Description:
310 ////////////////////////////////////////////////////////////////////
311 INLINE EggPrimitive::iterator EggPrimitive::
312 begin() const {
313  return _vertices.begin();
314 }
315 
316 ////////////////////////////////////////////////////////////////////
317 // Function: EggPrimitive::end
318 // Access: Public
319 // Description:
320 ////////////////////////////////////////////////////////////////////
321 INLINE EggPrimitive::iterator EggPrimitive::
322 end() const {
323  return _vertices.end();
324 }
325 
326 ////////////////////////////////////////////////////////////////////
327 // Function: EggPrimitive::rbegin
328 // Access: Public
329 // Description:
330 ////////////////////////////////////////////////////////////////////
331 INLINE EggPrimitive::reverse_iterator EggPrimitive::
332 rbegin() const {
333  return _vertices.rbegin();
334 }
335 
336 ////////////////////////////////////////////////////////////////////
337 // Function: EggPrimitive::rend
338 // Access: Public
339 // Description:
340 ////////////////////////////////////////////////////////////////////
341 INLINE EggPrimitive::reverse_iterator EggPrimitive::
342 rend() const {
343  return _vertices.rend();
344 }
345 
346 ////////////////////////////////////////////////////////////////////
347 // Function: EggPrimitive::empty
348 // Access: Public
349 // Description:
350 ////////////////////////////////////////////////////////////////////
351 INLINE bool EggPrimitive::
352 empty() const {
353  return _vertices.empty();
354 }
355 
356 ////////////////////////////////////////////////////////////////////
357 // Function: EggPrimitive::size
358 // Access: Public
359 // Description:
360 ////////////////////////////////////////////////////////////////////
361 INLINE EggPrimitive::size_type EggPrimitive::
362 size() const {
363  return _vertices.size();
364 }
365 
366 ////////////////////////////////////////////////////////////////////
367 // Function: EggPrimitive::Indexing operator
368 // Access: Public
369 // Description: This is read-only: you can't assign directly to an
370 // indexed vertex. See set_vertex() instead.
371 ////////////////////////////////////////////////////////////////////
373 operator [] (int index) const {
374  nassertr(index >= 0 && index < (int)size(), NULL);
375  return *(begin() + index);
376 }
377 
378 ////////////////////////////////////////////////////////////////////
379 // Function: EggPrimitive::insert
380 // Access: Public
381 // Description:
382 ////////////////////////////////////////////////////////////////////
383 INLINE EggPrimitive::iterator EggPrimitive::
384 insert(iterator position, EggVertex *x) {
385  prepare_add_vertex(x, position - _vertices.begin(), _vertices.size() + 1);
386  iterator i = _vertices.insert((Vertices::iterator &)position, x);
387  x->test_pref_integrity();
388  test_vref_integrity();
389  return i;
390 }
391 
392 ////////////////////////////////////////////////////////////////////
393 // Function: EggPrimitive::erase
394 // Access: Public
395 // Description:
396 ////////////////////////////////////////////////////////////////////
397 INLINE EggPrimitive::iterator EggPrimitive::
398 erase(iterator position) {
399  prepare_remove_vertex(*position, position - _vertices.begin(), _vertices.size());
400  iterator i = _vertices.erase((Vertices::iterator &)position);
401  test_vref_integrity();
402  return i;
403 }
404 
405 ////////////////////////////////////////////////////////////////////
406 // Function: EggPrimitive::replace
407 // Access: Public
408 // Description: Replaces the vertex at the indicated position with
409 // the indicated vertex. It is an error to call this
410 // with an invalid position iterator (e.g. end()).
411 ////////////////////////////////////////////////////////////////////
412 INLINE void EggPrimitive::
413 replace(iterator position, EggVertex *x) {
414  nassertv(position != end());
415 
416  // We pass -1 for i and n so that EggCompositePrimitive won't try to
417  // adjust its _components list.
418  prepare_remove_vertex(*position, -1, -1);
419  prepare_add_vertex(x, -1, -1);
420  *(Vertices::iterator &)position = x;
421 
422  x->test_pref_integrity();
423  test_vref_integrity();
424 }
425 
426 ////////////////////////////////////////////////////////////////////
427 // Function: EggPrimitive::clear
428 // Access: Published
429 // Description: Removes all of the vertices from the primitive.
430 ////////////////////////////////////////////////////////////////////
431 INLINE void EggPrimitive::
432 clear() {
433  erase(begin(), end());
434 }
435 
436 ////////////////////////////////////////////////////////////////////
437 // Function: EggPrimitive::get_num_vertices
438 // Access: Published
439 // Description:
440 ////////////////////////////////////////////////////////////////////
441 INLINE int EggPrimitive::
442 get_num_vertices() const {
443  return size();
444 }
445 
446 ////////////////////////////////////////////////////////////////////
447 // Function: EggPrimitive::set_vertex
448 // Access: Published
449 // Description: Replaces a particular vertex based on its index
450 // number in the list of vertices. This is just a
451 // convenience function for people who don't want to
452 // mess with the iterators.
453 ////////////////////////////////////////////////////////////////////
454 INLINE void EggPrimitive::
455 set_vertex(int index, EggVertex *vertex) {
456  nassertv(index >= 0 && index < (int)size());
457  replace(begin() + index, vertex);
458 }
459 
460 ////////////////////////////////////////////////////////////////////
461 // Function: EggPrimitive::get_vertex
462 // Access: Published
463 // Description: Returns a particular index based on its index number.
464 ////////////////////////////////////////////////////////////////////
466 get_vertex(int index) const {
467  nassertr(index >= 0 && index < (int)size(), NULL);
468  return *(begin() + index);
469 }
470 
471 
472 ////////////////////////////////////////////////////////////////////
473 // Function: EggPrimitive::get_pool
474 // Access: Published
475 // Description: Returns the vertex pool associated with the vertices
476 // of the primitive, or NULL if the primitive has no
477 // vertices.
478 ////////////////////////////////////////////////////////////////////
480 get_pool() const {
481  return empty() ? (EggVertexPool *)NULL : _vertices.front()->get_pool();
482 }
A base class for any of a number of kinds of geometry primitives: polygons, point lights...
Definition: eggPrimitive.h:51
void add_texture(EggTexture *texture)
Applies the indicated texture to the primitive.
Definition: eggPrimitive.I:197
bool get_bface_flag() const
Retrieves the backfacing flag of the polygon.
Definition: eggPrimitive.I:301
EggVertex * operator[](int index) const
This is read-only: you can&#39;t assign directly to an indexed vertex.
Definition: eggPrimitive.I:373
void clear()
Removes all of the vertices from the primitive.
Definition: eggPrimitive.I:432
Defines a texture map that may be applied to geometry.
Definition: eggTexture.h:33
void set_vertex(int index, EggVertex *vertex)
Replaces a particular vertex based on its index number in the list of vertices.
Definition: eggPrimitive.I:455
void copy_vertices(const EggPrimitive &other)
Replaces the current primitive&#39;s list of vertices with a copy of the list of vertices on the other pr...
void set_texture(EggTexture *texture)
Replaces the current list of textures with the indicated texture.
Definition: eggPrimitive.I:139
EggTexture * get_texture() const
Returns the first texture on the primitive, if any, or NULL if there are no textures on the primitive...
Definition: eggPrimitive.I:182
void set_bface_flag(bool flag)
Sets the backfacing flag of the polygon.
Definition: eggPrimitive.I:289
The set of attributes that may be applied to vertices as well as polygons, such as surface normal and...
Definition: eggAttributes.h:37
size_type size() const
Returns the number of EggTextures in the collection.
EggMaterial * get_material() const
Returns a pointer to the applied material, or NULL if there is no material applied.
Definition: eggPrimitive.I:262
void set_material(EggMaterial *material)
Applies the indicated material to the primitive.
Definition: eggPrimitive.I:241
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal...
Definition: eggVertex.h:41
void clear_texture()
Removes any texturing from the primitive.
Definition: eggPrimitive.I:207
string get_sort_name() const
Returns the name of the primitive for the purposes of sorting primitives into different groups...
Definition: eggPrimitive.I:82
iterator find(EggVertex *vertex)
Returns the iterator pointing to the indicated vertex, or end() if the vertex is not part of the prim...
int get_num_textures() const
Returns the number of textures applied to the primitive.
Definition: eggPrimitive.I:218
void clear_material()
Removes any material from the primitive.
Definition: eggPrimitive.I:251
bool has_material() const
Returns true if the primitive is materiald (and get_material() will return a real pointer)...
Definition: eggPrimitive.I:275
Shading get_connected_shading() const
Determines what sort of shading properties this primitive&#39;s connected neighbors have.
Definition: eggPrimitive.I:120
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:38
void replace(iterator position, EggVertex *vertex)
Replaces the vertex at the indicated position with the indicated vertex.
Definition: eggPrimitive.I:413
EggVertex * get_vertex(int index) const
Returns a particular index based on its index number.
Definition: eggPrimitive.I:466
A collection of vertices.
Definition: eggVertexPool.h:46
void clear_connected_shading()
Resets the connected_shading member in this primitive, so that get_connected_shading() will recompute...
Definition: eggPrimitive.I:98
bool has_texture() const
Returns true if the primitive has any textures specified, false otherwise.
Definition: eggPrimitive.I:155
EggVertexPool * get_pool() const
Returns the vertex pool associated with the vertices of the primitive, or NULL if the primitive has n...
Definition: eggPrimitive.I:480