Panda3D
Loading...
Searching...
No Matches
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
19TypeHandle EggVertexUV::_type_handle;
20
21/**
22 *
23 */
24EggVertexUV::
25EggVertexUV(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 */
38EggVertexUV::
39EggVertexUV(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 */
52EggVertexUV::
53EggVertexUV(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 */
66EggVertexUV &EggVertexUV::
67operator = (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 */
81EggVertexUV::
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 */
89PT(EggVertexUV) EggVertexUV::
90make_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 */
112transform(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 */
126void EggVertexUV::
127write(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_tangent4()) {
149 indent(out, indent_level + 2)
150 << "<Tangent> { " << get_tangent4() << " }\n";
151 } else if (has_tangent()) {
152 indent(out, indent_level + 2)
153 << "<Tangent> { " << get_tangent() << " }\n";
154 }
155 if (has_binormal()) {
156 indent(out, indent_level + 2)
157 << "<Binormal> { " << get_binormal() << " }\n";
158 }
159 _duvs.write(out, indent_level + 2, "<Duv>", get_num_dimensions());
160 indent(out, indent_level) << "}\n";
161 }
162}
163
164/**
165 * An ordering operator to compare two vertices for sorting order. This
166 * imposes an arbitrary ordering useful to identify unique vertices.
167 */
169compare_to(const EggVertexUV &other) const {
170 if (_flags != other._flags) {
171 return _flags - other._flags;
172 }
173 int compare;
174 compare = _uvw.compare_to(other._uvw, egg_parameters->_uv_threshold);
175 if (compare != 0) {
176 return compare;
177 }
178
179 if (has_tangent()) {
180 compare = _tangent.compare_to(other._tangent, egg_parameters->_normal_threshold);
181 if (compare != 0) {
182 return compare;
183 }
184 }
185
186 if (has_binormal()) {
187 compare = _binormal.compare_to(other._binormal, egg_parameters->_normal_threshold);
188 if (compare != 0) {
189 return compare;
190 }
191 }
192
193 if (_duvs != other._duvs) {
194 return _duvs < other._duvs ? -1 : 1;
195 }
196
197 return 0;
198}
This is a fairly low-level base class–any egg object that has a name.
The set of UV's that may or may not be assigned to a vertex.
Definition eggVertexUV.h:29
void transform(const LMatrix4d &mat)
Applies the indicated transformation matrix to the UV's tangent and/or binormal.
LTexCoordd get_uv() const
Returns the texture coordinate pair, if get_num_dimensions() is 2.
Definition eggVertexUV.I:57
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
int get_num_dimensions() const
Returns the number of components of the texture coordinate set.
Definition eggVertexUV.I:40
const LTexCoord3d & get_uvw() const
Returns the texture coordinate triple, if get_num_dimensions() is 3.
Definition eggVertexUV.I:68
int compare_to(const EggVertexUV &other) const
An ordering operator to compare two vertices for sorting order.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.