00001 // Filename: builderPrimTempl.h 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 #ifndef BUILDERPRIMTEMPL_H 00020 #define BUILDERPRIMTEMPL_H 00021 00022 #include <pandabase.h> 00023 00024 #include "builderVertex.h" 00025 #include "builderAttrib.h" 00026 #include "builderTypes.h" 00027 00028 #include "pvector.h" 00029 00030 00031 ///////////////////////////////////////////////////////////////////// 00032 // Class : BuilderPrimTempl 00033 // Description : The main body of BuilderPrim and BuilderPrimI. This 00034 // is a template class on vertex type, which must be 00035 // either BuilderVertex or BuilderVertexI; these classes 00036 // are themselves template classes on vertex type, 00037 // texcoord type, color type, etc. 00038 //////////////////////////////////////////////////////////////////// 00039 template <class VTX> 00040 class BuilderPrimTempl : public VTX::Attrib { 00041 public: 00042 typedef VTX Vertex; 00043 typedef TYPENAME VTX::VType VType; 00044 typedef TYPENAME VTX::NType NType; 00045 typedef TYPENAME VTX::TType TType; 00046 typedef TYPENAME VTX::CType CType; 00047 typedef TYPENAME VTX::Attrib DAttrib; 00048 00049 INLINE BuilderPrimTempl(); 00050 INLINE BuilderPrimTempl(const BuilderPrimTempl ©); 00051 INLINE BuilderPrimTempl &operator = (const BuilderPrimTempl ©); 00052 00053 void remove_doubled_verts(int closed); 00054 bool is_valid() const; 00055 00056 // has_normal() etc. is true if the primitive has a normal, as 00057 // assigned by the user. This is unrelated to the vertices or 00058 // component primitives that may or may not also have normals. 00059 INLINE bool has_normal() const; 00060 INLINE bool has_color() const; 00061 INLINE bool has_pixel_size() const; 00062 00063 // The following has_* functions are based on information derived 00064 // from examining the vertices and the component primitives that 00065 // make up this primitive. 00066 00067 // has_overall_normal() etc. is true if the primitive has a single, 00068 // overall normal shared among all vertices and all component 00069 // primitives, or if its normal was assigned directly via 00070 // set_normal(). For a polygon, this is the polygon normal. For a 00071 // tristrip, this means all triangles share the same normal. 00072 INLINE bool has_overall_normal() const; 00073 INLINE bool has_overall_color() const; 00074 INLINE bool has_overall_pixel_size() const; 00075 00076 // has_vertex_normal() etc. is true if each vertex in the primitive 00077 // has its own normal. It is not true if any vertex does not have a 00078 // normal. 00079 INLINE bool has_vertex_normal() const; 00080 INLINE bool has_vertex_color() const; 00081 INLINE bool has_vertex_texcoord() const; 00082 INLINE bool has_vertex_pixel_size() const; 00083 00084 // has_component_normal() can only be true for aggregate primitive 00085 // types like tristrips. In that case, it is true if each 00086 // individual component (e.g. each triangle of the tristrip) has its 00087 // own normal. 00088 INLINE bool has_component_normal() const; 00089 INLINE bool has_component_color() const; 00090 INLINE bool has_component_pixel_size() const; 00091 00092 // In the above, only one of has_overall_normal(), 00093 // has_vertex_normal(), and has_component_normal() can be true for a 00094 // given primitive. For convenience, the following functions return 00095 // true if any of the above is true: 00096 00097 INLINE bool has_any_normal() const; 00098 INLINE bool has_any_color() const; 00099 INLINE bool has_any_texcoord() const; 00100 INLINE bool has_any_pixel_size() const; 00101 00102 INLINE BuilderPrimTempl &clear(); 00103 INLINE BuilderPrimTempl &clear_vertices(); 00104 00105 INLINE BuilderPrimTempl &set_attrib(const DAttrib &attrib); 00106 00107 INLINE BuilderPrimType get_type() const; 00108 INLINE BuilderPrimTempl &set_type(BuilderPrimType t); 00109 00110 INLINE NType get_normal() const; 00111 INLINE BuilderPrimTempl &set_normal(const NType &n); 00112 00113 INLINE CType get_color() const; 00114 INLINE BuilderPrimTempl &set_color(const CType &c); 00115 00116 INLINE float get_pixel_size() const; 00117 INLINE BuilderPrimTempl &set_pixel_size(float s); 00118 00119 INLINE BuilderPrimTempl &add_vertex(const Vertex &v); 00120 00121 INLINE int get_num_verts() const; 00122 INLINE Vertex &get_vertex(int n); 00123 INLINE const Vertex &get_vertex(int n) const; 00124 00125 INLINE BuilderPrimTempl &add_component(const DAttrib &attrib); 00126 INLINE int get_num_components() const; 00127 INLINE DAttrib &get_component(int n); 00128 INLINE const DAttrib &get_component(int n) const; 00129 00130 INLINE bool operator < (const BuilderPrimTempl &other) const; 00131 00132 ostream &output(ostream &out) const; 00133 00134 protected: 00135 INLINE int sort_value() const; 00136 void update_overall_attrib(); 00137 00138 typedef pvector<Vertex> Verts; 00139 typedef pvector<DAttrib> Components; 00140 00141 Verts _verts; 00142 Components _components; 00143 BuilderPrimType _type; 00144 int _overall; 00145 }; 00146 00147 template <class VTX> 00148 INLINE ostream &operator << (ostream &out, 00149 const BuilderPrimTempl<VTX> &prim) { 00150 return prim.output(out); 00151 } 00152 00153 00154 #include "builderPrimTempl.I" 00155 00156 #endif