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