Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/builder/builderPrimTempl.I

Go to the documentation of this file.
00001 // Filename: builderPrimTempl.I
00002 // Created by:  drose (11Sep97)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include <notify.h>
00020 
00021 #include <algorithm>
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //     Function: BuilderPrimTempl::Constructor
00025 //       Access: Public
00026 //  Description:
00027 ////////////////////////////////////////////////////////////////////
00028 template <class VTX>
00029 INLINE BuilderPrimTempl<VTX>::
00030 BuilderPrimTempl() {
00031   clear();
00032 }
00033 
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: BuilderPrimTempl::Copy constructor
00037 //       Access: Public
00038 //  Description:
00039 ////////////////////////////////////////////////////////////////////
00040 template <class VTX>
00041 INLINE BuilderPrimTempl<VTX>::
00042 BuilderPrimTempl(const BuilderPrimTempl<VTX> &copy) {
00043   (*this) = copy;
00044 }
00045 
00046 
00047 ////////////////////////////////////////////////////////////////////
00048 //     Function: BuilderPrimTempl::Copy assignment operator
00049 //       Access: Public
00050 //  Description:
00051 ////////////////////////////////////////////////////////////////////
00052 template <class VTX>
00053 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00054 operator = (const BuilderPrimTempl<VTX> &copy) {
00055   DAttrib::operator = (copy);
00056   _verts = copy._verts;
00057   _components = copy._components;
00058   _type = copy._type;
00059   _overall = copy._overall;
00060 
00061   return *this;
00062 }
00063 
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: BuilderPrimTempl::has_normal
00067 //       Access: Public
00068 //  Description: Returns true if set_normal() has been called on the
00069 //               primitive.  This is unrelated to the normal values
00070 //               that may or may not override from the vertices.  Also
00071 //               see has_overall_normal().
00072 ////////////////////////////////////////////////////////////////////
00073 template <class VTX>
00074 INLINE bool BuilderPrimTempl<VTX>::
00075 has_normal() const {
00076   return DAttrib::has_normal();
00077 }
00078 
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: BuilderPrimTempl::has_color
00082 //       Access: Public
00083 //  Description: Returns true if set_color() has been called on the
00084 //               primitive.  This is unrelated to the color values
00085 //               that may or may not override from the vertices.  Also
00086 //               see has_overall_color().
00087 ////////////////////////////////////////////////////////////////////
00088 template <class VTX>
00089 INLINE bool BuilderPrimTempl<VTX>::
00090 has_color() const {
00091   return DAttrib::has_color();
00092 }
00093 
00094 
00095 ////////////////////////////////////////////////////////////////////
00096 //     Function: BuilderPrimTempl::has_pixel_size
00097 //       Access: Public
00098 //  Description: Returns true if set_pixel_size() has been called on
00099 //               the primitive.  This is unrelated to the pixel_size
00100 //               values that may or may not override from the
00101 //               vertices.  Also see has_overall_pixel_size().
00102 ////////////////////////////////////////////////////////////////////
00103 template <class VTX>
00104 INLINE bool BuilderPrimTempl<VTX>::
00105 has_pixel_size() const {
00106   return DAttrib::has_pixel_size();
00107 }
00108 
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: BuilderPrimTempl::has_overall_normal
00112 //       Access: Public
00113 //  Description: Returns true if the primitive has a single, overall
00114 //               normal value.  This can happen because of one of: (a)
00115 //               the primitive had a normal value assigned to it
00116 //               directly, and its individual vertices and components
00117 //               did not; (b) each vertex was assigned the same normal
00118 //               value; (c) each component was assigned the same normal
00119 //               value.
00120 //
00121 //               If has_overall_normal() returns true, then get_normal()
00122 //               will return the overall normal value.
00123 ////////////////////////////////////////////////////////////////////
00124 template <class VTX>
00125 INLINE bool BuilderPrimTempl<VTX>::
00126 has_overall_normal() const {
00127   if ((_overall & BAF_overall_updated) == 0) {
00128     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00129   }
00130   return (_overall & BAF_overall_normal)!=0;
00131 }
00132 
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: BuilderPrimTempl::has_overall_color
00136 //       Access: Public
00137 //  Description: Returns true if the primitive has a single, overall
00138 //               color value.  This can happen because of one of: (a)
00139 //               the primitive had a color value assigned to it
00140 //               directly, and its individual vertices and components
00141 //               did not; (b) each vertex was assigned the same color
00142 //               value; (c) each component was assigned the same color
00143 //               value.
00144 //
00145 //               If has_overall_color() returns true, then get_color()
00146 //               will return the overall color value.
00147 ////////////////////////////////////////////////////////////////////
00148 template <class VTX>
00149 INLINE bool BuilderPrimTempl<VTX>::
00150 has_overall_color() const {
00151   if ((_overall & BAF_overall_updated) == 0) {
00152     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00153   }
00154   return (_overall & BAF_overall_color)!=0;
00155 }
00156 
00157 
00158 ////////////////////////////////////////////////////////////////////
00159 //     Function: BuilderPrimTempl::has_overall_pixel_size
00160 //       Access: Public
00161 //  Description: Returns true if the primitive has a single, overall
00162 //               pixel_size value.  This can happen because of one of:
00163 //               (a) the primitive had a pixel_size value assigned to
00164 //               it directly, and its individual vertices and
00165 //               components did not; (b) each vertex was assigned the
00166 //               same pixel_size value; (c) each component was
00167 //               assigned the same pixel_size value.
00168 //
00169 //               If has_overall_pixel_size() returns true, then
00170 //               get_pixel_size() will return the overall pixel_size
00171 //               value.
00172 ////////////////////////////////////////////////////////////////////
00173 template <class VTX>
00174 INLINE bool BuilderPrimTempl<VTX>::
00175 has_overall_pixel_size() const {
00176   if ((_overall & BAF_overall_updated) == 0) {
00177     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00178   }
00179   return (_overall & BAF_overall_pixel_size)!=0;
00180 }
00181 
00182 
00183 ////////////////////////////////////////////////////////////////////
00184 //     Function: BuilderPrimTempl::has_vertex_normal
00185 //       Access: Public
00186 //  Description: Returns true if each of the primitive's vertices has
00187 //               a different normal value set.
00188 ////////////////////////////////////////////////////////////////////
00189 template <class VTX>
00190 INLINE bool BuilderPrimTempl<VTX>::
00191 has_vertex_normal() const {
00192   if ((_overall & BAF_overall_updated) == 0) {
00193     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00194   }
00195   return (_overall & BAF_vertex_normal)!=0;
00196 }
00197 
00198 
00199 ////////////////////////////////////////////////////////////////////
00200 //     Function: BuilderPrimTempl::has_vertex_color
00201 //       Access: Public
00202 //  Description: Returns true if each of the primitive's vertices has
00203 //               a different color value set.
00204 ////////////////////////////////////////////////////////////////////
00205 template <class VTX>
00206 INLINE bool BuilderPrimTempl<VTX>::
00207 has_vertex_color() const {
00208   if ((_overall & BAF_overall_updated) == 0) {
00209     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00210   }
00211   return (_overall & BAF_vertex_color)!=0;
00212 }
00213 
00214 
00215 ////////////////////////////////////////////////////////////////////
00216 //     Function: BuilderPrimTempl::has_vertex_texcoord
00217 //       Access: Public
00218 //  Description: Returns true if each of the primitive's vertices has
00219 //               a texcoord value set.
00220 ////////////////////////////////////////////////////////////////////
00221 template <class VTX>
00222 INLINE bool BuilderPrimTempl<VTX>::
00223 has_vertex_texcoord() const {
00224   if ((_overall & BAF_overall_updated) == 0) {
00225     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00226   }
00227   return (_overall & BAF_vertex_texcoord)!=0;
00228 }
00229 
00230 
00231 ////////////////////////////////////////////////////////////////////
00232 //     Function: BuilderPrimTempl::has_vertex_pixel_size
00233 //       Access: Public
00234 //  Description: Returns true if each of the primitive's vertices has
00235 //               a different pixel_size value set.
00236 ////////////////////////////////////////////////////////////////////
00237 template <class VTX>
00238 INLINE bool BuilderPrimTempl<VTX>::
00239 has_vertex_pixel_size() const {
00240   if ((_overall & BAF_overall_updated) == 0) {
00241     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00242   }
00243   return (_overall & BAF_vertex_pixel_size)!=0;
00244 }
00245 
00246 
00247 ////////////////////////////////////////////////////////////////////
00248 //     Function: BuilderPrimTempl::has_component_normal
00249 //       Access: Public
00250 //  Description: Returns true if the prim is a composite primitive
00251 //               like a tristrip, and its individual components each
00252 //               have a different normal value set.
00253 ////////////////////////////////////////////////////////////////////
00254 template <class VTX>
00255 INLINE bool BuilderPrimTempl<VTX>::
00256 has_component_normal() const {
00257   if ((_overall & BAF_overall_updated) == 0) {
00258     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00259   }
00260   return (_overall & BAF_component_normal)!=0;
00261 }
00262 
00263 
00264 ////////////////////////////////////////////////////////////////////
00265 //     Function: BuilderPrimTempl::has_component_color
00266 //       Access: Public
00267 //  Description: Returns true if the prim is a composite primitive
00268 //               like a tristrip, and its individual components each
00269 //               have a different color value set.
00270 ////////////////////////////////////////////////////////////////////
00271 template <class VTX>
00272 INLINE bool BuilderPrimTempl<VTX>::
00273 has_component_color() const {
00274   if ((_overall & BAF_overall_updated) == 0) {
00275     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00276   }
00277   return (_overall & BAF_component_color)!=0;
00278 }
00279 
00280 
00281 ////////////////////////////////////////////////////////////////////
00282 //     Function: BuilderPrimTempl::has_component_pixel_size
00283 //       Access: Public
00284 //  Description: Returns true if the prim is a composite primitive
00285 //               like a tristrip, and its individual components each
00286 //               have a different pixel_size value set.
00287 ////////////////////////////////////////////////////////////////////
00288 template <class VTX>
00289 INLINE bool BuilderPrimTempl<VTX>::
00290 has_component_pixel_size() const {
00291   if ((_overall & BAF_overall_updated) == 0) {
00292     ((BuilderPrimTempl<VTX> *)this)->update_overall_attrib();
00293   }
00294   return (_overall & BAF_component_pixel_size)!=0;
00295 }
00296 
00297 
00298 ////////////////////////////////////////////////////////////////////
00299 //     Function: BuilderPrimTempl::has_any_normal
00300 //       Access: Public
00301 //  Description: Returns true if the prim has a normal value set
00302 //               at any level.  That is, it returns true if any of
00303 //               has_overall_normal(), has_vertex_normal(), or
00304 //               has_component_normal() is true.
00305 ////////////////////////////////////////////////////////////////////
00306 template <class VTX>
00307 INLINE bool BuilderPrimTempl<VTX>::
00308 has_any_normal() const {
00309   return has_overall_normal() || has_vertex_normal() || has_component_normal();
00310 }
00311 
00312 
00313 ////////////////////////////////////////////////////////////////////
00314 //     Function: BuilderPrimTempl::has_any_color
00315 //       Access: Public
00316 //  Description: Returns true if the prim has a color value set at any
00317 //               level.  That is, it returns true if any of
00318 //               has_overall_color(), has_vertex_color(), or
00319 //               has_component_color() is true.
00320 ////////////////////////////////////////////////////////////////////
00321 template <class VTX>
00322 INLINE bool BuilderPrimTempl<VTX>::
00323 has_any_color() const {
00324   return has_overall_color() || has_vertex_color() || has_component_color();
00325 }
00326 
00327 
00328 ////////////////////////////////////////////////////////////////////
00329 //     Function: BuilderPrimTempl::has_any_texcoord
00330 //       Access: Public
00331 //  Description: Returns true if the prim has a texcoord value set
00332 //               at any level.  Since texture coordinates can only be
00333 //               set on vertices, this is the same thing as
00334 //               has_vertex_texcoord().
00335 ////////////////////////////////////////////////////////////////////
00336 template <class VTX>
00337 INLINE bool BuilderPrimTempl<VTX>::
00338 has_any_texcoord() const {
00339   return has_vertex_texcoord();
00340 }
00341 
00342 ////////////////////////////////////////////////////////////////////
00343 //     Function: BuilderPrimTempl::has_any_pixel_size
00344 //       Access: Public
00345 //  Description: Returns true if the prim has a pixel_size value set
00346 //               at any level.  That is, it returns true if any of
00347 //               has_overall_pixel_size(), has_vertex_pixel_size(), or
00348 //               has_component_pixel_size() is true.
00349 ////////////////////////////////////////////////////////////////////
00350 template <class VTX>
00351 INLINE bool BuilderPrimTempl<VTX>::
00352 has_any_pixel_size() const {
00353   return has_overall_pixel_size() || has_vertex_pixel_size() ||
00354     has_component_pixel_size();
00355 }
00356 
00357 
00358 ////////////////////////////////////////////////////////////////////
00359 //     Function: BuilderPrimTempl::clear
00360 //       Access: Public
00361 //  Description: Resets the BuilderPrim to its initial, default state.
00362 ////////////////////////////////////////////////////////////////////
00363 template <class VTX>
00364 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00365 clear() {
00366   DAttrib::clear();
00367   _type = BPT_poly;
00368   return clear_vertices();
00369 }
00370 
00371 
00372 ////////////////////////////////////////////////////////////////////
00373 //     Function: BuilderPrimTempl::clear_vertices
00374 //       Access: Public
00375 //  Description: Removes all the vertices that have been added to the
00376 //               BuilderPrim, without otherwise affecting its
00377 //               properties.
00378 ////////////////////////////////////////////////////////////////////
00379 template <class VTX>
00380 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00381 clear_vertices() {
00382   _verts.clear();
00383   _components.clear();
00384   _overall = 0;
00385 
00386   return *this;
00387 }
00388 
00389 
00390 ////////////////////////////////////////////////////////////////////
00391 //     Function: BuilderPrimTempl::set_attrib
00392 //       Access: Public
00393 //  Description: Copies the polygon attributes, color and normal, from
00394 //               the indicated attribute structure (which might be
00395 //               another BuilderPrim).
00396 ////////////////////////////////////////////////////////////////////
00397 template <class VTX>
00398 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00399 set_attrib(const DAttrib &attrib) {
00400   DAttrib::operator = (attrib);
00401   _overall = 0;
00402 
00403   return *this;
00404 }
00405 
00406 ////////////////////////////////////////////////////////////////////
00407 //     Function: BuilderPrimTempl::get_type
00408 //       Access: Public
00409 //  Description: Indicates the type of primitive represented by the
00410 //               BuilderPrim.  See set_type().
00411 ////////////////////////////////////////////////////////////////////
00412 template <class VTX>
00413 INLINE BuilderPrimType BuilderPrimTempl<VTX>::
00414 get_type() const {
00415   return _type;
00416 }
00417 
00418 
00419 ////////////////////////////////////////////////////////////////////
00420 //     Function: BuilderPrimTempl::set_type
00421 //       Access: Public
00422 //  Description: Indicates the type of primitive represented by the
00423 //               BuilderPrim.  Normally, the user should set this to
00424 //               one of BPT_poly, BPT_point, or BPT_line.  However,
00425 //               other types are possible, and are created by the
00426 //               mesher, including triangle strips, quad strips, and
00427 //               triangle fans.
00428 //
00429 //               Setting this value changes the interpretation of the
00430 //               vertices, for instance, a triangle strip is defined
00431 //               by vertices that zigzag back and forth between the
00432 //               triangles.  Note that when defining a composite
00433 //               primitive such as a triangle strip, it is also
00434 //               necessary to call add_component() the correct number
00435 //               of times to match the calls to add_vertex(),
00436 //               according to the primitive type.  See
00437 //               add_component().
00438 ////////////////////////////////////////////////////////////////////
00439 template <class VTX>
00440 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00441 set_type(BuilderPrimType t) {
00442   _type = t;
00443   return *this;
00444 }
00445 
00446 ////////////////////////////////////////////////////////////////////
00447 //     Function: BuilderPrimTempl::get_normal
00448 //       Access: Public
00449 //  Description: Returns the value previously set by set_normal(), or
00450 //               the overall normal value common to all vertices.  It
00451 //               is an error to call this without first testing that
00452 //               one of has_normal() or has_overall_normal() is true.
00453 ////////////////////////////////////////////////////////////////////
00454 template <class VTX>
00455 INLINE TYPENAME BuilderPrimTempl<VTX>::NType BuilderPrimTempl<VTX>::
00456 get_normal() const {
00457   return DAttrib::get_normal();
00458 }
00459 
00460 ////////////////////////////////////////////////////////////////////
00461 //     Function: BuilderPrimTempl::set_normal
00462 //       Access: Public
00463 //  Description: Sets the flat normal associated with the primitive.
00464 //               If each of the primitive's vertices also has a normal
00465 //               set, the vertex normals will override.
00466 ////////////////////////////////////////////////////////////////////
00467 template <class VTX>
00468 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00469 set_normal(const NType &n) {
00470   DAttrib::set_normal(n);
00471   _overall = 0;
00472   return *this;
00473 }
00474 
00475 ////////////////////////////////////////////////////////////////////
00476 //     Function: BuilderPrimTempl::get_color
00477 //       Access: Public
00478 //  Description: Returns the value previously set by set_color(), or
00479 //               the overall color value common to all vertices.  It
00480 //               is an error to call this without first testing that
00481 //               one of has_color() or has_overall_color() is true.
00482 ////////////////////////////////////////////////////////////////////
00483 template <class VTX>
00484 INLINE TYPENAME BuilderPrimTempl<VTX>::CType BuilderPrimTempl<VTX>::
00485 get_color() const {
00486   return DAttrib::get_color();
00487 }
00488 
00489 ////////////////////////////////////////////////////////////////////
00490 //     Function: BuilderPrimTempl::set_color
00491 //       Access: Public
00492 //  Description: Sets the flat color associated with the primitive.
00493 //               If each of the primitive's vertices also has a color
00494 //               set, the vertex colors will override.
00495 ////////////////////////////////////////////////////////////////////
00496 template <class VTX>
00497 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00498 set_color(const CType &c) {
00499   DAttrib::set_color(c);
00500   _overall = 0;
00501   return *this;
00502 }
00503 
00504 ////////////////////////////////////////////////////////////////////
00505 //     Function: BuilderPrimTempl::get_pixel_size
00506 //       Access: Public
00507 //  Description: Returns the value previously set by set_pixel_size(),
00508 //               or the overall pixel_size value common to all
00509 //               vertices.  It is an error to call this without first
00510 //               testing that one of has_pixel_size() or
00511 //               has_overall_pixel_size() is true.
00512 ////////////////////////////////////////////////////////////////////
00513 template <class VTX>
00514 INLINE float BuilderPrimTempl<VTX>::
00515 get_pixel_size() const {
00516   return DAttrib::get_pixel_size();
00517 }
00518 
00519 
00520 ////////////////////////////////////////////////////////////////////
00521 //     Function: BuilderPrimTempl::set_pixel_size
00522 //       Access: Public
00523 //  Description: Sets the line thickness (for a polygon or line) or
00524 //               size (for a point) in pixels.
00525 ////////////////////////////////////////////////////////////////////
00526 template <class VTX>
00527 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00528 set_pixel_size(float s) {
00529   DAttrib::set_pixel_size(s);
00530   _overall = 0;
00531   return *this;
00532 }
00533 
00534 
00535 ////////////////////////////////////////////////////////////////////
00536 //     Function: BuilderPrimTempl::add_vertex
00537 //       Access: Public
00538 //  Description: Adds a new vertex to the BuilderPrim.  The vertex
00539 //               data is copied into the prim's internal structure.
00540 //               Vertices should be added in counterclockwise order,
00541 //               when viewed from the front.
00542 ////////////////////////////////////////////////////////////////////
00543 template <class VTX>
00544 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00545 add_vertex(const Vertex &v) {
00546   _overall = 0;
00547   _verts.push_back(v);
00548   return *this;
00549 }
00550 
00551 
00552 ////////////////////////////////////////////////////////////////////
00553 //     Function: BuilderPrimTempl::get_num_verts
00554 //       Access: Public
00555 //  Description: Returns the number of vertices in the primitive.
00556 ////////////////////////////////////////////////////////////////////
00557 template <class VTX>
00558 INLINE int BuilderPrimTempl<VTX>::
00559 get_num_verts() const {
00560   return _verts.size();
00561 }
00562 
00563 ////////////////////////////////////////////////////////////////////
00564 //     Function: BuilderPrimTempl::get_vertex
00565 //       Access: Public
00566 //  Description: Returns a reference to the nth vertex of the
00567 //               primitive, where 0 <= n < get_num_verts().  This
00568 //               reference may be modified directly, e.g. to set a
00569 //               vertex normal on a vertex after it has been added to
00570 //               the primitive.
00571 ////////////////////////////////////////////////////////////////////
00572 template <class VTX>
00573 INLINE TYPENAME BuilderPrimTempl<VTX>::Vertex &BuilderPrimTempl<VTX>::
00574 get_vertex(int n) {
00575   nassertr(n >= 0 && n < (int)_verts.size(), *(new Vertex));
00576   return _verts[n];
00577 }
00578 
00579 ////////////////////////////////////////////////////////////////////
00580 //     Function: BuilderPrimTempl::get_vertex (const)
00581 //       Access: Public
00582 //  Description: Returns the nth vertex of the primitive, where 0 <= n
00583 //               < get_num_verts().
00584 ////////////////////////////////////////////////////////////////////
00585 template <class VTX>
00586 INLINE const TYPENAME BuilderPrimTempl<VTX>::Vertex &BuilderPrimTempl<VTX>::
00587 get_vertex(int n) const {
00588   nassertr(n >= 0 && n < (int)_verts.size(), *(new Vertex));
00589   return _verts[n];
00590 }
00591 
00592 
00593 ////////////////////////////////////////////////////////////////////
00594 //     Function: BuilderPrimTempl::add_component
00595 //       Access: Public
00596 //  Description: Normally, this function is only used by the mesher
00597 //               code; user code generally should not need to call it.
00598 //
00599 //               In addition to storing a simple polygon, a
00600 //               BuilderPrim can store a composite primitive, like a
00601 //               triangle strip.  In this case, it is useful to store
00602 //               the attributes (like color and normal) associated
00603 //               with each component.
00604 //
00605 //               When get_type() is one of the composite types, it is
00606 //               the responsibility of the caller to call
00607 //               add_component() once for each component, where the
00608 //               number of components is defined by the type and the
00609 //               number of vertices (e.g. for a triangle strip, the
00610 //               number of components is the number of vertices - 2).
00611 ////////////////////////////////////////////////////////////////////
00612 template <class VTX>
00613 INLINE BuilderPrimTempl<VTX> &BuilderPrimTempl<VTX>::
00614 add_component(const DAttrib &attrib) {
00615   _overall = 0;
00616   _components.push_back(attrib);
00617   return *this;
00618 }
00619 
00620 
00621 ////////////////////////////////////////////////////////////////////
00622 //     Function: BuilderPrimTempl::get_num_components
00623 //       Access: Public
00624 //  Description: Returns the number of times add_component() has been
00625 //               called.  This should be, but is not necessarily, the
00626 //               same as the number of components in the composite
00627 //               primitive (e.g. a triangle strip).  See
00628 //               add_component().
00629 ////////////////////////////////////////////////////////////////////
00630 template <class VTX>
00631 INLINE int BuilderPrimTempl<VTX>::
00632 get_num_components() const {
00633   return _components.size();
00634 }
00635 
00636 
00637 ////////////////////////////////////////////////////////////////////
00638 //     Function: BuilderPrimTempl::get_component
00639 //       Access: Public
00640 //  Description: Returns a refernece to the attribute structure
00641 //               associated with the nth component, where 0 <= n <
00642 //               get_num_components().  This reference may be modified
00643 //               directly, e.g. to change the color of a polygon after
00644 //               it has been added to the tristrip.  See
00645 //               add_component() and get_num_components().
00646 ////////////////////////////////////////////////////////////////////
00647 template <class VTX>
00648 INLINE TYPENAME BuilderPrimTempl<VTX>::DAttrib &BuilderPrimTempl<VTX>::
00649 get_component(int n) {
00650   nassertr(n >= 0 && n < (int)_components.size(), *(new DAttrib));
00651   return _components[n];
00652 }
00653 
00654 
00655 ////////////////////////////////////////////////////////////////////
00656 //     Function: BuilderPrimTempl::get_component (const)
00657 //       Access: Public
00658 //  Description: Returns the attributes associated with the nth
00659 //               component, where 0 <= n < get_num_components().  See
00660 //               add_component() and get_num_components().
00661 ////////////////////////////////////////////////////////////////////
00662 template <class VTX>
00663 INLINE const TYPENAME BuilderPrimTempl<VTX>::DAttrib &BuilderPrimTempl<VTX>::
00664 get_component(int n) const {
00665   nassertr(n >= 0 && n < (int)_components.size(), *(new DAttrib));
00666   return _components[n];
00667 }
00668 
00669 
00670 ////////////////////////////////////////////////////////////////////
00671 //     Function: BuilderPrimTempl::Ordering operator
00672 //       Access: Public
00673 //  Description: Returns true if this primitive should come before the
00674 //               other one, in the bin ordering.  This is used to sort
00675 //               polygons into groups which can be tristripped
00676 //               together, similar to the same operation on the
00677 //               BuilderBucket, except that this works at a finer
00678 //               level of detail (i.e. on prims within the same
00679 //               bucket).
00680 ////////////////////////////////////////////////////////////////////
00681 template <class VTX>
00682 INLINE bool BuilderPrimTempl<VTX>::
00683 operator < (const BuilderPrimTempl<VTX> &other) const {
00684   int sv1 = sort_value();
00685   int sv2 = other.sort_value();
00686 
00687   // Normally, we compare only based on the integer sort_value().
00688   // However, other things being equal, if two prims have a different
00689   // pixel_size then they sort differently.
00690 
00691   // We can ignore per-vertex pixel_size, since there's no way to
00692   // render that anyway.
00693 
00694   return sv1 < sv2 ||
00695     (sv1==sv2 && has_pixel_size() && get_pixel_size() < other.get_pixel_size());
00696 }
00697 
00698 
00699 ////////////////////////////////////////////////////////////////////
00700 //     Function: BuilderPrimTempl::sort_value
00701 //       Access: Protected
00702 //  Description: Returns a number for grouping primitives, such that
00703 //               only primitives that share the same number can be
00704 //               tristripped together.
00705 ////////////////////////////////////////////////////////////////////
00706 template <class VTX>
00707 INLINE int BuilderPrimTempl<VTX>::
00708 sort_value() const {
00709   // If a polygon is missing a normal, color, or uv, it shouldn't be
00710   // stripped along with polygons that have normals, colors, or uv's.
00711   // Furthermore, if one polygon has vertex normals and another is
00712   // flat shaded, they shouldn't be stripped together.
00713   return
00714     ((has_vertex_normal() ? 1:0) << 5) |
00715     ((has_vertex_color() ? 1:0) << 4) |
00716     ((has_any_texcoord() ? 1:0) << 3) |
00717     ((has_any_normal() ? 1:0) << 2) |
00718     ((has_any_color() ? 1:0) << 1) |
00719     ((has_pixel_size() ? 1:0) << 0);
00720 }
00721 
00722 
00723 ////////////////////////////////////////////////////////////////////
00724 //        Class: SameCoord
00725 //  Description: An STL function object that identifies vertices as
00726 //               equivalent when they have the same coordinate value,
00727 //               regardless of their attribute values like texture
00728 //               coordinates.  This is used in remove_doubled_verts().
00729 ////////////////////////////////////////////////////////////////////
00730 template <class VTX>
00731 class SameCoord {
00732 public:
00733   bool operator () (const VTX &a, const VTX &b) const {
00734     return a.get_coord() == b.get_coord();
00735   }
00736 };
00737 
00738 ////////////////////////////////////////////////////////////////////
00739 //     Function: BuilderPrimTempl::remove_doubled_verts
00740 //       Access: Public
00741 //  Description: Removes consecutive identical vertices from the prim
00742 //               definition.  These are meaningless and only confuse
00743 //               polygon subdividing and meshing.
00744 //
00745 //               If closed is true, this also removes vertices doubled
00746 //               at the beginning and end (as if the list of vertices
00747 //               were implicitly closed, as it is for a polygon).
00748 ////////////////////////////////////////////////////////////////////
00749 template <class VTX>
00750 void BuilderPrimTempl<VTX>::
00751 remove_doubled_verts(int closed) {
00752   // For some reason, writing this variable declaration this way
00753   // prevents VC++ from generating dozens of spurious "local variable
00754   // 'sc' used without having been initialized" warning messages.
00755   SameCoord<Vertex> sc = SameCoord<Vertex>();
00756 
00757   TYPENAME Verts::iterator new_end = unique(_verts.begin(), _verts.end(), sc);
00758   _verts.erase(new_end, _verts.end());
00759 
00760   if (closed) {
00761     // Then, if this is a polygon (which will be closed anyway),
00762     // remove the vertex from the end if it's a repeat of the
00763     // beginning.
00764     while (!_verts.empty() && sc(_verts.back(), _verts.front())) {
00765       _verts.pop_back();
00766     }
00767   }
00768 }
00769 
00770 ////////////////////////////////////////////////////////////////////
00771 //     Function: BuilderPrimTempl::is_valid
00772 //       Access: Public
00773 //  Description: Returns true if the primitive is well-defined and has
00774 //               the right number of vertices for its primitive type,
00775 //               and all of its vertices are valid.
00776 ////////////////////////////////////////////////////////////////////
00777 template <class VTX>
00778 bool BuilderPrimTempl<VTX>::
00779 is_valid() const {
00780   int num_verts = get_num_verts();
00781   for (int i = 0; i < num_verts; i++) {
00782     if (!get_vertex(i).is_valid()) {
00783       return false;
00784     }
00785   }
00786 
00787   switch (get_type()) {
00788   case BPT_poly:
00789     return num_verts >= 3;
00790 
00791   case BPT_point:
00792     return num_verts >= 1;
00793 
00794   case BPT_line:
00795     return num_verts >= 2;
00796 
00797   case BPT_tri:
00798     return num_verts == 3;
00799 
00800   case BPT_tristrip:
00801   case BPT_trifan:
00802     return num_verts >= 3;
00803 
00804   case BPT_quad:
00805     return num_verts == 4;
00806 
00807   default:
00808     return false;
00809   }
00810 }
00811 
00812 
00813 ////////////////////////////////////////////////////////////////////
00814 //     Function: BuilderPrimTempl::output
00815 //       Access: Public
00816 //  Description: Formats the prim for output in some sensible way.
00817 ////////////////////////////////////////////////////////////////////
00818 template <class VTX>
00819 ostream &BuilderPrimTempl<VTX>::
00820 output(ostream &out) const {
00821   int num_verts = get_num_verts();
00822 
00823   out << get_type() << ", " << num_verts << " vertices:\n";
00824 
00825   for (int i = 0; i < num_verts; i++) {
00826     out << "  " << i << ". " << get_vertex(i) << "\n";
00827   }
00828 
00829   if (has_overall_normal()) {
00830     out << "normal = " << get_normal() << "\n";
00831   }
00832   if (has_overall_color()) {
00833     out << "color = " << get_color() << "\n";
00834   }
00835   if (has_overall_pixel_size()) {
00836     out << "pixel_size = " << get_pixel_size() << "\n";
00837   }
00838 
00839   return out;
00840 }
00841 
00842 ////////////////////////////////////////////////////////////////////
00843 //     Function: BuilderPrimTempl::update_overall_attrib
00844 //       Access: Protected
00845 //  Description: Examines the primitive and all of its vertices and
00846 //               components to determine the amount of commonality of
00847 //               each attribute, and updates the bits in _overall to
00848 //               indicate this.  If an overall attribute is found, the
00849 //               primitive attribute value is set to that common
00850 //               value.
00851 ////////////////////////////////////////////////////////////////////
00852 template <class VTX>
00853 void BuilderPrimTempl<VTX>::
00854 update_overall_attrib() {
00855   int num_verts = get_num_verts();
00856   int num_components = get_num_components();
00857 
00858   NType common_normal;
00859   CType common_color;
00860   float common_pixel_size = 0.0;
00861 
00862   bool has_common_normal = false;
00863   bool has_common_color = false;
00864   bool has_common_pixel_size = false;
00865 
00866   bool has_component_normal = false;
00867   bool has_component_color = false;
00868   bool has_component_pixel_size = false;
00869 
00870   bool has_vertex_normal = false;
00871   bool has_vertex_color = false;
00872   bool has_vertex_texcoord = false;
00873   bool has_vertex_pixel_size = false;
00874 
00875   int i;
00876   if (num_verts > 0) {
00877     has_vertex_texcoord = true;
00878     for (i = 0; i < num_verts && has_vertex_texcoord; i++) {
00879       has_vertex_texcoord = get_vertex(i).has_texcoord();
00880     }
00881 
00882     if (get_vertex(0).has_normal()) {
00883       common_normal = get_vertex(0).get_normal();
00884       has_common_normal = true;
00885       has_vertex_normal = true;
00886 
00887       for (i = 1; i < num_verts; i++) {
00888         Vertex &vertex = get_vertex(i);
00889         has_vertex_normal =
00890           (has_vertex_normal && vertex.has_normal());
00891         if (!vertex.has_normal() ||
00892             !(vertex.get_normal() == common_normal)) {
00893           has_common_normal = false;
00894         }
00895       }
00896     }
00897     if (get_vertex(0).has_color()) {
00898       common_color = get_vertex(0).get_color();
00899       has_common_color = true;
00900       has_vertex_color = true;
00901 
00902       for (i = 1; i < num_verts; i++) {
00903         Vertex &vertex = get_vertex(i);
00904         has_vertex_color =
00905           (has_vertex_color && vertex.has_color());
00906         if (!vertex.has_color() ||
00907             !(vertex.get_color() == common_color)) {
00908           has_common_color = false;
00909         }
00910       }
00911     }
00912     if (get_vertex(0).has_pixel_size()) {
00913       common_pixel_size = get_vertex(0).get_pixel_size();
00914       has_common_pixel_size = true;
00915       has_vertex_pixel_size = true;
00916 
00917       for (i = 1; i < num_verts; i++) {
00918         Vertex &vertex = get_vertex(i);
00919         has_vertex_pixel_size =
00920           (has_vertex_pixel_size && vertex.has_pixel_size());
00921         if (!vertex.has_pixel_size() ||
00922             !(vertex.get_pixel_size() == common_pixel_size)) {
00923           has_common_pixel_size = false;
00924         }
00925       }
00926     }
00927   }
00928 
00929   if (num_components > 0) {
00930     if (!has_vertex_normal && get_component(0).has_normal()) {
00931       common_normal = get_component(0).get_normal();
00932       has_common_normal = true;
00933       has_component_normal = true;
00934 
00935       for (i = 1; i < num_components; i++) {
00936         DAttrib &component = get_component(i);
00937         has_component_normal =
00938           (has_component_normal && component.has_normal());
00939         if (!component.has_normal() ||
00940             !(component.get_normal() == common_normal)) {
00941           has_common_normal = false;
00942         }
00943       }
00944     }
00945     if (!has_vertex_color && get_component(0).has_color()) {
00946       common_color = get_component(0).get_color();
00947       has_common_color = true;
00948       has_component_color = true;
00949 
00950       for (i = 1; i < num_components; i++) {
00951         DAttrib &component = get_component(i);
00952         has_component_color =
00953           (has_component_color && component.has_color());
00954         if (!component.has_color() ||
00955             !(component.get_color() == common_color)) {
00956           has_common_color = false;
00957         }
00958       }
00959     }
00960     if (!has_vertex_pixel_size && get_component(0).has_pixel_size()) {
00961       common_pixel_size = get_component(0).get_pixel_size();
00962       has_common_pixel_size = true;
00963       has_component_pixel_size = true;
00964 
00965       for (i = 1; i < num_components; i++) {
00966         DAttrib &component = get_component(i);
00967         has_component_pixel_size =
00968           (has_component_pixel_size && component.has_pixel_size());
00969         if (!component.has_pixel_size() ||
00970             !(component.get_pixel_size() == common_pixel_size)) {
00971           has_common_pixel_size = false;
00972         }
00973       }
00974     }
00975   }
00976 
00977   _overall = BAF_overall_updated;
00978 
00979   if (has_common_normal) {
00980     // The primitive has one overall normal, or each of the vertices
00981     // has the same normal.
00982     _overall |= BAF_overall_normal;
00983     DAttrib::set_normal(common_normal);
00984 
00985   } else if (has_component_normal) {
00986     // Each component primitive has a different normal.
00987     _overall |= BAF_component_normal;
00988 
00989   } else if (has_vertex_normal) {
00990     // Each vertex has a different normal.
00991     _overall |= BAF_vertex_normal;
00992 
00993   } else if (has_normal()) {
00994     // Well, none of the above, but the prim itself has a normal.
00995     _overall |= BAF_overall_normal;
00996   }
00997 
00998 
00999   if (has_common_color) {
01000     // The primitive has one overall color, or each of the vertices
01001     // has the same color.
01002     _overall |= BAF_overall_color;
01003     DAttrib::set_color(common_color);
01004 
01005   } else if (has_component_color) {
01006     // Each component primitive has a different color.
01007     _overall |= BAF_component_color;
01008 
01009   } else if (has_vertex_color) {
01010     // Each vertex has a different color.
01011     _overall |= BAF_vertex_color;
01012 
01013   } else if (has_color()) {
01014     // None of the above, but the prim itself has a color.
01015     _overall |= BAF_overall_color;
01016   }
01017 
01018   if (has_vertex_texcoord) {
01019     // Each vertex has a texture coordinate.
01020     _overall |= BAF_vertex_texcoord;
01021   }
01022 
01023   if (has_common_pixel_size) {
01024     // The primitive has one overall pixel size, or each of the vertices
01025     // has the same pixel size.
01026     _overall |= BAF_overall_pixel_size;
01027     DAttrib::set_pixel_size(common_pixel_size);
01028 
01029   } else if (has_component_pixel_size) {
01030     // Each component primitive has a different pixel size.
01031     _overall |= BAF_component_pixel_size;
01032 
01033   } else if (has_vertex_pixel_size) {
01034     // Each vertex has a different pixel size.
01035     _overall |= BAF_vertex_pixel_size;
01036 
01037   } else if (has_pixel_size()) {
01038     // The prim itself has a pixel size.
01039     _overall |= BAF_overall_pixel_size;
01040   }
01041 }

Generated on Fri May 2 00:34:46 2003 for Panda by doxygen1.3