Panda3D
eggAttributes.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 eggAttributes.cxx
10  * @author drose
11  * @date 1999-01-16
12  */
13 
14 #include "eggAttributes.h"
15 #include "eggParameters.h"
16 #include "eggMorph.h"
17 #include "eggMorphList.h"
18 
19 #include "indent.h"
20 
21 TypeHandle EggAttributes::_type_handle;
22 
23 
24 /**
25  *
26  */
27 EggAttributes::
28 EggAttributes() {
29  _flags = 0;
30 }
31 
32 /**
33  *
34  */
35 EggAttributes::
36 EggAttributes(const EggAttributes &copy) {
37  (*this) = copy;
38 }
39 
40 /**
41  *
42  */
43 EggAttributes &EggAttributes::
44 operator = (const EggAttributes &copy) {
45  _flags = copy._flags;
46  _normal = copy._normal;
47  _color = copy._color;
48  _dnormals = copy._dnormals;
49  _drgbas = copy._drgbas;
50  return *this;
51 }
52 
53 /**
54  *
55  */
56 EggAttributes::
57 ~EggAttributes() {
58 }
59 
60 
61 /**
62  * Writes the attributes to the indicated output stream in Egg format.
63  */
64 void EggAttributes::
65 write(std::ostream &out, int indent_level) const {
66  if (has_normal()) {
67  if (_dnormals.empty()) {
68  indent(out, indent_level)
69  << "<Normal> { " << get_normal() << " }\n";
70  } else {
71  indent(out, indent_level) << "<Normal> {\n";
72  indent(out, indent_level + 2) << get_normal() << "\n";
73  _dnormals.write(out, indent_level + 2, "<DNormal>", 3);
74  indent(out, indent_level) << "}\n";
75  }
76  }
77  if (has_color()) {
78  if (_drgbas.empty()) {
79  indent(out, indent_level)
80  << "<RGBA> { " << get_color() << " }\n";
81  } else {
82  indent(out, indent_level) << "<RGBA> {\n";
83  indent(out, indent_level + 2) << get_color() << "\n";
84  _drgbas.write(out, indent_level + 2, "<DRBGA>", 4);
85  indent(out, indent_level) << "}\n";
86  }
87  }
88 }
89 
90 
91 /**
92  * An ordering operator to compare two vertices for sorting order. This
93  * imposes an arbitrary ordering useful to identify unique vertices.
94  */
96 compare_to(const EggAttributes &other) const {
97  if (_flags != other._flags) {
98  return (int)_flags - (int)other._flags;
99  }
100 
101  if (has_normal()) {
102  int compare =
103  _normal.compare_to(other._normal, egg_parameters->_normal_threshold);
104  if (compare != 0) {
105  return compare;
106  }
107  compare = _dnormals.compare_to(other._dnormals, egg_parameters->_normal_threshold);
108  if (compare != 0) {
109  return compare;
110  }
111  }
112 
113  if (has_color()) {
114  int compare =
115  _color.compare_to(other._color, egg_parameters->_color_threshold);
116  if (compare != 0) {
117  return compare;
118  }
119  compare = _drgbas.compare_to(other._drgbas, egg_parameters->_color_threshold);
120  if (compare != 0) {
121  return compare;
122  }
123  }
124 
125  return 0;
126 }
127 
128 /**
129  * Applies the indicated transformation matrix to the attributes.
130  */
131 void EggAttributes::
132 transform(const LMatrix4d &mat) {
133  if (has_normal()) {
134  _normal = _normal * mat;
135  LVector3d old_normal = _normal;
136  _normal.normalize();
137 
138  EggMorphNormalList::iterator mi;
139  for (mi = _dnormals.begin(); mi != _dnormals.end(); ++mi) {
140  // We can safely cast the morph object to a non-const, because we're not
141  // changing its name, which is the only thing the set cares about
142  // preserving.
143  EggMorphNormal &morph = (EggMorphNormal &)(*mi);
144 
145  // A bit of funny business to ensure the offset normal is normalized
146  // after the transform. This will break strange normal morphs that want
147  // to change the length of the normal, but what else can we do?
148  LVector3d offset = (*mi).get_offset() * mat;
149  LVector3d n = old_normal + offset;
150  n.normalize();
151  morph.set_offset(n - _normal);
152  }
153  }
154 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void transform(const LMatrix4d &mat)
Applies the indicated transformation matrix to the attributes.
A single <Dxyz> or <Duv> or some such entry.
Definition: eggMorph.h:30
LColor get_color() const
Returns the color set on this particular attribute.
Definition: eggAttributes.I:91
void write(std::ostream &out, int indent_level) const
Writes the attributes to the indicated output stream in Egg format.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
int compare_to(const EggMorphList< MorphType > &other, double threshold) const
compare_to() compares a different space than the operator methods, which only check the morph's name.
Definition: eggMorphList.I:83
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int compare_to(const EggAttributes &other) const
An ordering operator to compare two vertices for sorting order.