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