Panda3D
Loading...
Searching...
No Matches
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"
29
30#include "pointerTo.h"
31#include "pvector.h"
32
33#include <algorithm>
34
35class 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 */
47class 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
55private:
56 typedef vector_PT_EggVertex Vertices;
57
58 // Here begins the actual public interface to EggPrimitive.
59
60PUBLISHED:
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
77 virtual EggRenderMode *determine_alpha_mode();
78 virtual EggRenderMode *determine_depth_write_mode();
79 virtual EggRenderMode *determine_depth_test_mode();
80 virtual EggRenderMode *determine_visibility_mode();
81 virtual EggRenderMode *determine_depth_offset();
82 virtual EggRenderMode *determine_draw_order();
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);
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);
133 virtual bool has_primitives() const;
134 virtual bool joint_has_primitives() const;
135 virtual bool has_normals() const;
136
137 // The EggPrimitive itself appears to be an STL container of pointers to
138 // EggVertex objects. The set of vertices is read-only, however, except
139 // through the limited add_vertex/remove_vertex or insert/erase interface.
140 // The following implements this.
141public:
142#if defined(WIN32_VC) || defined(WIN64_VC)
143 typedef PT_EggVertex *pointer;
144 typedef PT_EggVertex *const_pointer;
145#else
146 typedef Vertices::const_pointer pointer;
147 typedef Vertices::const_pointer const_pointer;
148#endif
149 typedef Vertices::const_reference reference;
150 typedef Vertices::const_reference const_reference;
151 typedef Vertices::const_iterator iterator;
152 typedef Vertices::const_iterator const_iterator;
153 typedef Vertices::const_reverse_iterator reverse_iterator;
154 typedef Vertices::const_reverse_iterator const_reverse_iterator;
155 typedef Vertices::size_type size_type;
156 typedef Vertices::difference_type difference_type;
157
158 INLINE iterator begin() const;
159 INLINE iterator end() const;
160 INLINE reverse_iterator rbegin() const;
161 INLINE reverse_iterator rend() const;
162 INLINE bool empty() const;
163 INLINE size_type size() const;
164
165 INLINE EggVertex *operator [] (int index) const;
166
167 INLINE iterator insert(iterator position, EggVertex *x);
168 INLINE iterator erase(iterator position);
169 iterator erase(iterator first, iterator last);
170 INLINE void replace(iterator position, EggVertex *vertex);
171 iterator find(EggVertex *vertex);
172
173PUBLISHED:
174 INLINE void clear();
175
178 void remove_vertex(size_t index);
179 void copy_vertices(const EggPrimitive &other);
180
181 // These are shorthands if you don't want to use the iterators.
182 INLINE size_t get_num_vertices() const;
183 INLINE EggVertex *get_vertex(size_t index) const;
184 INLINE void set_vertex(size_t index, EggVertex *vertex);
185 INLINE void insert_vertex(size_t index, EggVertex *vertex);
186 MAKE_SEQ(get_vertices, get_num_vertices, get_vertex);
187
188 INLINE EggVertexPool *get_pool() const;
189
190 MAKE_SEQ_PROPERTY(vertices, get_num_vertices, get_vertex, set_vertex, remove_vertex, insert_vertex);
191 MAKE_PROPERTY(pool, get_pool);
192
193 virtual void write(std::ostream &out, int indent_level) const=0;
194
195#ifdef _DEBUG
196 void test_vref_integrity() const;
197#else
198 void test_vref_integrity() const { }
199#endif // _DEBUG
200
201protected:
202 Vertices _vertices;
203
204 // Don't try to use these private functions. User code should add and
205 // remove vertices via add_vertex() / remove_vertex(), or via the STL-like
206 // push_back() / pop_back() or insert() / erase(), above.
207 virtual void prepare_add_vertex(EggVertex *vertex, int i, int n);
208 virtual void prepare_remove_vertex(EggVertex *vertex, int i, int n);
209
210protected:
211 void write_body(std::ostream &out, int indent_level) const;
212
213 virtual bool egg_start_parse_body();
214 virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
215 CoordinateSystem to_cs);
216 virtual void r_flatten_transforms();
217 virtual void r_apply_texmats(EggTextureCollection &textures);
218
219 void do_apply_flat_attribute(int vertex_index, EggAttributes *attrib);
220
221private:
222 void set_connected_shading(Shading shading, const EggAttributes *neighbor);
223
224 class ConnectedShadingNode {
225 public:
226 Shading _shading;
227 const EggAttributes *_neighbor;
228 };
229 typedef pvector<ConnectedShadingNode> ConnectedShadingNodes;
230
231 void r_set_connected_shading(int depth_count,
232 Shading shading, const EggAttributes *neighbor,
233 ConnectedShadingNodes &connected_nodes);
234
235private:
236 typedef vector_PT_EggTexture Textures;
237 Textures _textures;
238 PT_EggMaterial _material;
239 bool _bface;
240 Shading _connected_shading;
241
242public:
243
244 static TypeHandle get_class_type() {
245 return _type_handle;
246 }
247 static void init_type() {
248 EggNode::init_type();
249 EggAttributes::init_type();
250 EggRenderMode::get_class_type();
251 register_type(_type_handle, "EggPrimitive",
252 EggNode::get_class_type(),
253 EggAttributes::get_class_type(),
254 EggRenderMode::get_class_type());
255 }
256 virtual TypeHandle get_type() const {
257 return get_class_type();
258 }
259 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
260
261private:
262 static TypeHandle _type_handle;
263
264 friend class EggTextureCollection;
265};
266
267#include "eggPrimitive.I"
268
269#endif
The set of attributes that may be applied to vertices as well as polygons, such as surface normal and...
void write(std::ostream &out, int indent_level) const
Writes the attributes to the indicated output stream in Egg format.
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
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
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
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
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
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
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
set_bface_flag
Sets the backfacing flag of the polygon.
void clear_texture()
Removes any texturing from the primitive.
virtual void unify_attributes(Shading shading)
If the shading property is S_per_vertex, ensures that all vertices have a normal and a color,...
get_material
Returns a pointer to the applied material, or NULL if there is no material applied.
get_pool
Returns the vertex pool associated with the vertices of the primitive, or NULL if the primitive has n...
get_shading
Returns the shading properties apparent on this particular primitive.
void set_texture(EggTexture *texture)
Replaces the current list of textures with the indicated texture.
void copy_vertices(const EggPrimitive &other)
Replaces the current primitive's list of vertices with a copy of the list of vertices on the other pr...
set_material
Applies the indicated material to the primitive.
clear_material
Removes any material from the primitive.
bool has_texture() const
Returns true if the primitive has any textures specified, false otherwise.
bool has_vertex_normal() const
Returns true if any vertex on the primitive has a specific normal set, false otherwise.
get_vertex
Returns a particular index based on its index number.
virtual void apply_first_attribute()
Sets the first vertex of the triangle (or each component) to the primitive normal and/or color,...
get_connected_shading
Determines what sort of shading properties this primitive's connected neighbors have.
void copy_attributes(const EggAttributes &other)
Copies the rendering attributes from the indicated primitive.
virtual bool joint_has_primitives() const
Returns true if there are any primitives (e.g.
set_vertex
Replaces a particular vertex based on its index number in the list of vertices.
get_bface_flag
Retrieves the backfacing flag of the polygon.
EggVertex * add_vertex(EggVertex *vertex)
Adds the indicated vertex to the end of the primitive's list of vertices, and returns it.
void replace(iterator position, EggVertex *vertex)
Replaces the vertex at the indicated position with the indicated vertex.
virtual bool has_normals() const
Returns true if any of the primitives (e.g.
get_num_textures
Returns the number of textures applied to the primitive.
virtual void post_apply_flat_attribute()
Intended as a followup to apply_last_attribute(), this also sets an attribute on the first vertices o...
virtual void reverse_vertex_ordering()
Reverses the ordering of the vertices in this primitive, if appropriate, in order to change the direc...
get_sort_name
Returns the name of the primitive for the purposes of sorting primitives into different groups,...
remove_vertex
Removes the indicated vertex from the primitive and returns it.
bool has_vertex_color() const
Returns true if any vertex on the primitive has a specific color set, false otherwise.
void remove_doubled_verts(bool closed)
Certain kinds of primitives, particularly polygons, don't like to have the same vertex repeated conse...
void clear()
Removes all of the vertices from the primitive.
void clear_connected_shading()
Resets the connected_shading member in this primitive, so that get_connected_shading() will recompute...
void remove_nonunique_verts()
Removes any multiple appearances of the same vertex from the primitive.
insert_vertex
Inserts a vertex at the given position.
iterator find(EggVertex *vertex)
Returns the iterator pointing to the indicated vertex, or end() if the vertex is not part of the prim...
get_texture
Returns the first texture on the primitive, if any, or NULL if there are no textures on the primitive...
virtual bool has_primitives() const
Returns true if there are any primitives (e.g.
virtual void apply_last_attribute()
Sets the last vertex of the triangle (or each component) to the primitive normal and/or color,...
void add_texture(EggTexture *texture)
Applies the indicated texture to the primitive.
virtual bool cleanup()
Cleans up modeling errors in whatever context this makes sense.
has_material
Returns true if the primitive is materiald (and get_material() will return a real pointer),...
This is a collection of textures by TRef name.
Defines a texture map that may be applied to geometry.
Definition eggTexture.h:30
A collection of vertices.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.