-
Notifications
You must be signed in to change notification settings - Fork 2
/
MeshNode.cpp
312 lines (259 loc) · 7.95 KB
/
MeshNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "MeshNode.h"
#include "globals.h" // Causes problems if included in header
#include "Mesh.h"
MeshNode::MeshNode() : id(0), parentid(0), facing(false), gl(false), parent(NULL)
{
}
void MeshNode::Transform(const MeshNodePtr& interpnode, const float interpval, VertexPtrvec& verts,
const GraphicMatrix& parentm, const Vector3& campos)
{
if (interpnode.get() == this)
{
TransformNoInt(verts, parentm, campos);
return;
}
Vector3 interprot1 = lerp(interpnode->rot1, rot1, interpval);
Vector3 interprot2 = lerp(interpnode->rot2, rot2, interpval);
Vector3 interptrans = lerp(interpnode->trans, trans, interpval);
m.identity();
if (!facing)
{
m.rotatez(interprot2.z);
m.rotatey(interprot2.y);
m.rotatex(interprot2.x);
m.translate(interptrans);
m.rotatez(interprot1.z);
m.rotatey(interprot1.y);
m.rotatex(interprot1.x);
}
else
{
Vector3 dir;
Vector3 start(0, 0, 1);
Vector3 facerot, currpos;
start.transform3(m);
// Find the current position - note that this duplicates code, but with facing containers
// it is necessary to do this twice so there's not a good way around that
GraphicMatrix facem;
facem.translate(interptrans);
if (parent) facem *= parent->m;
facem *= parentm;
currpos.transform(facem);
dir = campos - currpos;
facerot = RotateBetweenVectors(start, dir);
m.rotatex(facerot.x);
m.rotatey(facerot.y);
m.translate(interptrans);
}
if (parent)
{
m *= parent->m;
}
m *= parentm;
TransformLoop(interpnode, interpval, verts, parentm, campos);
// Recursively call this on our children
size_t csize = children.size();
for (size_t i = 0; i < csize; ++i)
{
children[i]->Transform(interpnode->children[i], interpval, verts, m, campos);
}
}
// Split this off to aid profiling - inline when not profiling
inline
void MeshNode::TransformLoop(const MeshNodePtr& interpnode, const float interpval, VertexPtrvec& verts,
const GraphicMatrix& parentm, const Vector3& campos)
{
size_t vsize = vertices.size();
if (gl)
{
for (size_t i = 0; i < vsize; ++i)
{
Vertex& verti = vertices[i];
Vertex& interpvi = interpnode->vertices[i];
Vertex& currvert = *verts[verti.id];
currvert.norm = lerp(interpvi.norm, verti.norm, interpval);
currvert.tangent = lerp(interpvi.tangent, verti.tangent, interpval);
currvert.pos = lerp(interpvi.pos, verti.pos, interpval);
currvert.pos.transform(m);
currvert.norm.transform3(m);
currvert.tangent.transform3(m);
for (size_t j = 0; j < 4; ++j)
{
currvert.color[j] =
static_cast<unsigned char>(lerp(float(interpvi.color[j]), float(verti.color[j]), interpval));
}
}
}
else
{
for (size_t i = 0; i < vsize; ++i)
{
Vertex& verti = vertices[i];
Vertex& currvert = *verts[verti.id];
currvert.pos = lerp(interpnode->vertices[i].pos, verti.pos, interpval);
currvert.pos.transform(m);
}
}
}
void MeshNode::TransformNoInt(VertexPtrvec& verts, const GraphicMatrix& parentm, const Vector3& campos)
{
m.identity();
if (!facing)
{
m.rotatez(rot2.z);
m.rotatey(rot2.y);
m.rotatex(rot2.x);
m.translate(trans);
m.rotatez(rot1.z);
m.rotatey(rot1.y);
m.rotatex(rot1.x);
}
else
{
Vector3 dir;
Vector3 start(0, 0, 1);
Vector3 facerot, currpos;
start.transform3(m);
// Find the current position - note that this duplicates code, but with facing containers
// it is necessary to do this twice so there's not a good way around that
GraphicMatrix facem;
facem.translate(trans);
if (parent) facem *= parent->m;
facem *= parentm;
currpos.transform(facem);
dir = campos - currpos;
facerot = RotateBetweenVectors(start, dir);
m.rotatex(facerot.x);
m.rotatey(facerot.y);
m.translate(trans);
}
if (parent)
{
m *= parent->m;
}
m *= parentm;
TransformNoIntLoop(verts, parentm, campos);
// Recursively call this on our children
size_t csize = children.size();
for (size_t i = 0; i < csize; ++i)
{
children[i]->TransformNoInt(verts, m, campos);
}
}
// Split this off to aid profiling - inline when not profiling
inline
void MeshNode::TransformNoIntLoop(VertexPtrvec& verts, const GraphicMatrix& parentm, const Vector3& campos)
{
size_t vsize = vertices.size();
if (gl) // Means we need normal and color data, otherwise we don't care
{
for (size_t i = 0; i < vsize; ++i)
{
Vertex& verti = vertices[i];
Vertex& currvert = *verts[verti.id];
currvert.norm = verti.norm;
currvert.pos = verti.pos;
currvert.tangent = verti.tangent;
currvert.pos.transform(m);
currvert.norm.transform3(m);
currvert.tangent.transform3(m);
currvert.color = verti.color;
}
}
else
{
for (size_t i = 0; i < vsize; ++i)
{
Vertex& verti = vertices[i];
Vertex& currvert = *verts[verti.id];
currvert.pos = verti.pos;
currvert.pos.transform(m);
}
}
}
// Currently this function keeps parent the same, even though that may not always be
// what is wanted. It causes problems if we don't do this, however, because this gets
// called when inserting a mesh into an STL container, which is a fairly common
// operation and expected to insert an exact copy, not a copy with this pointer null'd
MeshNodePtr MeshNode::Clone()
{
MeshNodePtr newmn(new MeshNode(*this));
newmn->children.clear();
newmn->parent = parent;
newmn->gl = gl;
newmn->vertices = vertices;
for (size_t i = 0; i < children.size(); ++i)
{
newmn->children.push_back(children[i]->Clone());
}
return newmn;
}
void MeshNode::GetContainers(map<string, MeshNodePtr>& cont, MeshNodePtr& thisptr)
{
cont[name] = thisptr;
for (size_t i = 0; i < children.size(); ++i)
children[i]->GetContainers(cont, children[i]);
}
void MeshNode::Scale(const float& sval)
{
for (size_t i = 0; i < vertices.size(); ++i)
{
vertices[i].pos *= sval;
}
trans *= sval;
for (size_t i = 0; i < children.size(); ++i)
{
children[i]->Scale(sval);
}
}
void MeshNode::ScaleZ(const float& sval)
{
for (size_t i = 0; i < vertices.size(); ++i)
{
vertices[i].pos.z *= sval;
}
trans.z *= sval;
for (size_t i = 0; i < children.size(); ++i)
{
children[i]->ScaleZ(sval);
}
}
void MeshNode::SetGL(const bool dogl)
{
gl = dogl;
for (size_t i = 0; i < children.size(); ++i)
{
children[i]->SetGL(dogl);
}
}
void MeshNode::SetTangent(const size_t id, const Vector3& tan)
{
for (size_t i = 0; i < vertices.size(); ++i)
{
if (vertices[i].id == id)
{
vertices[i].tangent = tan;
return;
}
}
for (size_t i = 0; i < children.size(); ++i)
children[i]->SetTangent(id, tan);
}