Panda3D
eggTriangleStrip.cxx
1 // Filename: eggTriangleStrip.cxx
2 // Created by: drose (13Mar05)
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 "eggTriangleStrip.h"
16 #include "eggGroupNode.h"
17 #include "eggPolygon.h"
18 #include "indent.h"
19 
20 TypeHandle EggTriangleStrip::_type_handle;
21 
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: EggTriangleStrip::Destructor
25 // Access: Published, Virtual
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 EggTriangleStrip::
29 ~EggTriangleStrip() {
30  clear();
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: EggTriangleStrip::write
35 // Access: Published, Virtual
36 // Description: Writes the triangle strip to the indicated output
37 // stream in Egg format.
38 ////////////////////////////////////////////////////////////////////
40 write(ostream &out, int indent_level) const {
41  write_header(out, indent_level, "<TriangleStrip>");
42  write_body(out, indent_level+2);
43  indent(out, indent_level) << "}\n";
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: EggTriangleStrip::get_num_lead_vertices
48 // Access: Protected, Virtual
49 // Description: Returns the number of initial vertices that are not
50 // used in defining any component; the first component
51 // is defined by the (n + 1)th vertex, and then a new
52 // component at each vertex thereafter.
53 ////////////////////////////////////////////////////////////////////
54 int EggTriangleStrip::
55 get_num_lead_vertices() const {
56  return 2;
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: EggTriangleStrip::triangulate_poly
61 // Access: Protected, Virtual
62 // Description: Fills the container up with EggPolygons that
63 // represent the component triangles of this triangle
64 // strip.
65 //
66 // It is assumed that the EggTriangleStrip is not
67 // already a child of any other group when this function
68 // is called.
69 //
70 // Returns true if the triangulation is successful, or
71 // false if there was some error (in which case the
72 // container may contain some partial triangulation).
73 ////////////////////////////////////////////////////////////////////
74 bool EggTriangleStrip::
75 do_triangulate(EggGroupNode *container) const {
76  if (size() < 3) {
77  return false;
78  }
79  const_iterator vi = begin();
80  EggVertex *v0 = (*vi);
81  ++vi;
82  EggVertex *v1 = (*vi);
83  ++vi;
84  bool reversed = false;
85 
86  for (int i = 0; i < (int)size() - 2; i++) {
87  PT(EggPolygon) poly = new EggPolygon;
88  poly->copy_attributes(*this);
89  const EggAttributes *attrib = get_component(i);
90  if (attrib->has_color()) {
91  poly->set_color(attrib->get_color());
92  }
93  if (attrib->has_normal()) {
94  poly->set_normal(attrib->get_normal());
95  }
96 
97  if (reversed) {
98  poly->add_vertex(v1);
99  poly->add_vertex(v0);
100  reversed = false;
101  } else {
102  poly->add_vertex(v0);
103  poly->add_vertex(v1);
104  reversed = true;
105  }
106  poly->add_vertex(*vi);
107  v0 = v1;
108  v1 = *vi;
109  container->add_child(poly);
110  ++vi;
111  }
112 
113  return true;
114 }
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
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 write(ostream &out, int indent_level) const
Writes the triangle strip to the indicated output stream in Egg format.
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
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