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