Panda3D
eggVertexUV.cxx
1 // Filename: eggVertexUV.cxx
2 // Created by: drose (20Jul04)
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 "eggVertexUV.h"
16 #include "eggParameters.h"
17 
18 #include "indent.h"
19 
20 TypeHandle EggVertexUV::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: EggVertexUV::Constructor
24 // Access: Published
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 EggVertexUV::
28 EggVertexUV(const string &name, const LTexCoordd &uv) :
29  EggNamedObject(name),
30  _flags(0),
31  _uvw(uv[0], uv[1], 0.0)
32 {
33  if (get_name() == "default") {
34  clear_name();
35  }
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: EggVertexUV::Constructor
40 // Access: Published
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 EggVertexUV::
44 EggVertexUV(const string &name, const LTexCoord3d &uvw) :
45  EggNamedObject(name),
46  _flags(F_has_w),
47  _uvw(uvw)
48 {
49  if (get_name() == "default") {
50  clear_name();
51  }
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: EggVertexUV::Copy Constructor
56 // Access: Published
57 // Description:
58 ////////////////////////////////////////////////////////////////////
59 EggVertexUV::
60 EggVertexUV(const EggVertexUV &copy) :
61  EggNamedObject(copy),
62  _duvs(copy._duvs),
63  _flags(copy._flags),
64  _tangent(copy._tangent),
65  _binormal(copy._binormal),
66  _uvw(copy._uvw)
67 {
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: EggVertexUV::Copy Assignment Operator
72 // Access: Published
73 // Description:
74 ////////////////////////////////////////////////////////////////////
75 EggVertexUV &EggVertexUV::
76 operator = (const EggVertexUV &copy) {
77  EggNamedObject::operator = (copy);
78  _duvs = copy._duvs;
79  _flags = copy._flags;
80  _tangent = copy._tangent;
81  _binormal = copy._binormal;
82  _uvw = copy._uvw;
83 
84  return (*this);
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: EggVertexUV::Destructor
89 // Access: Published, Virtual
90 // Description:
91 ////////////////////////////////////////////////////////////////////
92 EggVertexUV::
93 ~EggVertexUV() {
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function: EggVertexUV::make_average
98 // Access: Published, Static
99 // Description: Creates a new EggVertexUV that contains the
100 // averaged values of the two given objects. It is
101 // an error if they don't have the same name.
102 ///////////////////////////////////////////////////////////////////
103 PT(EggVertexUV) EggVertexUV::
104 make_average(const EggVertexUV *first, const EggVertexUV *second) {
105  nassertr(first->get_name() == second->get_name(), NULL);
106  int flags = first->_flags & second->_flags;
107 
108  LTexCoord3d uvw = (first->_uvw + second->_uvw) / 2;
109 
110  PT(EggVertexUV) new_obj = new EggVertexUV(first->get_name(), uvw);
111  new_obj->_flags = flags;
112  new_obj->_tangent = (first->_tangent + second->_tangent) / 2;
113  new_obj->_binormal = (first->_binormal + second->_binormal) / 2;
114 
115  // Normalize because we're polite.
116  new_obj->_tangent.normalize();
117  new_obj->_binormal.normalize();
118  return new_obj;
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: EggVertexUV::transform
123 // Access: Published, Virtual
124 // Description: Applies the indicated transformation matrix to the
125 // UV's tangent and/or binormal. This does nothing if
126 // there is no tangent or binormal.
127 ////////////////////////////////////////////////////////////////////
128 void EggVertexUV::
129 transform(const LMatrix4d &mat) {
130  if (has_tangent()) {
131  _tangent = _tangent * mat;
132  _tangent.normalize();
133  }
134  if (has_binormal()) {
135  _binormal = _binormal * mat;
136  _binormal.normalize();
137  }
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: EggVertexUV::write
142 // Access: Public
143 // Description:
144 ////////////////////////////////////////////////////////////////////
145 void EggVertexUV::
146 write(ostream &out, int indent_level) const {
147  string inline_name = get_name();
148  if (!inline_name.empty()) {
149  inline_name += ' ';
150  }
151 
152  if (_duvs.empty() && (_flags & ~F_has_w) == 0) {
153  if (has_w()) {
154  indent(out, indent_level)
155  << "<UV> " << inline_name << "{ " << get_uvw() << " }\n";
156  } else {
157  indent(out, indent_level)
158  << "<UV> " << inline_name << "{ " << get_uv() << " }\n";
159  }
160  } else {
161  indent(out, indent_level) << "<UV> " << inline_name << "{\n";
162  if (has_w()) {
163  indent(out, indent_level+2) << get_uvw() << "\n";
164  } else {
165  indent(out, indent_level+2) << get_uv() << "\n";
166  }
167  if (has_tangent()) {
168  indent(out, indent_level + 2)
169  << "<Tangent> { " << get_tangent() << " }\n";
170  }
171  if (has_binormal()) {
172  indent(out, indent_level + 2)
173  << "<Binormal> { " << get_binormal() << " }\n";
174  }
175  _duvs.write(out, indent_level + 2, "<Duv>", get_num_dimensions());
176  indent(out, indent_level) << "}\n";
177  }
178 }
179 
180 ////////////////////////////////////////////////////////////////////
181 // Function: EggVertexUV::compare_to
182 // Access: Public
183 // Description: An ordering operator to compare two vertices for
184 // sorting order. This imposes an arbitrary ordering
185 // useful to identify unique vertices.
186 ////////////////////////////////////////////////////////////////////
187 int EggVertexUV::
188 compare_to(const EggVertexUV &other) const {
189  if (_flags != other._flags) {
190  return _flags - other._flags;
191  }
192  int compare;
193  compare = _uvw.compare_to(other._uvw, egg_parameters->_uv_threshold);
194  if (compare != 0) {
195  return compare;
196  }
197 
198  if (has_tangent()) {
199  compare = _tangent.compare_to(other._tangent, egg_parameters->_normal_threshold);
200  if (compare != 0) {
201  return compare;
202  }
203  }
204 
205  if (has_binormal()) {
206  compare = _binormal.compare_to(other._binormal, egg_parameters->_normal_threshold);
207  if (compare != 0) {
208  return compare;
209  }
210  }
211 
212  if (_duvs != other._duvs) {
213  return _duvs < other._duvs ? -1 : 1;
214  }
215 
216  return 0;
217 }
The set of UV&#39;s that may or may not be assigned to a vertex.
Definition: eggVertexUV.h:32
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
const LTexCoord3d & get_uvw() const
Returns the texture coordinate triple, if get_num_dimensions() is 3.
Definition: eggVertexUV.I:88
LTexCoordd get_uv() const
Returns the texture coordinate pair, if get_num_dimensions() is 2.
Definition: eggVertexUV.I:74
void clear_name()
Resets the Namable&#39;s name to empty.
Definition: namable.I:64
This is a two-component point in space.
Definition: lpoint2.h:424
int get_num_dimensions() const
Returns the number of components of the texture coordinate set.
Definition: eggVertexUV.I:51
bool normalize()
Normalizes the vector in place.
Definition: lvecBase3.h:2149
bool has_w() const
Returns true if the texture coordinate has a third, w component, false if it is just a normal 2-d tex...
Definition: eggVertexUV.I:63
int compare_to(const EggVertexUV &other) const
An ordering operator to compare two vertices for sorting order.
This is a fairly low-level base class–any egg object that has a name.
void transform(const LMatrix4d &mat)
Applies the indicated transformation matrix to the UV&#39;s tangent and/or binormal.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:544
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:2306
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85