Panda3D
eggTriangleFan.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 eggTriangleFan.cxx
10  * @author drose
11  * @date 2005-03-23
12  */
13 
14 #include "eggTriangleFan.h"
15 #include "eggGroupNode.h"
16 #include "eggPolygon.h"
17 #include "indent.h"
18 
19 TypeHandle EggTriangleFan::_type_handle;
20 
21 /**
22  *
23  */
24 EggTriangleFan::
25 ~EggTriangleFan() {
26  clear();
27 }
28 
29 /**
30  * Makes a copy of this object.
31  */
33 make_copy() const {
34  return new EggTriangleFan(*this);
35 }
36 
37 /**
38  * Writes the triangle fan to the indicated output stream in Egg format.
39  */
41 write(std::ostream &out, int indent_level) const {
42  write_header(out, indent_level, "<TriangleFan>");
43  write_body(out, indent_level+2);
44  indent(out, indent_level) << "}\n";
45 }
46 
47 /**
48  * Sets the first vertex of the triangle (or each component) to the primitive
49  * normal and/or color, if the primitive is flat-shaded. This reflects the
50  * DirectX convention of storing flat-shaded properties on the first vertex,
51  * although it is not usually a convention in Egg.
52  *
53  * This may introduce redundant vertices to the vertex pool.
54  */
57  // In the case of a triangle fan, the first vertex of the fan is the common
58  // vertex, so we consider the second vertex to be the key vertex of the
59  // first triangle, and move from there.
60  for (size_t i = 0; i < get_num_components(); ++i) {
61  EggAttributes *component = get_component(i);
62  do_apply_flat_attribute(i + 1, component);
63  }
64 }
65 
66 /**
67  * Returns the number of initial vertices that are not used in defining any
68  * component; the first component is defined by the (n + 1)th vertex, and then
69  * a new component at each vertex thereafter.
70  */
71 int EggTriangleFan::
72 get_num_lead_vertices() const {
73  return 2;
74 }
75 
76 /**
77  * Fills the container up with EggPolygons that represent the component
78  * triangles of this triangle fan.
79  *
80  * It is assumed that the EggTriangleFan is not already a child of any other
81  * group when this function is called.
82  *
83  * Returns true if the triangulation is successful, or false if there was some
84  * error (in which case the container may contain some partial triangulation).
85  */
86 bool EggTriangleFan::
87 do_triangulate(EggGroupNode *container) const {
88  if (size() < 3) {
89  return false;
90  }
91  const_iterator vi = begin();
92  EggVertex *v0 = (*vi);
93  ++vi;
94  EggVertex *v1 = (*vi);
95  ++vi;
96 
97  for (int i = 0; i < (int)size() - 2; i++) {
98  PT(EggPolygon) poly = new EggPolygon;
99  poly->copy_attributes(*this);
100  const EggAttributes *attrib = get_component(i);
101  if (attrib->has_color()) {
102  poly->set_color(attrib->get_color());
103  }
104  if (attrib->has_normal()) {
105  poly->set_normal(attrib->get_normal());
106  }
107 
108  poly->add_vertex(v0);
109  poly->add_vertex(v1);
110  poly->add_vertex(*vi);
111  v1 = *vi;
112  container->add_child(poly);
113  ++vi;
114  }
115 
116  return true;
117 }
void write_header(std::ostream &out, int indent_level, const char *egg_keyword) const
Writes the first line of the egg object, e.g.
virtual EggTriangleFan * make_copy() const override
Makes a copy of this object.
A base class for nodes in the hierarchy that are not leaf nodes.
Definition: eggGroupNode.h:46
void clear()
Removes all of the vertices from the primitive.
Definition: eggPrimitive.I:352
get_component
Returns the attributes for the nth component triangle.
virtual void apply_first_attribute() override
Sets the first vertex of the triangle (or each component) to the primitive normal and/or color,...
A connected fan of triangles.
void copy_attributes(const EggAttributes &other)
Copies the rendering attributes from the indicated primitive.
LColor get_color() const
Returns the color set on this particular attribute.
Definition: eggAttributes.I:91
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The set of attributes that may be applied to vertices as well as polygons, such as surface normal and...
Definition: eggAttributes.h:33
virtual void write(std::ostream &out, int indent_level) const override
Writes the triangle fan to the indicated output stream in Egg format.
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal.
Definition: eggVertex.h:39
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A single polygon.
Definition: eggPolygon.h:24
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
EggNode * add_child(EggNode *node)
Adds the indicated child to the group and returns it.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_num_components
Returns the number of individual component triangles within the composite.