Panda3D
eggPrimitive.h
1 // Filename: eggPrimitive.h
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 #ifndef EGGPRIMITIVE_H
16 #define EGGPRIMITIVE_H
17 
18 #include "pandabase.h"
19 
20 #include "eggNode.h"
21 #include "eggAttributes.h"
22 #include "eggVertex.h"
23 #include "eggTexture.h"
24 #include "eggMaterial.h"
25 #include "eggRenderMode.h"
26 #include "pt_EggTexture.h"
27 #include "pt_EggMaterial.h"
28 #include "vector_PT_EggVertex.h"
29 #include "vector_PT_EggTexture.h"
30 
31 #include "pointerTo.h"
32 #include "pvector.h"
33 
34 #include <algorithm>
35 
36 class EggVertexPool;
37 
38 ////////////////////////////////////////////////////////////////////
39 // Class : EggPrimitive
40 // Description : A base class for any of a number of kinds of geometry
41 // primitives: polygons, point lights, nurbs patches,
42 // parametrics curves, etc. Things with a set of
43 // vertices and some rendering properties like color.
44 //
45 // An EggPrimitive is an STL-style container of pointers
46 // to EggVertex's. In fact, it IS a vector, and can be
47 // manipulated in all the ways that vectors can.
48 // However, it is necessary that all vertices belong to
49 // the same vertex pool.
50 ////////////////////////////////////////////////////////////////////
51 class EXPCL_PANDAEGG EggPrimitive : public EggNode, public EggAttributes,
52  public EggRenderMode
53 {
54 
55  // This is a bit of private interface stuff that must be here as a
56  // forward reference. This allows us to define the EggPrimitive as
57  // an STL container.
58 
59 private:
60  typedef vector_PT_EggVertex Vertices;
61 
62  // Here begins the actual public interface to EggPrimitive.
63 
64 PUBLISHED:
65  enum Shading {
66  // The order here is important. The later choices are more
67  // specific than the earlier ones.
68  S_unknown,
69  S_overall,
70  S_per_face,
71  S_per_vertex
72  };
73 
74  INLINE EggPrimitive(const string &name = "");
75  INLINE EggPrimitive(const EggPrimitive &copy);
76  INLINE EggPrimitive &operator = (const EggPrimitive &copy);
77  INLINE ~EggPrimitive();
78 
85  virtual EggRenderMode *determine_bin();
86 
87  INLINE string get_sort_name() const;
88 
89  virtual Shading get_shading() const;
90  INLINE void clear_connected_shading();
91  INLINE Shading get_connected_shading() const;
92 
93  INLINE void set_texture(EggTexture *texture);
94  INLINE bool has_texture() const;
95  INLINE bool has_texture(EggTexture *texture) const;
96  INLINE EggTexture *get_texture() const;
97 
98  INLINE void add_texture(EggTexture *texture);
99  INLINE void clear_texture();
100  INLINE int get_num_textures() const;
101  INLINE EggTexture *get_texture(int n) const;
102  MAKE_SEQ(get_textures, get_num_textures, get_texture);
103 
104  INLINE void set_material(EggMaterial *material);
105  INLINE void clear_material();
106  INLINE EggMaterial *get_material() const;
107  INLINE bool has_material() const;
108 
109  INLINE void set_bface_flag(bool flag);
110  INLINE bool get_bface_flag() const;
111 
112  void copy_attributes(const EggAttributes &other);
113  void copy_attributes(const EggPrimitive &other);
114 
115  bool has_vertex_normal() const;
116  bool has_vertex_color() const;
117 
118  virtual void unify_attributes(Shading shading);
119  virtual void apply_last_attribute();
120  virtual void apply_first_attribute();
121  virtual void post_apply_flat_attribute();
122  virtual void reverse_vertex_ordering();
123  virtual bool cleanup();
124 
125  void remove_doubled_verts(bool closed);
126  void remove_nonunique_verts();
127  virtual bool has_primitives() const;
128  virtual bool joint_has_primitives() const;
129  virtual bool has_normals() const;
130 
131 
132  // The EggPrimitive itself appears to be an STL container of
133  // pointers to EggVertex objects. The set of vertices is read-only,
134  // however, except through the limited add_vertex/remove_vertex or
135  // insert/erase interface. The following implements this.
136 public:
137 #if defined(WIN32_VC) || defined(WIN64_VC)
138  typedef PT_EggVertex *pointer;
139  typedef PT_EggVertex *const_pointer;
140 #else
141  typedef Vertices::const_pointer pointer;
142  typedef Vertices::const_pointer const_pointer;
143 #endif
144  typedef Vertices::const_reference reference;
145  typedef Vertices::const_reference const_reference;
146  typedef Vertices::const_iterator iterator;
147  typedef Vertices::const_iterator const_iterator;
148  typedef Vertices::const_reverse_iterator reverse_iterator;
149  typedef Vertices::const_reverse_iterator const_reverse_iterator;
150  typedef Vertices::size_type size_type;
151  typedef Vertices::difference_type difference_type;
152 
153  INLINE iterator begin() const;
154  INLINE iterator end() const;
155  INLINE reverse_iterator rbegin() const;
156  INLINE reverse_iterator rend() const;
157  INLINE bool empty() const;
158  INLINE size_type size() const;
159 
160  INLINE EggVertex *operator [] (int index) const;
161 
162  INLINE iterator insert(iterator position, EggVertex *x);
163  INLINE iterator erase(iterator position);
164  iterator erase(iterator first, iterator last);
165  INLINE void replace(iterator position, EggVertex *vertex);
166  iterator find(EggVertex *vertex);
167 
168 PUBLISHED:
169  INLINE void clear();
170 
171  EggVertex *add_vertex(EggVertex *vertex);
172  EggVertex *remove_vertex(EggVertex *vertex);
173  void copy_vertices(const EggPrimitive &other);
174 
175  // These are shorthands if you don't want to use the iterators.
176  INLINE int get_num_vertices() const;
177  INLINE void set_vertex(int index, EggVertex *vertex);
178  INLINE EggVertex *get_vertex(int index) const;
179  MAKE_SEQ(get_vertices, get_num_vertices, get_vertex);
180 
181  INLINE EggVertexPool *get_pool() const;
182 
183  virtual void write(ostream &out, int indent_level) const=0;
184 
185 #ifdef _DEBUG
186  void test_vref_integrity() const;
187 #else
188  void test_vref_integrity() const { }
189 #endif // _DEBUG
190 
191 protected:
192  Vertices _vertices;
193 
194  // Don't try to use these private functions. User code should add
195  // and remove vertices via add_vertex()/remove_vertex(), or via the
196  // STL-like push_back()/pop_back() or insert()/erase(), above.
197  virtual void prepare_add_vertex(EggVertex *vertex, int i, int n);
198  virtual void prepare_remove_vertex(EggVertex *vertex, int i, int n);
199 
200 protected:
201  void write_body(ostream &out, int indent_level) const;
202 
203  virtual bool egg_start_parse_body();
204  virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
205  CoordinateSystem to_cs);
206  virtual void r_flatten_transforms();
207  virtual void r_apply_texmats(EggTextureCollection &textures);
208 
209  void do_apply_flat_attribute(int vertex_index, EggAttributes *attrib);
210 
211 private:
212  void set_connected_shading(Shading shading, const EggAttributes *neighbor);
213 
214  class ConnectedShadingNode {
215  public:
216  Shading _shading;
217  const EggAttributes *_neighbor;
218  };
220 
221  void r_set_connected_shading(int depth_count,
222  Shading shading, const EggAttributes *neighbor,
223  ConnectedShadingNodes &connected_nodes);
224 
225 private:
226  typedef vector_PT_EggTexture Textures;
227  Textures _textures;
228  PT_EggMaterial _material;
229  bool _bface;
230  Shading _connected_shading;
231 
232 public:
233 
234  static TypeHandle get_class_type() {
235  return _type_handle;
236  }
237  static void init_type() {
238  EggNode::init_type();
239  EggAttributes::init_type();
240  EggRenderMode::get_class_type();
241  register_type(_type_handle, "EggPrimitive",
242  EggNode::get_class_type(),
243  EggAttributes::get_class_type(),
244  EggRenderMode::get_class_type());
245  }
246  virtual TypeHandle get_type() const {
247  return get_class_type();
248  }
249  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
250 
251 private:
252  static TypeHandle _type_handle;
253 
254  friend class EggTextureCollection;
255 };
256 
257 #include "eggPrimitive.I"
258 
259 #endif
A base class for any of a number of kinds of geometry primitives: polygons, point lights...
Definition: eggPrimitive.h:51
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
Defines a texture map that may be applied to geometry.
Definition: eggTexture.h:33
virtual EggRenderMode * determine_draw_order()
Walks back up the hierarchy, looking for an EggGroup or EggPrimitive or some such object at this leve...
Definition: eggNode.cxx:190
virtual EggRenderMode * determine_depth_test_mode()
Walks back up the hierarchy, looking for an EggGroup or EggPrimitive or some such object at this leve...
Definition: eggNode.cxx:136
This is a collection of textures by TRef name.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
This class stores miscellaneous rendering properties that is associated with geometry, and which may be set on the geometry primitive level, on the group above it, or indirectly via a texture.
Definition: eggRenderMode.h:36
The set of attributes that may be applied to vertices as well as polygons, such as surface normal and...
Definition: eggAttributes.h:37
virtual EggRenderMode * determine_visibility_mode()
Walks back up the hierarchy, looking for an EggGroup or EggPrimitive or some such object at this leve...
Definition: eggNode.cxx:154
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal...
Definition: eggVertex.h:41
virtual EggRenderMode * determine_depth_write_mode()
Walks back up the hierarchy, looking for an EggGroup or EggPrimitive or some such object at this leve...
Definition: eggNode.cxx:118
virtual EggRenderMode * determine_alpha_mode()
Walks back up the hierarchy, looking for an EggGroup or EggPrimitive or some such object at this leve...
Definition: eggNode.cxx:100
virtual EggRenderMode * determine_bin()
Walks back up the hierarchy, looking for an EggGroup or EggPrimitive or some such object at this leve...
Definition: eggNode.cxx:208
void write(ostream &out, int indent_level) const
Writes the attributes to the indicated output stream in Egg format.
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:38
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual EggRenderMode * determine_depth_offset()
Walks back up the hierarchy, looking for an EggGroup or EggPrimitive or some such object at this leve...
Definition: eggNode.cxx:172
A collection of vertices.
Definition: eggVertexPool.h:46