Panda3D
 All Classes Functions Variables Enumerations
eggPrimitive.I
00001 // Filename: eggPrimitive.I
00002 // Created by:  drose (16Jan99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: EggPrimitive::Constructor
00018 //       Access: Published
00019 //  Description:
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE EggPrimitive::
00022 EggPrimitive(const string &name): EggNode(name) {
00023   _bface = false;
00024   _connected_shading = S_unknown;
00025 }
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: EggPrimitive::Copy constructor
00029 //       Access: Published
00030 //  Description:
00031 ////////////////////////////////////////////////////////////////////
00032 INLINE EggPrimitive::
00033 EggPrimitive(const EggPrimitive &copy) :
00034   EggNode(copy),
00035   EggAttributes(copy),
00036   _textures(copy._textures),
00037   _material(copy._material),
00038   _bface(copy._bface)
00039 {
00040   copy_vertices(copy);
00041   _connected_shading = S_unknown;
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: EggPrimitive::Copy assignment operator
00046 //       Access: Published
00047 //  Description:
00048 ////////////////////////////////////////////////////////////////////
00049 INLINE EggPrimitive &EggPrimitive::
00050 operator = (const EggPrimitive &copy) {
00051   EggNode::operator = (copy);
00052   EggAttributes::operator = (copy);
00053   copy_vertices(copy);
00054   _textures = copy._textures;
00055   _material = copy._material;
00056   _bface = copy._bface;
00057   _connected_shading = S_unknown;
00058   return *this;
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: EggPrimitive::Destructor
00063 //       Access: Published
00064 //  Description:
00065 ////////////////////////////////////////////////////////////////////
00066 INLINE EggPrimitive::
00067 ~EggPrimitive() {
00068   clear();
00069 }
00070 
00071 ////////////////////////////////////////////////////////////////////
00072 //     Function: EggPrimitive::get_sort_name
00073 //       Access: Published
00074 //  Description: Returns the name of the primitive for the purposes of
00075 //               sorting primitives into different groups, if there is
00076 //               one.
00077 //
00078 //               Presently, this is defined as the primitive name
00079 //               itself, unless it begins with a digit.
00080 ////////////////////////////////////////////////////////////////////
00081 INLINE string EggPrimitive::
00082 get_sort_name() const {
00083   const string &name = get_name();
00084   if (!name.empty() && !isdigit(name[0])) {
00085     return name;
00086   }
00087   return string();
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: EggPrimitive::clear_connected_shading
00092 //       Access: Published
00093 //  Description: Resets the connected_shading member in this
00094 //               primitive, so that get_connected_shading() will
00095 //               recompute a new value.
00096 ////////////////////////////////////////////////////////////////////
00097 INLINE void EggPrimitive::
00098 clear_connected_shading() {
00099   _connected_shading = S_unknown;
00100 }
00101 
00102 ////////////////////////////////////////////////////////////////////
00103 //     Function: EggPrimitive::get_connected_shading
00104 //       Access: Published
00105 //  Description: Determines what sort of shading properties this
00106 //               primitive's connected neighbors have.
00107 //
00108 //               To get the most accurate results, you should first
00109 //               call clear_connected_shading() on all connected
00110 //               primitives (or on all primitives in the egg file).
00111 //               It might also be a good idea to call
00112 //               remove_unused_vertices() to ensure proper
00113 //               connectivity.
00114 //
00115 //               You may find it easiest to call these other methods
00116 //               on the EggData root node (they are defined on
00117 //               EggGroupNode).
00118 ////////////////////////////////////////////////////////////////////
00119 INLINE EggPrimitive::Shading EggPrimitive::
00120 get_connected_shading() const {
00121   if (_connected_shading == S_unknown) {
00122     ((EggPrimitive *)this)->set_connected_shading(S_unknown, this);
00123   }
00124 
00125   return _connected_shading;
00126 }
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: EggPrimitive::set_texture
00130 //       Access: Published
00131 //  Description: Replaces the current list of textures with the
00132 //               indicated texture.
00133 //
00134 //               This method is deprecated and is used in support of
00135 //               single-texturing only.  Please use the multitexture
00136 //               variant add_texture instead.
00137 ////////////////////////////////////////////////////////////////////
00138 INLINE void EggPrimitive::
00139 set_texture(EggTexture *texture) {
00140   clear_texture();
00141   add_texture(texture);
00142 }
00143 
00144 ////////////////////////////////////////////////////////////////////
00145 //     Function: EggPrimitive::has_texture
00146 //       Access: Published
00147 //  Description: Returns true if the primitive has any textures
00148 //               specified, false otherwise.
00149 //
00150 //               This method is deprecated and is used in support of
00151 //               single-texturing only.  New code should be written to
00152 //               use the multitexture variants instead.
00153 ////////////////////////////////////////////////////////////////////
00154 INLINE bool EggPrimitive::
00155 has_texture() const {
00156   return get_num_textures() > 0;
00157 }
00158 
00159 ////////////////////////////////////////////////////////////////////
00160 //     Function: EggPrimitive::has_texture
00161 //       Access: Published
00162 //  Description: Returns true if the primitive has the particular
00163 //               indicated texture, false otherwise.
00164 ////////////////////////////////////////////////////////////////////
00165 INLINE bool EggPrimitive::
00166 has_texture(EggTexture *texture) const {
00167   PT_EggTexture t = texture;
00168   return (::find(_textures.begin(), _textures.end(), t) != _textures.end());
00169 }
00170 
00171 ////////////////////////////////////////////////////////////////////
00172 //     Function: EggPrimitive::get_texture
00173 //       Access: Published
00174 //  Description: Returns the first texture on the primitive, if any,
00175 //               or NULL if there are no textures on the primitive.
00176 //
00177 //               This method is deprecated and is used in support of
00178 //               single-texturing only.  New code should be written to
00179 //               use the multitexture variants instead.
00180 ////////////////////////////////////////////////////////////////////
00181 INLINE EggTexture *EggPrimitive::
00182 get_texture() const {
00183   return has_texture() ? get_texture(0) : (EggTexture *)NULL;
00184 }
00185 
00186 ////////////////////////////////////////////////////////////////////
00187 //     Function: EggPrimitive::add_texture
00188 //       Access: Published
00189 //  Description: Applies the indicated texture to the primitive.
00190 //
00191 //               Note that, in the case of multiple textures being
00192 //               applied to a single primitive, the order in which the
00193 //               textures are applied does not affect the rendering
00194 //               order; use EggTexture::set_sort() to specify that.
00195 ////////////////////////////////////////////////////////////////////
00196 INLINE void EggPrimitive::
00197 add_texture(EggTexture *texture) {
00198   _textures.push_back(texture);
00199 }
00200 
00201 ////////////////////////////////////////////////////////////////////
00202 //     Function: EggPrimitive::clear_texture
00203 //       Access: Published
00204 //  Description: Removes any texturing from the primitive.
00205 ////////////////////////////////////////////////////////////////////
00206 INLINE void EggPrimitive::
00207 clear_texture() {
00208   _textures.clear();
00209 }
00210 
00211 ////////////////////////////////////////////////////////////////////
00212 //     Function: EggPrimitive::get_num_textures
00213 //       Access: Published
00214 //  Description: Returns the number of textures applied to the
00215 //               primitive.
00216 ////////////////////////////////////////////////////////////////////
00217 INLINE int EggPrimitive::
00218 get_num_textures() const {
00219   return _textures.size();
00220 }
00221 
00222 ////////////////////////////////////////////////////////////////////
00223 //     Function: EggPrimitive::get_texture
00224 //       Access: Published
00225 //  Description: Returns the nth texture that has been applied to the
00226 //               primitive.
00227 ////////////////////////////////////////////////////////////////////
00228 INLINE EggTexture *EggPrimitive::
00229 get_texture(int n) const {
00230   nassertr(n >= 0 && n < (int)_textures.size(), NULL);
00231   return _textures[n];
00232 }
00233 
00234 
00235 ////////////////////////////////////////////////////////////////////
00236 //     Function: EggPrimitive::set_material
00237 //       Access: Published
00238 //  Description: Applies the indicated material to the primitive.
00239 ////////////////////////////////////////////////////////////////////
00240 INLINE void EggPrimitive::
00241 set_material(EggMaterial *material) {
00242   _material = material;
00243 }
00244 
00245 ////////////////////////////////////////////////////////////////////
00246 //     Function: EggPrimitive::clear_material
00247 //       Access: Published
00248 //  Description: Removes any material from the primitive.
00249 ////////////////////////////////////////////////////////////////////
00250 INLINE void EggPrimitive::
00251 clear_material() {
00252   _material = (EggMaterial *)NULL;
00253 }
00254 
00255 ////////////////////////////////////////////////////////////////////
00256 //     Function: EggPrimitive::get_material
00257 //       Access: Published
00258 //  Description: Returns a pointer to the applied material, or NULL if
00259 //               there is no material applied.
00260 ////////////////////////////////////////////////////////////////////
00261 INLINE EggMaterial *EggPrimitive::
00262 get_material() const {
00263   return _material;
00264 }
00265 
00266 
00267 ////////////////////////////////////////////////////////////////////
00268 //     Function: EggPrimitive::has_material
00269 //       Access: Published
00270 //  Description: Returns true if the primitive is materiald (and
00271 //               get_material() will return a real pointer), false
00272 //               otherwise (and get_material() will return NULL).
00273 ////////////////////////////////////////////////////////////////////
00274 INLINE bool EggPrimitive::
00275 has_material() const {
00276   return _material != (EggMaterial *)NULL;
00277 }
00278 
00279 
00280 ////////////////////////////////////////////////////////////////////
00281 //     Function: EggPrimitive::set_bface_flag
00282 //       Access: Published
00283 //  Description: Sets the backfacing flag of the polygon.  If this is
00284 //               true, the polygon will be rendered so that both faces
00285 //               are visible; if it is false, only the front face of
00286 //               the polygon will be visible.
00287 ////////////////////////////////////////////////////////////////////
00288 INLINE void EggPrimitive::
00289 set_bface_flag(bool flag) {
00290   _bface = flag;
00291 }
00292 
00293 
00294 ////////////////////////////////////////////////////////////////////
00295 //     Function: EggPrimitive::get_bface_flag
00296 //       Access: Published
00297 //  Description: Retrieves the backfacing flag of the polygon.  See
00298 //               set_bface_flag().
00299 ////////////////////////////////////////////////////////////////////
00300 INLINE bool EggPrimitive::
00301 get_bface_flag() const {
00302   return _bface;
00303 }
00304 
00305 
00306 ////////////////////////////////////////////////////////////////////
00307 //     Function: EggPrimitive::begin
00308 //       Access: Public
00309 //  Description:
00310 ////////////////////////////////////////////////////////////////////
00311 INLINE EggPrimitive::iterator EggPrimitive::
00312 begin() const {
00313   return _vertices.begin();
00314 }
00315 
00316 ////////////////////////////////////////////////////////////////////
00317 //     Function: EggPrimitive::end
00318 //       Access: Public
00319 //  Description:
00320 ////////////////////////////////////////////////////////////////////
00321 INLINE EggPrimitive::iterator EggPrimitive::
00322 end() const {
00323   return _vertices.end();
00324 }
00325 
00326 ////////////////////////////////////////////////////////////////////
00327 //     Function: EggPrimitive::rbegin
00328 //       Access: Public
00329 //  Description:
00330 ////////////////////////////////////////////////////////////////////
00331 INLINE EggPrimitive::reverse_iterator EggPrimitive::
00332 rbegin() const {
00333   return _vertices.rbegin();
00334 }
00335 
00336 ////////////////////////////////////////////////////////////////////
00337 //     Function: EggPrimitive::rend
00338 //       Access: Public
00339 //  Description:
00340 ////////////////////////////////////////////////////////////////////
00341 INLINE EggPrimitive::reverse_iterator EggPrimitive::
00342 rend() const {
00343   return _vertices.rend();
00344 }
00345 
00346 ////////////////////////////////////////////////////////////////////
00347 //     Function: EggPrimitive::empty
00348 //       Access: Public
00349 //  Description:
00350 ////////////////////////////////////////////////////////////////////
00351 INLINE bool EggPrimitive::
00352 empty() const {
00353   return _vertices.empty();
00354 }
00355 
00356 ////////////////////////////////////////////////////////////////////
00357 //     Function: EggPrimitive::size
00358 //       Access: Public
00359 //  Description:
00360 ////////////////////////////////////////////////////////////////////
00361 INLINE EggPrimitive::size_type EggPrimitive::
00362 size() const {
00363   return _vertices.size();
00364 }
00365 
00366 ////////////////////////////////////////////////////////////////////
00367 //     Function: EggPrimitive::Indexing operator
00368 //       Access: Public
00369 //  Description: This is read-only: you can't assign directly to an
00370 //               indexed vertex.  See set_vertex() instead.
00371 ////////////////////////////////////////////////////////////////////
00372 INLINE EggVertex *EggPrimitive::
00373 operator [] (int index) const {
00374   nassertr(index >= 0 && index < (int)size(), NULL);
00375   return *(begin() + index);
00376 }
00377 
00378 ////////////////////////////////////////////////////////////////////
00379 //     Function: EggPrimitive::insert
00380 //       Access: Public
00381 //  Description:
00382 ////////////////////////////////////////////////////////////////////
00383 INLINE EggPrimitive::iterator EggPrimitive::
00384 insert(iterator position, EggVertex *x) {
00385   prepare_add_vertex(x, position - _vertices.begin(), _vertices.size() + 1);
00386   iterator i = _vertices.insert((Vertices::iterator &)position, x);
00387   x->test_pref_integrity();
00388   test_vref_integrity();
00389   return i;
00390 }
00391 
00392 ////////////////////////////////////////////////////////////////////
00393 //     Function: EggPrimitive::erase
00394 //       Access: Public
00395 //  Description:
00396 ////////////////////////////////////////////////////////////////////
00397 INLINE EggPrimitive::iterator EggPrimitive::
00398 erase(iterator position) {
00399   prepare_remove_vertex(*position, position - _vertices.begin(), _vertices.size());
00400   iterator i = _vertices.erase((Vertices::iterator &)position);
00401   test_vref_integrity();
00402   return i;
00403 }
00404 
00405 ////////////////////////////////////////////////////////////////////
00406 //     Function: EggPrimitive::replace
00407 //       Access: Public
00408 //  Description: Replaces the vertex at the indicated position with
00409 //               the indicated vertex.  It is an error to call this
00410 //               with an invalid position iterator (e.g. end()).
00411 ////////////////////////////////////////////////////////////////////
00412 INLINE void EggPrimitive::
00413 replace(iterator position, EggVertex *x) {
00414   nassertv(position != end());
00415 
00416   // We pass -1 for i and n so that EggCompositePrimitive won't try to
00417   // adjust its _components list.
00418   prepare_remove_vertex(*position, -1, -1);
00419   prepare_add_vertex(x, -1, -1);
00420   *(Vertices::iterator &)position = x;
00421 
00422   x->test_pref_integrity();
00423   test_vref_integrity();
00424 }
00425 
00426 ////////////////////////////////////////////////////////////////////
00427 //     Function: EggPrimitive::clear
00428 //       Access: Published
00429 //  Description: Removes all of the vertices from the primitive.
00430 ////////////////////////////////////////////////////////////////////
00431 INLINE void EggPrimitive::
00432 clear() {
00433   erase(begin(), end());
00434 }
00435 
00436 ////////////////////////////////////////////////////////////////////
00437 //     Function: EggPrimitive::get_num_vertices
00438 //       Access: Published
00439 //  Description: 
00440 ////////////////////////////////////////////////////////////////////
00441 INLINE int EggPrimitive::
00442 get_num_vertices() const {
00443   return size();
00444 }
00445 
00446 ////////////////////////////////////////////////////////////////////
00447 //     Function: EggPrimitive::set_vertex
00448 //       Access: Published
00449 //  Description: Replaces a particular vertex based on its index
00450 //               number in the list of vertices.  This is just a
00451 //               convenience function for people who don't want to
00452 //               mess with the iterators.
00453 ////////////////////////////////////////////////////////////////////
00454 INLINE void EggPrimitive::
00455 set_vertex(int index, EggVertex *vertex) {
00456   nassertv(index >= 0 && index < (int)size());
00457   replace(begin() + index, vertex);
00458 }
00459 
00460 ////////////////////////////////////////////////////////////////////
00461 //     Function: EggPrimitive::get_vertex
00462 //       Access: Published
00463 //  Description: Returns a particular index based on its index number.
00464 ////////////////////////////////////////////////////////////////////
00465 INLINE EggVertex *EggPrimitive::
00466 get_vertex(int index) const {
00467   nassertr(index >= 0 && index < (int)size(), NULL);
00468   return *(begin() + index);
00469 }
00470 
00471 
00472 ////////////////////////////////////////////////////////////////////
00473 //     Function: EggPrimitive::get_pool
00474 //       Access: Published
00475 //  Description: Returns the vertex pool associated with the vertices
00476 //               of the primitive, or NULL if the primitive has no
00477 //               vertices.
00478 ////////////////////////////////////////////////////////////////////
00479 INLINE EggVertexPool *EggPrimitive::
00480 get_pool() const {
00481   return empty() ? (EggVertexPool *)NULL : _vertices.front()->get_pool();
00482 }
 All Classes Functions Variables Enumerations