Panda3D
eggVertexUV.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 eggVertexUV.cxx
10  * @author drose
11  * @date 2004-07-20
12  */
13 
14 #include "eggVertexUV.h"
15 #include "eggParameters.h"
16 
17 #include "indent.h"
18 
19 TypeHandle EggVertexUV::_type_handle;
20 
21 /**
22  *
23  */
24 EggVertexUV::
25 EggVertexUV(const std::string &name, const LTexCoordd &uv) :
26  EggNamedObject(name),
27  _flags(0),
28  _uvw(uv[0], uv[1], 0.0)
29 {
30  if (get_name() == "default") {
31  clear_name();
32  }
33 }
34 
35 /**
36  *
37  */
38 EggVertexUV::
39 EggVertexUV(const std::string &name, const LTexCoord3d &uvw) :
40  EggNamedObject(name),
41  _flags(F_has_w),
42  _uvw(uvw)
43 {
44  if (get_name() == "default") {
45  clear_name();
46  }
47 }
48 
49 /**
50  *
51  */
52 EggVertexUV::
53 EggVertexUV(const EggVertexUV &copy) :
54  EggNamedObject(copy),
55  _duvs(copy._duvs),
56  _flags(copy._flags),
57  _tangent(copy._tangent),
58  _binormal(copy._binormal),
59  _uvw(copy._uvw)
60 {
61 }
62 
63 /**
64  *
65  */
66 EggVertexUV &EggVertexUV::
67 operator = (const EggVertexUV &copy) {
68  EggNamedObject::operator = (copy);
69  _duvs = copy._duvs;
70  _flags = copy._flags;
71  _tangent = copy._tangent;
72  _binormal = copy._binormal;
73  _uvw = copy._uvw;
74 
75  return (*this);
76 }
77 
78 /**
79  *
80  */
81 EggVertexUV::
82 ~EggVertexUV() {
83 }
84 
85 /**
86  * Creates a new EggVertexUV that contains the averaged values of the two
87  * given objects. It is an error if they don't have the same name.
88  */
89 PT(EggVertexUV) EggVertexUV::
90 make_average(const EggVertexUV *first, const EggVertexUV *second) {
91  nassertr(first->get_name() == second->get_name(), nullptr);
92  int flags = first->_flags & second->_flags;
93 
94  LTexCoord3d uvw = (first->_uvw + second->_uvw) / 2;
95 
96  PT(EggVertexUV) new_obj = new EggVertexUV(first->get_name(), uvw);
97  new_obj->_flags = flags;
98  new_obj->_tangent = (first->_tangent + second->_tangent) / 2;
99  new_obj->_binormal = (first->_binormal + second->_binormal) / 2;
100 
101  // Normalize because we're polite.
102  new_obj->_tangent.normalize();
103  new_obj->_binormal.normalize();
104  return new_obj;
105 }
106 
107 /**
108  * Applies the indicated transformation matrix to the UV's tangent and/or
109  * binormal. This does nothing if there is no tangent or binormal.
110  */
111 void EggVertexUV::
112 transform(const LMatrix4d &mat) {
113  if (has_tangent()) {
114  _tangent = _tangent * mat;
115  _tangent.normalize();
116  }
117  if (has_binormal()) {
118  _binormal = _binormal * mat;
119  _binormal.normalize();
120  }
121 }
122 
123 /**
124  *
125  */
126 void EggVertexUV::
127 write(std::ostream &out, int indent_level) const {
128  std::string inline_name = get_name();
129  if (!inline_name.empty()) {
130  inline_name += ' ';
131  }
132 
133  if (_duvs.empty() && (_flags & ~F_has_w) == 0) {
134  if (has_w()) {
135  indent(out, indent_level)
136  << "<UV> " << inline_name << "{ " << get_uvw() << " }\n";
137  } else {
138  indent(out, indent_level)
139  << "<UV> " << inline_name << "{ " << get_uv() << " }\n";
140  }
141  } else {
142  indent(out, indent_level) << "<UV> " << inline_name << "{\n";
143  if (has_w()) {
144  indent(out, indent_level+2) << get_uvw() << "\n";
145  } else {
146  indent(out, indent_level+2) << get_uv() << "\n";
147  }
148  if (has_tangent()) {
149  indent(out, indent_level + 2)
150  << "<Tangent> { " << get_tangent() << " }\n";
151  }
152  if (has_binormal()) {
153  indent(out, indent_level + 2)
154  << "<Binormal> { " << get_binormal() << " }\n";
155  }
156  _duvs.write(out, indent_level + 2, "<Duv>", get_num_dimensions());
157  indent(out, indent_level) << "}\n";
158  }
159 }
160 
161 /**
162  * An ordering operator to compare two vertices for sorting order. This
163  * imposes an arbitrary ordering useful to identify unique vertices.
164  */
165 int EggVertexUV::
166 compare_to(const EggVertexUV &other) const {
167  if (_flags != other._flags) {
168  return _flags - other._flags;
169  }
170  int compare;
171  compare = _uvw.compare_to(other._uvw, egg_parameters->_uv_threshold);
172  if (compare != 0) {
173  return compare;
174  }
175 
176  if (has_tangent()) {
177  compare = _tangent.compare_to(other._tangent, egg_parameters->_normal_threshold);
178  if (compare != 0) {
179  return compare;
180  }
181  }
182 
183  if (has_binormal()) {
184  compare = _binormal.compare_to(other._binormal, egg_parameters->_normal_threshold);
185  if (compare != 0) {
186  return compare;
187  }
188  }
189 
190  if (_duvs != other._duvs) {
191  return _duvs < other._duvs ? -1 : 1;
192  }
193 
194  return 0;
195 }
The set of UV's that may or may not be assigned to a vertex.
Definition: eggVertexUV.h:29
const LTexCoord3d & get_uvw() const
Returns the texture coordinate triple, if get_num_dimensions() is 3.
Definition: eggVertexUV.I:68
LTexCoordd get_uv() const
Returns the texture coordinate pair, if get_num_dimensions() is 2.
Definition: eggVertexUV.I:57
void clear_name()
Resets the Namable's name to empty.
Definition: namable.I:35
PT(EggVertexUV) EggVertexUV
Creates a new EggVertexUV that contains the averaged values of the two given objects.
Definition: eggVertexUV.cxx:89
int get_num_dimensions() const
Returns the number of components of the texture coordinate set.
Definition: eggVertexUV.I:40
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:49
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
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's tangent and/or binormal.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.