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