-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_set.cpp
464 lines (396 loc) · 12 KB
/
key_set.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include<vector>
#include<iostream>
#include<fstream>
#include <string.h>
#include<string>
#include <GL/glut.h>
#include <math.h>
#include "key_set.h"
#include "quat.h"
#include "model.h"
#define BONE_DIST .75 //distance from bone that points are influenced by bone's movement
using namespace std;
/*
void key_set::animate() {
model *dragon = new model((const char *)"dragon.obj");
key_set *frame = new key_set(dragon);
frame->load_skeleton("skeleton");
frame->pre_compute_quat();
//for(int i = 0;i<=3000;i+=50) {
glPushMatrix();
frame->animate(timer,false);
timer += 50;
skin->draw_model(false,false);
glPopMatrix();
//}
}
*/
key_set::key_set(model * m):cur(NULL), cframe(0), skeleton(NULL), frame_time(0), skin(m)
{
for(unsigned i = 0;i<m->points.size();i++){
point * p = new point;
p->x = m->points[i].x;
p->y = m->points[i].y;
p->z = m->points[i].z;
orig_skin.push_back(*p);
}
}
//add bone: if parent is NULL, a new skeleton is added to the
//keyframe vector
bone * key_set::add_bone(bone * parent, point begin, double length, double anglex,
double angley, double anglez, string name){
bone * t;
//if there is no parent then create a new root
if(parent == NULL){
if(!(parent = new bone)) return NULL;
skeleton = parent;
parent->parent = NULL;
cur = parent;
}else{
if(!(t = new bone)) return NULL;
t->parent = parent;
parent->child.push_back(t);
parent = t;
}
parent->begin.x = begin.x;
parent->begin.y = begin.y;
parent->begin.z = begin.z;
parent->anglex = anglex;
parent->angley = angley;
parent->anglez = anglez;
parent->curx = anglex;
parent->cury = angley;
parent->curz = anglez;
parent->length = length;
parent->anim = false;
parent->done = true;
parent->dist = BONE_DIST;
parent->points.clear();//clear content of vector points
parent->orig_points.clear();
parent->name = name;
return parent;
}
void key_set::BoneListNames(bone *root,char names[MAX_BONECOUNT][20]) {
int i,present;
if (!root) return;
//check if this name is already in the list
present = 0;
for (i=0;(i < MAX_BONECOUNT) && (names[i][0] != '\0');i++)
if (! root->name.compare((const char*) names[i])) {
present = 1;
break;
}
//if not present and if there is space in list
if (! present && (i < MAX_BONECOUNT)) {
strcpy(names[i],root->name.c_str());
//names[i].copy(root->name,root->name.size());
if(i + 1 < MAX_BONECOUNT)
names[i+1][0] = '\0';
}
//fill the list with subtree's names
for (i=0;i<root->child.size();i++)
BoneListNames(root->child[i],names);
}
void key_set::draw_skeleton(){
//diable lighting for skeleton
glDisable(GL_LIGHTING);
//set id's to starting num, skin->points.size() so it doesnt interfer with spheres
id_count = skin->points.size();//sum of points in model mesh
traverse_skeleton(skeleton->begin, skeleton);
glEnable(GL_LIGHTING);
}
void key_set::traverse_skeleton(point begin, bone * root){ //recursive code to draw skeleton
//calculate end position
vec end;
end.x = 0.0;
end.y = root->length;
end.z = 0.0;
end = root->q * end; //do rotation based on quaternions
end.x += begin.x;
end.y += begin.y;
end.z += begin.z;
if(root == cur) glColor3f(1.0,0.0,0.0);
else glColor3f(1.0,1.0,1.0);
root->id = id_count;
glBegin(GL_LINES);
glVertex3f(begin.x,begin.y,begin.z);
glVertex3f(end.x,end.y,end.z);
glEnd();
glPushMatrix();
glTranslatef(end.x,end.y,end.z);
glutSolidSphere(0.1, 5, 5);
glPopMatrix();
point pend; //convert from vec to point
pend.x = end.x;
pend.y = end.y;
pend.z = end.z;
id_count++;
for(unsigned int i=0;i<root->child.size();i++)
traverse_skeleton(pend, root->child[i]);
}
//recursively move to the next bone in tree order
void key_set::change_bone(){
if(cur->child.size()>0){
cur = cur->child[0];
cur->done = true;
}else{
cur = get_next_child(cur);
}
//debug:
if(cur == NULL) cur = skeleton;
cout << "bone: " << cur->name << endl;
cout << "x: " << cur->start.x << endl;
cout << "y: " << cur->start.y << endl;
cout << "z: " << cur->start.z << endl;
if (cur->parent == NULL) cout << " parent = NULL" << endl;
else cout << " parent = " << cur->parent->name << endl;
cout << " children: " << endl;
for(unsigned int i=0;i<cur->child.size();i++){
cout << " child " << i << " " << cur->child[i]->name << endl;
}
}
//find neighbor child on the same level which next to cur
bone * key_set::get_next_child(bone * cur){
if(cur->done == true){
bone * tmp = cur->parent;
for(unsigned int i=0;i<tmp->child.size();i++){
if(tmp->child[i] == cur){
if(i == tmp->child.size()-1) //if last child
cur = get_next_child(cur->parent);
else{
cur = tmp->child[i+1];
cur->done = true;
}
break;
}
}
}
return cur;
}
bone * key_set::BoneFindByName(bone * root, string name){
if(root == NULL) return NULL;
if(root->name.compare(name) == 0){ //if names are the same
return root;
}else{
int count = root->child.size();
bone * tmp;
for(int i = 0;i<count;i++){
tmp = BoneFindByName(root->child[i], name);
if(tmp!=NULL) return tmp;
}
return NULL;
}
}
//loads a skeleton
void key_set::load_skeleton(const char * title){
double anglex, angley, anglez, len;
uint32_t time;
ifstream file (title);
if (file.is_open()){
while (file.good()){
string parent;
bone tmp;
file >> tmp.name;
file >> tmp.begin.x;
file >> tmp.begin.y;
file >> tmp.begin.z;
file >> tmp.anglex;
file >> tmp.angley;
file >> tmp.anglez;
file >> tmp.length;
if(!file.good()) break;
file >> parent;
tmp.parent = BoneFindByName(skeleton, parent);
bone * b = add_bone(tmp.parent, tmp.begin, tmp.length, tmp.anglex, tmp.angley,
tmp.anglez, tmp.name);
//now load in bone points
int p;
file >> p;
for(int i=0;i<p;i++){
int t;
file >> t;
b->points.push_back(&skin->points[t]);
b->orig_points.push_back(t);
}
for(int j=0;j<4;j++){
//load keyframe data recording each bone from 0 to 3000 4 frames in total per bone
file >> anglex;
file >> angley;
file >> anglez;
file >> len;
file >> time;
//update keyframes array
if(keyframes.size() == 0 || time > keyframes.back())//.back() return the last element
keyframes.push_back(time);
keyframe * k = new keyframe;
k->anglex = anglex;
k->angley = angley;
k->anglez = anglez;
k->length = len;
k->time = time;
b->keyframes.push_back(k);
}
}
file.close();
}
else cout << "unable to open file" << endl;
skeleton->done = false;
}
void key_set::next_frame(){
cframe++;
if (cframe >= keyframes.size()) cframe = 0;
cout << "keyframe switched to " << cframe << endl;
}
void key_set::animate(int t, bool draw){
//compute which keyframes to interpolate between
glDisable(GL_LIGHTING);
int prev, next;
double u;
if(keyframes.size() < 2) {
cerr << "need at least two keyframes to animate" << endl;
return;
}
//cout << "t = " << t << "keyframes.back() = " << keyframes.back() << endl;
if (t > keyframes.back()) t = (t%keyframes.back()); //repeat animation if it gets to end
for(unsigned i=1;i<keyframes.size();i++){
if(t <= keyframes[i]) {
prev = i-1;
next = i;
break;
}
}
u = t-keyframes[prev];
u = u/(keyframes[next]-keyframes[prev]);
//create a identity quaternion to start with
quat q(0,0,0,1);
animate_rec(q, skeleton->begin, skeleton, prev, next, u, draw);
glEnable(GL_LIGHTING);
}
void key_set::set_cur_to_keyframe(){
set_cur_to_keyframe_rec(skeleton);
pre_compute_quat_rec(quat(0,0,0,1), skeleton->begin, skeleton, false, 0);
}
//interpolate keyframes into angle x,y,z recursively
void key_set::set_cur_to_keyframe_rec(bone * root){
root->curx = root->keyframes[cframe]->anglex;
root->cury = root->keyframes[cframe]->angley;
root->curz = root->keyframes[cframe]->anglez;
for(unsigned i=0;i<root->child.size();i++){
set_cur_to_keyframe_rec(root->child[i]);
}
}
void key_set::animate_rec( //recursive code to animate skeleton //calculate end position
quat last,
point begin,
bone * root,
int prev,
int next,
double u,
bool draw)
{
if(root != NULL) {
vec end;
end.x = 0.0;
end.y = root->length;
end.z = 0.0;
quat r;
//do linear interp
//r = (root->keyframes[prev]->q * (1-u)) + (root->keyframes[next]->q * u);
r.slerp(root->keyframes[prev]->q, root->keyframes[next]->q, u);
end = r * end; //do rotation based on final quaternion
end.x += begin.x;
end.y += begin.y;
end.z += begin.z;
if(draw){
if(root == cur) glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3f(begin.x,begin.y,begin.z);
glVertex3f(end.x,end.y,end.z);
glEnd();
glPushMatrix();
glTranslatef(end.x,end.y,end.z);
glutSolidSphere(0.1, 5, 5);
glPopMatrix();
glColor3f(1.0,1.0,1.0);
}
point pend; //convert vec struct back to point struct
pend.x = end.x;
pend.y = end.y;
pend.z = end.z;
move_points_anim(root, r, begin);
for(unsigned int i=0;i<root->child.size();i++){
animate_rec(r, pend, root->child[i], prev, next, u, draw);
}
}
}
void key_set::move_points_anim(bone * root, quat & q, point & begin){
for(int i=0;i<root->points.size();i++){
vec v;
v.x = orig_skin[root->orig_points[i]].x - root->start.x;
v.y = orig_skin[root->orig_points[i]].y - root->start.y;
v.z = orig_skin[root->orig_points[i]].z - root->start.z;
v = root->o.get_conjugate() * v;
v = q * v; //do rotation based on quaternions
root->points[i]->x = v.x + begin.x;
root->points[i]->y = v.y + begin.y;
root->points[i]->z = v.z + begin.z;
}
}
//compute quaternions before running animation to save time
void key_set::pre_compute_quat(){
//computer for base skeleton
quat q(0,0,0,1);
pre_compute_quat_rec(q, skeleton->begin, skeleton, false, 0);
//compute for each keyframe
for(int i=0;i<keyframes.size();i++){
pre_compute_quat_rec(quat(0,0,0,1), skeleton->begin, skeleton, true, i);
}
}
//recurisve function for pre_compute_quat
//if keyframe, will computer quaternions for keyframe # k
void key_set::pre_compute_quat_rec(quat r, point begin, bone * root, bool keyframe, int k){
if(k > root->keyframes.size() - 1) {
cerr << "error: pre_compute_quat_rec: k not in bounds" << endl;
exit(1);
}
// assign starting position of bone
if(keyframe){
root->keyframes[k]->begin.x = begin.x;
root->keyframes[k]->begin.y = begin.y;
root->keyframes[k]->begin.z = begin.z;
}else{
root->begin.x = begin.x;
root->begin.y = begin.y;
root->begin.z = begin.z;
}
root->start.x = begin.x;
root->start.y = begin.y;
root->start.z = begin.z;
quat q;
if(keyframe){
q.from_euler(root->keyframes[k]->anglex,root->keyframes[k]->angley,
root->keyframes[k]->anglez);
q = r * q;
root->keyframes[k]->q = q;
}else{
q.from_euler(root->curx,root->cury,root->curz);
q = r * q;
root->q = q;
root->o = q;
}
vec end;
end.x = 0.0;
end.y = root->length;
end.z = 0.0;
end = q * end; //do rotation based on quaternions
end.x += begin.x;
end.y += begin.y;
end.z += begin.z;
point pend; //convert vec struct back to point struct
pend.x = end.x;
pend.y = end.y;
pend.z = end.z;
for(unsigned int i=0;i<root->child.size();i++){
pre_compute_quat_rec(q, pend, root->child[i], keyframe, k);
}
}