Panda3D
geomTrifans.cxx
1 // Filename: geomTrifans.cxx
2 // Created by: drose (08Mar05)
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 #include "geomTrifans.h"
16 #include "geomTriangles.h"
17 #include "geomVertexRewriter.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "graphicsStateGuardianBase.h"
21 
22 TypeHandle GeomTrifans::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: GeomTrifans::Constructor
26 // Access: Published
27 // Description:
28 ////////////////////////////////////////////////////////////////////
29 GeomTrifans::
30 GeomTrifans(GeomTrifans::UsageHint usage_hint) :
31  GeomPrimitive(usage_hint)
32 {
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: GeomTrifans::Copy Constructor
37 // Access: Published
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 GeomTrifans::
41 GeomTrifans(const GeomTrifans &copy) :
42  GeomPrimitive(copy)
43 {
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: GeomTrifans::Destructor
48 // Access: Published, Virtual
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 GeomTrifans::
52 ~GeomTrifans() {
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: GeomTrifans::make_copy
57 // Access: Public, Virtual
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 PT(GeomPrimitive) GeomTrifans::
61 make_copy() const {
62  return new GeomTrifans(*this);
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: GeomTrifans::get_primitive_type
67 // Access: Public, Virtual
68 // Description: Returns the fundamental rendering type of this
69 // primitive: whether it is points, lines, or polygons.
70 //
71 // This is used to set up the appropriate antialiasing
72 // settings when AntialiasAttrib::M_auto is in effect;
73 // it also implies the type of primitive that will be
74 // produced when decompose() is called.
75 ////////////////////////////////////////////////////////////////////
76 GeomPrimitive::PrimitiveType GeomTrifans::
77 get_primitive_type() const {
78  return PT_polygons;
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: GeomTrifans::get_geom_rendering
83 // Access: Published, Virtual
84 // Description: Returns the set of GeomRendering bits that represent
85 // the rendering properties required to properly render
86 // this primitive.
87 ////////////////////////////////////////////////////////////////////
88 int GeomTrifans::
89 get_geom_rendering() const {
90  if (is_indexed()) {
91  return GR_triangle_fan | GR_indexed_other;
92  } else {
93  return GR_triangle_fan;
94  }
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: GeomTrifans::draw
99 // Access: Public, Virtual
100 // Description: Calls the appropriate method on the GSG to draw the
101 // primitive.
102 ////////////////////////////////////////////////////////////////////
103 bool GeomTrifans::
105  bool force) const {
106  return gsg->draw_trifans(reader, force);
107 }
108 
109 ////////////////////////////////////////////////////////////////////
110 // Function: GeomTrifans::decompose_impl
111 // Access: Protected, Virtual
112 // Description: Decomposes a complex primitive type into a simpler
113 // primitive type, for instance triangle strips to
114 // triangles, and returns a pointer to the new primitive
115 // definition. If the decomposition cannot be
116 // performed, this might return the original object.
117 //
118 // This method is useful for application code that wants
119 // to iterate through the set of triangles on the
120 // primitive without having to write handlers for each
121 // possible kind of primitive type.
122 ////////////////////////////////////////////////////////////////////
123 CPT(GeomPrimitive) GeomTrifans::
124 decompose_impl() const {
125  PT(GeomTriangles) triangles = new GeomTriangles(get_usage_hint());
126  triangles->set_shade_model(get_shade_model());
127  CPTA_int ends = get_ends();
128 
129  int num_vertices = get_num_vertices();
130 
131  int vi = 0;
132  int li = 0;
133  while (li < (int)ends.size()) {
134  int end = ends[li];
135  nassertr(vi + 2 <= end, triangles.p());
136  int v0 = get_vertex(vi);
137  ++vi;
138  int v1 = get_vertex(vi);
139  ++vi;
140  while (vi < end) {
141  int v2 = get_vertex(vi);
142  ++vi;
143  triangles->add_vertex(v0);
144  triangles->add_vertex(v1);
145  triangles->add_vertex(v2);
146  v1 = v2;
147  triangles->close_primitive();
148  }
149  ++li;
150  }
151 
152  nassertr(vi == num_vertices, NULL);
153 
154  return triangles.p();
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: GeomTrifans::rotate_impl
159 // Access: Protected, Virtual
160 // Description: The virtual implementation of do_rotate().
161 ////////////////////////////////////////////////////////////////////
162 CPT(GeomVertexArrayData) GeomTrifans::
163 rotate_impl() const {
164  // Actually, we can't rotate fans without chaging the winding order.
165  // It's an error to define a flat shade model for a GeomTrifan.
166  nassertr(false, NULL);
167  return NULL;
168 }
169 
170 ////////////////////////////////////////////////////////////////////
171 // Function: GeomTrifans::register_with_read_factory
172 // Access: Public, Static
173 // Description: Tells the BamReader how to create objects of type
174 // Geom.
175 ////////////////////////////////////////////////////////////////////
176 void GeomTrifans::
177 register_with_read_factory() {
178  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
179 }
180 
181 ////////////////////////////////////////////////////////////////////
182 // Function: GeomTrifans::make_from_bam
183 // Access: Protected, Static
184 // Description: This function is called by the BamReader's factory
185 // when a new object of type Geom is encountered
186 // in the Bam file. It should create the Geom
187 // and extract its information from the file.
188 ////////////////////////////////////////////////////////////////////
189 TypedWritable *GeomTrifans::
190 make_from_bam(const FactoryParams &params) {
191  GeomTrifans *object = new GeomTrifans(UH_unspecified);
192  DatagramIterator scan;
193  BamReader *manager;
194 
195  parse_params(params, scan, manager);
196  object->fillin(scan, manager);
197 
198  return object;
199 }
ShadeModel get_shade_model() const
Returns the ShadeModel hint for this primitive.
Definition: geomPrimitive.I:24
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
virtual int get_geom_rendering() const
Returns the set of GeomRendering bits that represent the rendering properties required to properly re...
Defines a series of triangle fans.
Definition: geomTrifans.h:25
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
int get_num_vertices() const
Returns the number of indices used by all the primitives in this object.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
int get_vertex(int i) const
Returns the ith vertex index in the table.
UsageHint get_usage_hint() const
Returns the usage hint for this primitive.
Definition: geomPrimitive.I:67
Defines a series of disconnected triangles.
Definition: geomTriangles.h:25
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
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.
Similar to PointerToArray, except that its contents may not be modified.
This is the data for one array of a GeomVertexData structure.