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