Panda3D
 All Classes Functions Variables Enumerations
eggAttributes.cxx
1 // Filename: eggAttributes.cxx
2 // Created by: drose (16Jan99)
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 "eggAttributes.h"
16 #include "eggParameters.h"
17 #include "eggMorph.h"
18 #include "eggMorphList.h"
19 
20 #include "indent.h"
21 
22 TypeHandle EggAttributes::_type_handle;
23 
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: EggAttributes::Constructor
27 // Access: Published
28 // Description:
29 ////////////////////////////////////////////////////////////////////
30 EggAttributes::
31 EggAttributes() {
32  _flags = 0;
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: EggAttributes::Copy constructor
37 // Access: Published
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 EggAttributes::
41 EggAttributes(const EggAttributes &copy) {
42  (*this) = copy;
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: EggAttributes::Copy assignment operator
47 // Access: Published
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 EggAttributes &EggAttributes::
51 operator = (const EggAttributes &copy) {
52  _flags = copy._flags;
53  _normal = copy._normal;
54  _color = copy._color;
55  _dnormals = copy._dnormals;
56  _drgbas = copy._drgbas;
57  return *this;
58 }
59 
60 ////////////////////////////////////////////////////////////////////
61 // Function: EggAttributes::Destructor
62 // Access: Published, Virtual
63 // Description:
64 ////////////////////////////////////////////////////////////////////
65 EggAttributes::
66 ~EggAttributes() {
67 }
68 
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: EggAttributes::write
72 // Access: Published
73 // Description: Writes the attributes to the indicated output stream in
74 // Egg format.
75 ////////////////////////////////////////////////////////////////////
76 void EggAttributes::
77 write(ostream &out, int indent_level) const {
78  if (has_normal()) {
79  if (_dnormals.empty()) {
80  indent(out, indent_level)
81  << "<Normal> { " << get_normal() << " }\n";
82  } else {
83  indent(out, indent_level) << "<Normal> {\n";
84  indent(out, indent_level + 2) << get_normal() << "\n";
85  _dnormals.write(out, indent_level + 2, "<DNormal>", 3);
86  indent(out, indent_level) << "}\n";
87  }
88  }
89  if (has_color()) {
90  if (_drgbas.empty()) {
91  indent(out, indent_level)
92  << "<RGBA> { " << get_color() << " }\n";
93  } else {
94  indent(out, indent_level) << "<RGBA> {\n";
95  indent(out, indent_level + 2) << get_color() << "\n";
96  _drgbas.write(out, indent_level + 2, "<DRBGA>", 4);
97  indent(out, indent_level) << "}\n";
98  }
99  }
100 }
101 
102 
103 ////////////////////////////////////////////////////////////////////
104 // Function: EggAttributes::compare_to
105 // Access: Published
106 // Description: An ordering operator to compare two vertices for
107 // sorting order. This imposes an arbitrary ordering
108 // useful to identify unique vertices.
109 ////////////////////////////////////////////////////////////////////
110 int EggAttributes::
111 compare_to(const EggAttributes &other) const {
112  if (_flags != other._flags) {
113  return (int)_flags - (int)other._flags;
114  }
115 
116  if (has_normal()) {
117  int compare =
118  _normal.compare_to(other._normal, egg_parameters->_normal_threshold);
119  if (compare != 0) {
120  return compare;
121  }
122  compare = _dnormals.compare_to(other._dnormals, egg_parameters->_normal_threshold);
123  if (compare != 0) {
124  return compare;
125  }
126  }
127 
128  if (has_color()) {
129  int compare =
130  _color.compare_to(other._color, egg_parameters->_color_threshold);
131  if (compare != 0) {
132  return compare;
133  }
134  compare = _drgbas.compare_to(other._drgbas, egg_parameters->_color_threshold);
135  if (compare != 0) {
136  return compare;
137  }
138  }
139 
140  return 0;
141 }
142 
143 ////////////////////////////////////////////////////////////////////
144 // Function: EggAttributes::transform
145 // Access: Published, Virtual
146 // Description: Applies the indicated transformation matrix to the
147 // attributes.
148 ////////////////////////////////////////////////////////////////////
149 void EggAttributes::
150 transform(const LMatrix4d &mat) {
151  if (has_normal()) {
152  _normal = _normal * mat;
153  LVector3d old_normal = _normal;
154  _normal.normalize();
155 
156  EggMorphNormalList::iterator mi;
157  for (mi = _dnormals.begin(); mi != _dnormals.end(); ++mi) {
158  // We can safely cast the morph object to a non-const, because
159  // we're not changing its name, which is the only thing the set
160  // cares about preserving.
161  EggMorphNormal &morph = (EggMorphNormal &)(*mi);
162 
163  // A bit of funny business to ensure the offset normal is
164  // normalized after the transform. This will break strange
165  // normal morphs that want to change the length of the normal,
166  // but what else can we do?
167  LVector3d offset = (*mi).get_offset() * mat;
168  LVector3d n = old_normal + offset;
169  n.normalize();
170  morph.set_offset(n - _normal);
171  }
172  }
173 }
int compare_to(const LVecBase4f &other) const
This flavor of compare_to uses a default threshold value based on the numeric type.
Definition: lvecBase4.h:971
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
LColor get_color() const
Returns the color set on this particular attribute.
void write(ostream &out, int indent_level) const
Writes the attributes to the indicated output stream in Egg format.
void transform(const LMatrix4d &mat)
Applies the indicated transformation matrix to the attributes.
int compare_to(const EggAttributes &other) const
An ordering operator to compare two vertices for sorting order.
A single &lt;Dxyz&gt; or &lt;Duv&gt; or some such entry.
Definition: eggMorph.h:33
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&#39;s name...
Definition: eggMorphList.I:101
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:2132
The set of attributes that may be applied to vertices as well as polygons, such as surface normal and...
Definition: eggAttributes.h:37
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:746
int compare_to(const LVecBase3d &other) const
This flavor of compare_to uses a default threshold value based on the numeric type.
Definition: lvecBase3.h:2273
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85