Panda3D
geomPrimitive.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 geomPrimitive.h
10  * @author drose
11  * @date 2005-03-06
12  */
13 
14 #ifndef GEOMPRIMITIVE_H
15 #define GEOMPRIMITIVE_H
16 
17 #include "pandabase.h"
18 #include "geomEnums.h"
19 #include "geomVertexArrayData.h"
20 #include "geomVertexData.h"
21 #include "copyOnWriteObject.h"
22 #include "luse.h"
23 #include "updateSeq.h"
24 #include "pointerTo.h"
25 #include "pta_int.h"
26 #include "pStatCollector.h"
27 #include "cycleData.h"
28 #include "cycleDataReader.h"
29 #include "cycleDataWriter.h"
30 #include "cycleDataStageReader.h"
31 #include "cycleDataStageWriter.h"
32 #include "pipelineCycler.h"
33 #include "deletedChain.h"
34 
36 class IndexBufferContext;
38 class FactoryParams;
40 
41 /**
42  * This is an abstract base class for a family of classes that represent the
43  * fundamental geometry primitives that may be stored in a Geom.
44  *
45  * They all have in common the fact that they are defined by tables of vertex
46  * data stored in a GeomVertexData object. Each GeomPrimitive object contains
47  * an ordered list of integers, which index into the vertex array defined by
48  * the GeomVertexData and define the particular vertices of the GeomVertexData
49  * that are used for this primitive.
50  *
51  * The meaning of a given arrangement of vertices is defined by each
52  * individual primitive type; for instance, a GeomTriangle renders a triangle
53  * from each three consecutive vertices, while a GeomTriangleStrip renders a
54  * strip of (n - 2) connected triangles from each sequence of n vertices.
55  */
56 class EXPCL_PANDA_GOBJ GeomPrimitive : public CopyOnWriteObject, public GeomEnums {
57 protected:
58  GeomPrimitive();
59  virtual PT(CopyOnWriteObject) make_cow_copy();
60 
61 PUBLISHED:
62  explicit GeomPrimitive(UsageHint usage_hint);
63  GeomPrimitive(const GeomPrimitive &copy);
64  void operator = (const GeomPrimitive &copy);
65  virtual ~GeomPrimitive();
66  ALLOC_DELETED_CHAIN(GeomPrimitive);
67 
68  virtual PT(GeomPrimitive) make_copy() const=0;
69 
70  virtual PrimitiveType get_primitive_type() const=0;
71  virtual int get_geom_rendering() const;
72  MAKE_PROPERTY(primitive_type, get_primitive_type);
73  MAKE_PROPERTY(geom_rendering, get_geom_rendering);
74 
75  INLINE ShadeModel get_shade_model() const;
76  INLINE void set_shade_model(ShadeModel shade_model);
77  MAKE_PROPERTY(shade_model, get_shade_model);
78 
79  INLINE UsageHint get_usage_hint() const;
80  void set_usage_hint(UsageHint usage_hint);
81  MAKE_PROPERTY(usage_hint, get_usage_hint);
82 
83  INLINE NumericType get_index_type() const;
84  void set_index_type(NumericType index_type);
85  MAKE_PROPERTY(index_type, get_index_type);
86 
87  // The following published methods are provided for safe, high-level
88  // iteration through the vertices and sub-primitives within the
89  // GeomPrimitive class. These work correctly regardless of the primitive
90  // type and without depending on knowledge about the way primitives' lengths
91  // are encoded. You can also safely build up a composite primitive using
92  // these methods.
93 
94  INLINE bool is_composite() const;
95  INLINE bool is_indexed() const;
96  INLINE int get_first_vertex() const;
97  INLINE int get_num_vertices() const;
98  INLINE int get_vertex(int i) const;
99  MAKE_SEQ(get_vertex_list, get_num_vertices, get_vertex);
100  void add_vertex(int vertex);
101  INLINE void add_vertices(int v1, int v2);
102  INLINE void add_vertices(int v1, int v2, int v3);
103  INLINE void add_vertices(int v1, int v2, int v3, int v4);
104  void add_consecutive_vertices(int start, int num_vertices);
105  void add_next_vertices(int num_vertices);
106  void reserve_num_vertices(int num_vertices);
107  bool close_primitive();
108  void clear_vertices();
109  void offset_vertices(int offset);
110  void offset_vertices(int offset, int begin_row, int end_row);
111  void make_nonindexed(GeomVertexData *dest, const GeomVertexData *source);
112  void pack_vertices(GeomVertexData *dest, const GeomVertexData *source);
113  void make_indexed();
114 
115  INLINE int get_num_primitives() const;
116  int get_primitive_start(int n) const;
117  int get_primitive_end(int n) const;
118  int get_primitive_num_vertices(int n) const;
119  int get_num_used_vertices() const;
120 
121  INLINE int get_num_faces() const;
122  INLINE int get_primitive_num_faces(int n) const;
123 
124  INLINE int get_min_vertex() const;
125  int get_primitive_min_vertex(int n) const;
126  INLINE int get_max_vertex() const;
127  int get_primitive_max_vertex(int n) const;
128 
129  CPT(GeomPrimitive) decompose() const;
130  CPT(GeomPrimitive) rotate() const;
131  CPT(GeomPrimitive) doubleside() const;
132  CPT(GeomPrimitive) reverse() const;
133  CPT(GeomPrimitive) match_shade_model(ShadeModel shade_model) const;
134  CPT(GeomPrimitive) make_points() const;
135  CPT(GeomPrimitive) make_lines() const;
136  CPT(GeomPrimitive) make_patches() const;
137  virtual CPT(GeomPrimitive) make_adjacency() const;
138 
139  int get_num_bytes() const;
140  INLINE int get_data_size_bytes() const;
141  INLINE UpdateSeq get_modified() const;
142  MAKE_PROPERTY(num_bytes, get_num_bytes);
143  MAKE_PROPERTY(data_size_bytes, get_data_size_bytes);
144  MAKE_PROPERTY(modified, get_modified);
145 
146  bool request_resident(Thread *current_thread = Thread::get_current_thread()) const;
147 
148  INLINE bool check_valid(const GeomVertexData *vertex_data) const;
149  INLINE bool check_valid(const GeomVertexDataPipelineReader *data_reader) const;
150 
151  virtual void output(std::ostream &out) const;
152  virtual void write(std::ostream &out, int indent_level) const;
153 
154 PUBLISHED:
155 /*
156  * These public methods are not intended for high-level usage. They are
157  * public so that low-level code that absolutely needs fast access to the
158  * primitive data can get to it, but using them requires knowledge about how
159  * the component primitives are encoded within the GeomPrimitive class, and
160  * it's easy to screw something up. Also, if too many code samples depend on
161  * this internal knowledge, it may make it difficult to extend this class
162  * later. It is recommended that application-level code use the above
163  * interfaces instead.
164  */
165 
166  INLINE CPT(GeomVertexArrayData) get_vertices() const;
167  INLINE CPT(GeomVertexArrayDataHandle) get_vertices_handle(Thread *current_thread) const;
168  PT(GeomVertexArrayData) modify_vertices(int num_vertices = -1);
169  INLINE PT(GeomVertexArrayDataHandle) modify_vertices_handle(Thread *current_thread);
170  void set_vertices(const GeomVertexArrayData *vertices, int num_vertices = -1);
171  void set_nonindexed_vertices(int first_vertex, int num_vertices);
172 
173  INLINE int get_index_stride() const;
174  INLINE int get_strip_cut_index() const;
175  MAKE_PROPERTY(index_stride, get_index_stride);
176  MAKE_PROPERTY(strip_cut_index, get_strip_cut_index);
177 
178  INLINE CPTA_int get_ends() const;
179  PTA_int modify_ends();
180  void set_ends(PTA_int ends);
181 
182  INLINE CPT(GeomVertexArrayData) get_mins() const;
183  INLINE CPT(GeomVertexArrayData) get_maxs() const;
184  MAKE_PROPERTY(mins, get_mins);
185  MAKE_PROPERTY(maxs, get_maxs);
186 
187  void set_minmax(int min_vertex, int max_vertex,
189  void clear_minmax();
190 
191  virtual int get_num_vertices_per_primitive() const;
192  virtual int get_min_num_vertices_per_primitive() const;
193  virtual int get_num_unused_vertices_per_primitive() const;
194  MAKE_PROPERTY(num_vertices_per_primitive, get_num_vertices_per_primitive);
195  MAKE_PROPERTY(min_num_vertices_per_primitive, get_min_num_vertices_per_primitive);
196  MAKE_PROPERTY(num_unused_vertices_per_primitive, get_num_unused_vertices_per_primitive);
197 
198 public:
199  void prepare(PreparedGraphicsObjects *prepared_objects);
200  bool is_prepared(PreparedGraphicsObjects *prepared_objects) const;
201 
202  IndexBufferContext *prepare_now(PreparedGraphicsObjects *prepared_objects,
204  bool release(PreparedGraphicsObjects *prepared_objects);
205  int release_all();
206 
207  static const GeomVertexArrayFormat *get_index_format(NumericType index_type);
208  INLINE const GeomVertexArrayFormat *get_index_format() const;
209  INLINE PT(GeomVertexArrayData) make_index_data() const;
210 
211 private:
212  static CPT(GeomVertexArrayFormat) make_index_format(NumericType index_type);
213 
214  void clear_prepared(PreparedGraphicsObjects *prepared_objects);
215  static int get_highest_index_value(NumericType index_type);
216  static int get_strip_cut_index(NumericType index_type);
217 
218 public:
219  virtual bool draw(GraphicsStateGuardianBase *gsg,
220  const GeomPrimitivePipelineReader *reader,
221  bool force) const=0;
222 
223  void calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point,
224  PN_stdfloat &sq_center_dist, bool &found_any,
225  const GeomVertexData *vertex_data,
226  bool got_mat, const LMatrix4 &mat,
227  const InternalName *column_name,
228  Thread *current_thread) const;
229 
230  void calc_sphere_radius(const LPoint3 &center,
231  PN_stdfloat &sq_radius, bool &found_any,
232  const GeomVertexData *vertex_data,
233  Thread *current_thread) const;
234 
235 protected:
236  virtual CPT(GeomPrimitive) decompose_impl() const;
237  virtual CPT(GeomVertexArrayData) rotate_impl() const;
238  virtual CPT(GeomPrimitive) doubleside_impl() const;
239  virtual CPT(GeomPrimitive) reverse_impl() const;
240  virtual bool requires_unused_vertices() const;
241  virtual void append_unused_vertices(GeomVertexArrayData *vertices,
242  int vertex);
243 
244 private:
245  class CData;
246 
247  void recompute_minmax(CData *cdata);
248  void do_make_indexed(CData *cdata);
249  void consider_elevate_index_type(CData *cdata, int vertex);
250  void do_set_index_type(CData *cdata, NumericType index_type);
251  PT(GeomVertexArrayData) do_modify_vertices(CData *cdata);
252 
253 private:
254  // A GeomPrimitive keeps a list (actually, a map) of all the
255  // PreparedGraphicsObjects tables that it has been prepared into. Each PGO
256  // conversely keeps a list (a set) of all the Geoms that have been prepared
257  // there. When either destructs, it removes itself from the other's list.
259  Contexts _contexts;
260 
261  // This is the data that must be cycled between pipeline stages.
262  class EXPCL_PANDA_GOBJ CData : public CycleData {
263  public:
264  INLINE CData();
265  INLINE CData(const CData &copy);
266  ALLOC_DELETED_CHAIN(CData);
267 
268  virtual CycleData *make_copy() const;
269  virtual void write_datagram(BamWriter *manager, Datagram &dg) const;
270  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
271  virtual void fillin(DatagramIterator &scan, BamReader *manager);
272  virtual TypeHandle get_parent_type() const {
273  return GeomPrimitive::get_class_type();
274  }
275 
276  ShadeModel _shade_model;
277  int _first_vertex;
278  int _num_vertices;
279  NumericType _index_type;
280  UsageHint _usage_hint;
281  COWPT(GeomVertexArrayData) _vertices;
282  PTA_int _ends;
283  COWPT(GeomVertexArrayData) _mins;
284  COWPT(GeomVertexArrayData) _maxs;
285  UpdateSeq _modified;
286 
287  bool _got_minmax;
288  unsigned int _min_vertex;
289  unsigned int _max_vertex;
290 
291  public:
292  static TypeHandle get_class_type() {
293  return _type_handle;
294  }
295  static void init_type() {
296  register_type(_type_handle, "GeomPrimitive::CData");
297  }
298 
299  private:
300  static TypeHandle _type_handle;
301 
302  friend class GeomPrimitive;
303  };
304 
305  PipelineCycler<CData> _cycler;
306  typedef CycleDataReader<CData> CDReader;
307  typedef CycleDataWriter<CData> CDWriter;
308  typedef CycleDataStageReader<CData> CDStageReader;
309  typedef CycleDataStageWriter<CData> CDStageWriter;
310 
311 private:
312  static PStatCollector _decompose_pcollector;
313  static PStatCollector _doubleside_pcollector;
314  static PStatCollector _reverse_pcollector;
315  static PStatCollector _rotate_pcollector;
316 
317 public:
318  virtual void write_datagram(BamWriter *manager, Datagram &dg);
319 
320  virtual void finalize(BamReader *manager);
321 
322 protected:
323  void fillin(DatagramIterator &scan, BamReader *manager);
324 
325 public:
326  static TypeHandle get_class_type() {
327  return _type_handle;
328  }
329  static void init_type() {
330  CopyOnWriteObject::init_type();
331  register_type(_type_handle, "GeomPrimitive",
332  CopyOnWriteObject::get_class_type());
333  CData::init_type();
334  }
335  virtual TypeHandle get_type() const {
336  return get_class_type();
337  }
338  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
339 
340 private:
341  static TypeHandle _type_handle;
342 
343  friend class Geom;
344  friend class PreparedGraphicsObjects;
345  friend class GeomPrimitivePipelineReader;
346 };
347 
348 /**
349  * Encapsulates the data from a GeomPrimitive, pre-fetched for one stage of
350  * the pipeline.
351  */
352 class EXPCL_PANDA_GOBJ GeomPrimitivePipelineReader : public GeomEnums {
353 public:
354  INLINE GeomPrimitivePipelineReader(CPT(GeomPrimitive) object, Thread *current_thread);
356  INLINE ~GeomPrimitivePipelineReader();
357 
358  ALLOC_DELETED_CHAIN(GeomPrimitivePipelineReader);
359 
360  GeomPrimitivePipelineReader &operator = (const GeomPrimitivePipelineReader &copy) = delete;
361 
362  INLINE const GeomPrimitive *get_object() const;
363  INLINE Thread *get_current_thread() const;
364 
365  void check_minmax() const;
366 
367  INLINE ShadeModel get_shade_model() const;
368  INLINE UsageHint get_usage_hint() const;
369  INLINE NumericType get_index_type() const;
370  INLINE bool is_indexed() const;
371  int get_first_vertex() const;
372  INLINE int get_num_vertices() const;
373  int get_vertex(int i) const;
374  int get_num_primitives() const;
375  void get_referenced_vertices(BitArray &bits) const;
376  INLINE int get_min_vertex() const;
377  INLINE int get_max_vertex() const;
378  INLINE int get_data_size_bytes() const;
379  INLINE UpdateSeq get_modified() const;
380  bool check_valid(const GeomVertexDataPipelineReader *data_reader) const;
381  INLINE int get_index_stride() const;
382  INLINE const unsigned char *get_read_pointer(bool force) const;
383  INLINE int get_strip_cut_index() const;
384  INLINE CPTA_int get_ends() const;
385  INLINE CPT(GeomVertexArrayData) get_mins() const;
386  INLINE CPT(GeomVertexArrayData) get_maxs() const;
387 
388  INLINE IndexBufferContext *prepare_now(PreparedGraphicsObjects *prepared_objects,
389  GraphicsStateGuardianBase *gsg) const;
390  INLINE bool draw(GraphicsStateGuardianBase *gsg, bool force) const;
391 
392 private:
393  CPT(GeomPrimitive) _object;
394  Thread *_current_thread;
395  const GeomPrimitive::CData *_cdata;
396 
397  CPT(GeomVertexArrayData) _vertices;
398  const GeomVertexArrayData::CData *_vertices_cdata;
399 
400 public:
401  static TypeHandle get_class_type() {
402  return _type_handle;
403  }
404  static void init_type() {
405  register_type(_type_handle, "GeomPrimitivePipelineReader");
406  }
407 
408 private:
409  static TypeHandle _type_handle;
410 };
411 
412 INLINE std::ostream &operator << (std::ostream &out, const GeomPrimitive &obj);
413 
414 #include "geomPrimitive.I"
415 
416 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a special class object that holds all the information returned by a particular GSG to indicat...
This class is similar to CycleDataWriter, except it allows writing to a particular stage of the pipel...
A single page of data maintained by a PipelineCycler.
Definition: cycleData.h:47
This class exists just to provide scoping for the various enumerated types used by Geom,...
Definition: geomEnums.h:24
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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.
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:56
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual TypeHandle get_parent_type() const
Returns the type of the container that owns the CycleData.
Definition: cycleData.cxx:76
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
This is our own Panda specialization on the default STL list.
Definition: plist.h:35
This data object is returned by GeomVertexArrayData::get_handle() or modify_handle().
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A table of objects that are saved within the graphics context for reference by handle later.
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void finalize(BamReader *manager)
Called by the BamReader to perform any final actions needed for setting up the object after all objec...
A dynamic array with an unlimited number of bits.
Definition: bitArray.h:39
A lightweight class that represents a single element that may be timed and/or counted via stats.
virtual void write_datagram(BamWriter *, Datagram &) const
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition: cycleData.cxx:32
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:54
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
virtual int complete_pointers(TypedWritable **p_list, BamReader *manager)
Receives an array of pointers, one for each time manager->read_pointer() was called in fillin().
Definition: cycleData.cxx:48
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Encodes a string name in a hash table, mapping it to a pointer.
Definition: internalName.h:38
virtual void fillin(DatagramIterator &scan, BamReader *manager)
This internal function is intended to be called by each class's make_from_bam() method to read in all...
Definition: cycleData.cxx:58
This class is similar to CycleDataReader, except it allows reading from a particular stage of the pip...
This base class provides basic reference counting, but also can be used with a CopyOnWritePointer to ...
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
A thread; that is, a lightweight process.
Definition: thread.h:46
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This describes the structure of a single array within a Geom data.
Encapsulates the data from a GeomVertexData, pre-fetched for one stage of the pipeline.
A class to retrieve the individual data elements previously stored in a Datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
This is a sequence number that increments monotonically.
Definition: updateSeq.h:37
Encapsulates the data from a GeomPrimitive, pre-fetched for one stage of the pipeline.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Similar to PointerToArray, except that its contents may not be modified.
This is the data for one array of a GeomVertexData structure.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.