Panda3D
geomLinestripsAdjacency.cxx
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 geomLinestripsAdjacency.cxx
10  * @author rdb
11  * @date 2018-03-01
12  */
13 
15 #include "geomLines.h"
16 #include "geomLinesAdjacency.h"
17 #include "geomVertexRewriter.h"
18 #include "pStatTimer.h"
19 #include "bamReader.h"
20 #include "bamWriter.h"
22 
23 TypeHandle GeomLinestripsAdjacency::_type_handle;
24 
25 /**
26  *
27  */
28 GeomLinestripsAdjacency::
29 GeomLinestripsAdjacency(GeomEnums::UsageHint usage_hint) :
30  GeomPrimitive(usage_hint)
31 {
32 }
33 
34 /**
35  *
36  */
37 GeomLinestripsAdjacency::
38 GeomLinestripsAdjacency(const GeomLinestripsAdjacency &copy) :
39  GeomPrimitive(copy)
40 {
41 }
42 
43 /**
44  *
45  */
46 GeomLinestripsAdjacency::
47 ~GeomLinestripsAdjacency() {
48 }
49 
50 /**
51  *
52  */
53 PT(GeomPrimitive) GeomLinestripsAdjacency::
54 make_copy() const {
55  return new GeomLinestripsAdjacency(*this);
56 }
57 
58 /**
59  * Returns the fundamental rendering type of this primitive: whether it is
60  * points, lines, or polygons.
61  *
62  * This is used to set up the appropriate antialiasing settings when
63  * AntialiasAttrib::M_auto is in effect; it also implies the type of primitive
64  * that will be produced when decompose() is called.
65  */
66 GeomPrimitive::PrimitiveType GeomLinestripsAdjacency::
67 get_primitive_type() const {
68  return PT_lines;
69 }
70 
71 /**
72  * Returns the set of GeomRendering bits that represent the rendering
73  * properties required to properly render this primitive.
74  */
75 int GeomLinestripsAdjacency::
76 get_geom_rendering() const {
77  if (is_indexed()) {
78  if (get_num_primitives() > 1) {
79  return GR_line_strip | GR_indexed_other | GR_strip_cut_index | GR_adjacency;
80  } else {
81  return GR_line_strip | GR_indexed_other | GR_adjacency;
82  }
83  } else {
84  return GR_line_strip | GR_adjacency;
85  }
86 }
87 
88 /**
89  * Returns the minimum number of vertices that must be added before
90  * close_primitive() may legally be called.
91  */
92 int GeomLinestripsAdjacency::
93 get_min_num_vertices_per_primitive() const {
94  return 4;
95 }
96 
97 /**
98  * Returns the number of vertices that are added between primitives that
99  * aren't, strictly speaking, part of the primitives themselves. This is
100  * used, for instance, to define degenerate triangles to connect otherwise
101  * disconnected triangle strips.
102  */
103 int GeomLinestripsAdjacency::
104 get_num_unused_vertices_per_primitive() const {
105  return 1;
106 }
107 
108 /**
109  * Calls the appropriate method on the GSG to draw the primitive.
110  */
111 bool GeomLinestripsAdjacency::
113  bool force) const {
114  return gsg->draw_linestrips_adj(reader, force);
115 }
116 
117 /**
118  * Decomposes a complex primitive type into a simpler primitive type, for
119  * instance line strips to lines, and returns a pointer to the new primitive
120  * definition. If the decomposition cannot be performed, this might return
121  * the original object.
122  *
123  * This method is useful for application code that wants to iterate through
124  * the set of lines on the primitive without having to write handlers for each
125  * possible kind of primitive type.
126  */
127 CPT(GeomPrimitive) GeomLinestripsAdjacency::
128 decompose_impl() const {
129  Thread *current_thread = Thread::get_current_thread();
131  lines->set_shade_model(get_shade_model());
132 
133  GeomPrimitivePipelineReader from(this, current_thread);
134  int num_vertices = from.get_num_vertices();
135  CPTA_int ends = get_ends();
136 
137  const int num_unused = 1;
138 
139  int vi = -num_unused;
140  int li = 0;
141  while (li < (int)ends.size()) {
142  // Skip unused vertices between tristrips.
143  vi += num_unused;
144  int end = ends[li];
145  nassertr(vi + 3 <= end, lines);
146  int v0 = from.get_vertex(vi++);
147  int v1 = from.get_vertex(vi++);
148  int v2 = from.get_vertex(vi++);
149  while (vi < end) {
150  int v3 = from.get_vertex(vi++);
151  lines->add_vertex(v0);
152  lines->add_vertex(v1);
153  lines->add_vertex(v2);
154  lines->add_vertex(v3);
155  v0 = v1;
156  v1 = v2;
157  v2 = v3;
158  }
159  ++li;
160  }
161  nassertr(vi == num_vertices, nullptr);
162 
163  return lines;
164 }
165 
166 /**
167  * Should be redefined to return true in any primitive that implements
168  * append_unused_vertices().
169  */
170 bool GeomLinestripsAdjacency::
171 requires_unused_vertices() const {
172  return true;
173 }
174 
175 /**
176  * Called when a new primitive is begun (other than the first primitive), this
177  * should add some degenerate vertices between primitives, if the primitive
178  * type requires that. The second parameter is the first vertex that begins
179  * the new primitive.
180  */
181 void GeomLinestripsAdjacency::
182 append_unused_vertices(GeomVertexArrayData *vertices, int vertex) {
183  GeomVertexWriter to(vertices, 0);
184  to.set_row_unsafe(vertices->get_num_rows());
185  to.add_data1i(get_strip_cut_index());
186 }
187 
188 /**
189  * Tells the BamReader how to create objects of type Geom.
190  */
193  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
194 }
195 
196 /**
197  * This function is called by the BamReader's factory when a new object of
198  * type Geom is encountered in the Bam file. It should create the Geom and
199  * extract its information from the file.
200  */
201 TypedWritable *GeomLinestripsAdjacency::
202 make_from_bam(const FactoryParams &params) {
203  GeomLinestripsAdjacency *object = new GeomLinestripsAdjacency(UH_unspecified);
204  DatagramIterator scan;
205  BamReader *manager;
206 
207  parse_params(params, scan, manager);
208  object->fillin(scan, manager);
209 
210  return object;
211 }
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
Defines a series of disconnected line segments with adjacency information, for use with geometry shad...
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
UsageHint get_usage_hint() const
Returns the minimum (i.e.
Definition: geom.cxx:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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.
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
get_current_thread
Returns a pointer to the currently-executing Thread object.
Definition: thread.h:109
static void register_with_read_factory()
Tells the BamReader how to create objects of type Geom.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
CPTA_int get_ends() const
Returns a const pointer to the primitive ends array so application code can read it directly.
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_strip_cut_index
Returns the index of the indicated type that is reserved for use as a strip cut index,...
PT(GeomPrimitive) GeomLinestripsAdjacency
Returns the fundamental rendering type of this primitive: whether it is points, lines,...
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
int get_num_rows() const
Returns the number of rows stored in the array, based on the number of bytes and the stride.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
int get_num_primitives() const
Returns the number of individual primitives stored within this object.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A thread; that is, a lightweight process.
Definition: thread.h:46
get_shade_model
Returns the ShadeModel hint for this primitive.
Definition: geomPrimitive.h:77
A class to retrieve the individual data elements previously stored in a Datagram.
Defines a series of line strips with adjacency information.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Encapsulates the data from a GeomPrimitive, pre-fetched for one stage of the pipeline.
bool is_indexed() const
Returns true if the primitive is indexed, false otherwise.
Definition: geomPrimitive.I:86
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.