]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Skeleton.cpp
Fixed warnings, removed unused attributes, broken audio output setting, and so on
[lugaru.git] / Source / Animation / Skeleton.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "Animation/Skeleton.hpp"
22
23 #include "Animation/Animation.hpp"
24 #include "Audio/openal_wrapper.hpp"
25 #include "Game.hpp"
26 #include "Utils/Folders.hpp"
27 #include "Tutorial.hpp"
28
29 extern float multiplier;
30 extern float gravity;
31 extern Terrain terrain;
32 extern int environment;
33 extern float camerashake;
34 extern bool freeze;
35 extern int detail;
36
37 extern int whichjointstartarray[26];
38 extern int whichjointendarray[26];
39
40 Skeleton::Skeleton() :
41     selected(0),
42     id(0),
43     num_models(0),
44     clothes(false),
45     spinny(false),
46     skinsize(0),
47     checkdelay(0),
48     longdead(0),
49     broken(false),
50     free(0),
51     oldfree(0),
52     freetime(0),
53     freefall(false)
54 {
55     memset(forwardjoints, 0, sizeof(forwardjoints));
56     memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
57     memset(jointlabels, 0, sizeof(jointlabels));
58     memset(skinText, 0, sizeof(skinText));
59 }
60
61 /* EFFECT
62  * sets forward, lowforward, specialforward[]
63  *
64  * USES:
65  * Skeleton::Load
66  * Person/Person::DoAnimations
67  * Person/Person::DrawSkeleton
68  */
69 void Skeleton::FindForwards()
70 {
71     //Find forward vectors
72     CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
73     Normalise(&forward);
74
75     CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
76     Normalise(&lowforward);
77
78     //Special forwards
79     specialforward[0] = forward;
80
81     specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
82     specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
83     specialforward[1] += forward * .4;
84     Normalise(&specialforward[1]);
85     specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
86     specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
87     specialforward[2] += forward * .4;
88     Normalise(&specialforward[2]);
89
90     specialforward[3] = jointPos(righthip) + jointPos(rightankle);
91     specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
92     specialforward[3] += lowforward * .4;
93     Normalise(&specialforward[3]);
94     specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
95     specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
96     specialforward[4] += lowforward * .4;
97     Normalise(&specialforward[4]);
98 }
99
100 /* EFFECT
101  * TODO
102  *
103  * USES:
104  * Person/Person::RagDoll
105  * Person/Person::DoStuff
106  * Person/IKHelper
107  */
108 float Skeleton::DoConstraints(XYZ *coords, float *scale)
109 {
110     const float elasticity = .3;
111     XYZ bounceness;
112     const int numrepeats = 3;
113     float groundlevel = .15;
114     int m;
115     unsigned i;
116     XYZ temp;
117     XYZ terrainnormal;
118     int whichhit;
119     float frictionness;
120     XYZ terrainlight;
121     int whichpatchx;
122     int whichpatchz;
123     float damage = 0; // eventually returned from function
124     bool breaking = false;
125
126     if (free) {
127         freetime += multiplier;
128
129         whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
130         whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
131
132         terrainlight = *coords;
133         Object::SphereCheckPossible(&terrainlight, 1);
134
135         //Add velocity
136         for (i = 0; i < joints.size(); i++) {
137             joints[i].position = joints[i].position + joints[i].velocity * multiplier;
138
139             switch (joints[i].label) {
140             case head:
141                 groundlevel = .8;
142                 break;
143             case righthand:
144             case rightwrist:
145             case rightelbow:
146             case lefthand:
147             case leftwrist:
148             case leftelbow:
149                 groundlevel = .2;
150                 break;
151             default:
152                 groundlevel = .15;
153                 break;
154             }
155
156             joints[i].position.y -= groundlevel;
157             joints[i].oldvelocity = joints[i].velocity;
158         }
159
160         float tempmult = multiplier;
161         //multiplier/=numrepeats;
162
163         for (int j = 0; j < numrepeats; j++) {
164             float r = .05;
165             // right leg constraints?
166             if (!joint(rightknee).locked && !joint(righthip).locked) {
167                 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
168                 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
169                     jointPos(rightknee) -= lowforward * .05;
170                     if (spinny)
171                         jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
172                     else
173                         jointVel(rightknee) -= lowforward * .05;
174                     jointPos(rightankle) += lowforward * .025;
175                     if (spinny)
176                         jointVel(rightankle) += lowforward * .025 / multiplier / 4;
177                     else
178                         jointVel(rightankle) += lowforward * .25;
179                     jointPos(righthip) += lowforward * .025;
180                     if (spinny)
181                         jointVel(righthip) += lowforward * .025 / multiplier / 4;
182                     else
183                         jointVel(righthip) += lowforward * .025;
184                     temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
185                 }
186             }
187
188             // left leg constraints?
189             if (!joint(leftknee).locked && !joint(lefthip).locked) {
190                 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
191                 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
192                     jointPos(leftknee) -= lowforward * .05;
193                     if (spinny)
194                         jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
195                     else
196                         jointVel(leftknee) -= lowforward * .05;
197                     jointPos(leftankle) += lowforward * .025;
198                     if (spinny)
199                         jointVel(leftankle) += lowforward * .025 / multiplier / 4;
200                     else
201                         jointVel(leftankle) += lowforward * .25;
202                     jointPos(lefthip) += lowforward * .025;
203                     if (spinny)
204                         jointVel(lefthip) += lowforward * .025 / multiplier / 4;
205                     else
206                         jointVel(lefthip) += lowforward * .025;
207                     temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
208                 }
209             }
210
211             for (i = 0; i < joints.size(); i++) {
212                 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
213                     joints[i].locked = 0;
214                 if (spinny && findLengthfast(&joints[i].velocity) > 600)
215                     joints[i].locked = 0;
216                 if (joints[i].delay > 0) {
217                     bool freely = true;
218                     for (unsigned j = 0; j < joints.size(); j++) {
219                         if (joints[j].locked)
220                             freely = false;
221                     }
222                     if (freely)
223                         joints[i].delay -= multiplier * 3;
224                 }
225             }
226
227             for (i = 0; i < muscles.size(); i++) {
228                 //Length constraints
229                 muscles[i].DoConstraint(spinny);
230             }
231
232             float friction;
233             for (i = 0; i < joints.size(); i++) {
234                 //Length constraints
235                 //Ground constraint
236                 groundlevel = 0;
237                 if (joints[i].position.y * (*scale) + coords->y < terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel) {
238                     freefall = 0;
239                     friction = 1.5;
240                     if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
241                         joints[i].locked = 1;
242                         joints[i].delay = 1;
243                         if (!Tutorial::active || id == 0) {
244                             emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
245                         }
246                         breaking = true;
247                     }
248
249                     if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
250                         joints[i].locked = 1;
251                         joints[i].delay = 1;
252                         if (!Tutorial::active || id == 0) {
253                             emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
254                         }
255                     }
256
257                     terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
258                     ReflectVector(&joints[i].velocity, &terrainnormal);
259                     bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
260                     if (!joints[i].locked)
261                         damage += findLengthfast(&bounceness) / 4000;
262                     if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
263                         bounceness = 0;
264                     frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
265                     joints[i].velocity -= bounceness;
266                     if (1 - friction * frictionness > 0)
267                         joints[i].velocity *= 1 - friction * frictionness;
268                     else
269                         joints[i].velocity = 0;
270
271                     if (!Tutorial::active || id == 0)
272                         if (findLengthfast(&bounceness) > 8000 && breaking) {
273                             // FIXME: this crashes because k is not initialized!
274                             // to reproduce, type 'wolfie' in console and play a while
275                             // I'll just comment it out for now
276                             //Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(temp - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .4, .5, Random() % 360);
277                             Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
278                             breaking = false;
279                             camerashake += .6;
280
281                             emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
282
283                             addEnvSound(*coords, 64);
284                         }
285
286                     if (findLengthfast(&bounceness) > 2500) {
287                         Normalise(&bounceness);
288                         bounceness = bounceness * 50;
289                     }
290
291                     joints[i].velocity += bounceness * elasticity;
292
293                     if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
294                         bounceness = 0;
295                         joints[i].velocity = joints[i].oldvelocity;
296                     }
297
298
299                     if (joints[i].locked == 0)
300                         if (findLengthfast(&joints[i].velocity) < 1)
301                             joints[i].locked = 1;
302
303                     if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
304                         terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
305                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
306                         if (detail == 2)
307                             terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
308                     } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
309                         terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
310                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x * 190 / 255, terrainlight.y * 170 / 255, terrainlight.z * 108 / 255, .5, .7);
311                     }
312
313                     else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
314                         terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
315                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x * 90 / 255, terrainlight.y * 70 / 255, terrainlight.z * 8 / 255, .5, .5);
316                     } else if (findLengthfast(&bounceness) > 500)
317                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
318
319
320                     joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
321                     if (longdead > 100)
322                         broken = 1;
323                 }
324                 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
325                     for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
326                         unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m];
327                         if (k < Object::objects.size()) {
328                             if (Object::objects[k]->possible) {
329                                 friction = Object::objects[k]->friction;
330                                 XYZ start = joints[i].realoldposition;
331                                 XYZ end = joints[i].position * (*scale) + *coords;
332                                 whichhit = Object::objects[k]->model.LineCheckPossible(&start, &end, &temp, &Object::objects[k]->position, &Object::objects[k]->yaw);
333                                 if (whichhit != -1) {
334                                     if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
335                                         joints[i].locked = 1;
336                                         joints[i].delay = 1;
337                                         if (!Tutorial::active || id == 0) {
338                                             emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
339                                         }
340                                         breaking = true;
341                                     }
342
343                                     if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
344                                         joints[i].locked = 1;
345                                         joints[i].delay = 1;
346                                         if (!Tutorial::active || id == 0) {
347                                             emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
348                                         }
349                                     }
350
351                                     terrainnormal = DoRotation(Object::objects[k]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0) * -1;
352                                     if (terrainnormal.y > .8)
353                                         freefall = 0;
354                                     bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
355                                     if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
356                                         bounceness = 0;
357                                         joints[i].velocity = joints[i].oldvelocity;
358                                     }
359                                     if (!Tutorial::active || id == 0)
360                                         if (findLengthfast(&bounceness) > 4000 && breaking) {
361                                             Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(temp - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .4, .5, Random() % 360);
362                                             Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
363                                             breaking = false;
364                                             camerashake += .6;
365
366                                             emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
367
368                                             addEnvSound(*coords, 64);
369                                         }
370                                     if (Object::objects[k]->type == treetrunktype) {
371                                         Object::objects[k]->rotx += joints[i].velocity.x * multiplier * .4;
372                                         Object::objects[k]->roty += joints[i].velocity.z * multiplier * .4;
373                                         Object::objects[k + 1]->rotx += joints[i].velocity.x * multiplier * .4;
374                                         Object::objects[k + 1]->roty += joints[i].velocity.z * multiplier * .4;
375                                     }
376                                     if (!joints[i].locked)
377                                         damage += findLengthfast(&bounceness) / 2500;
378                                     ReflectVector(&joints[i].velocity, &terrainnormal);
379                                     frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
380                                     joints[i].velocity -= bounceness;
381                                     if (1 - friction * frictionness > 0)
382                                         joints[i].velocity *= 1 - friction * frictionness;
383                                     else
384                                         joints[i].velocity = 0;
385                                     if (findLengthfast(&bounceness) > 2500) {
386                                         Normalise(&bounceness);
387                                         bounceness = bounceness * 50;
388                                     }
389                                     joints[i].velocity += bounceness * elasticity;
390
391
392                                     if (!joints[i].locked)
393                                         if (findLengthfast(&joints[i].velocity) < 1) {
394                                             joints[i].locked = 1;
395                                         }
396                                     if (findLengthfast(&bounceness) > 500)
397                                         Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
398                                     joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
399                                     if (longdead > 100)
400                                         broken = 1;
401                                 }
402                             }
403                         }
404                     }
405                 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
406             }
407         }
408         multiplier = tempmult;
409
410
411         if (terrain.patchobjectnum[whichpatchx][whichpatchz])
412             for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
413                 unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m];
414                 if (Object::objects[k]->possible) {
415                     for (i = 0; i < 26; i++) {
416                         //Make this less stupid
417                         XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
418                         XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
419                         whichhit = Object::objects[k]->model.LineCheckSlidePossible(&start, &end, &Object::objects[k]->position, &Object::objects[k]->yaw);
420                         if (whichhit != -1) {
421                             joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
422                             for (unsigned j = 0; j < muscles.size(); j++) {
423                                 if ((muscles[j].parent1->label == whichjointstartarray[i] && muscles[j].parent2->label == whichjointendarray[i]) || (muscles[j].parent2->label == whichjointstartarray[i] && muscles[j].parent1->label == whichjointendarray[i]))
424                                     muscles[j].DoConstraint(spinny);
425                             }
426                         }
427                     }
428                 }
429             }
430
431         for (i = 0; i < joints.size(); i++) {
432             switch (joints[i].label) {
433             case head:
434                 groundlevel = .8;
435                 break;
436             case righthand:
437             case rightwrist:
438             case rightelbow:
439             case lefthand:
440             case leftwrist:
441             case leftelbow:
442                 groundlevel = .2;
443                 break;
444             default:
445                 groundlevel = .15;
446                 break;
447             }
448             joints[i].position.y += groundlevel;
449             joints[i].mass = 1;
450             if (joints[i].label == lefthip || joints[i].label == leftknee || joints[i].label == leftankle || joints[i].label == righthip || joints[i].label == rightknee || joints[i].label == rightankle)
451                 joints[i].mass = 2;
452             if (joints[i].locked) {
453                 joints[i].mass = 4;
454             }
455         }
456
457         return damage;
458     }
459
460     if (!free) {
461         for (i = 0; i < muscles.size(); i++) {
462             if (muscles[i].type == boneconnect)
463                 muscles[i].DoConstraint(0);
464         }
465     }
466
467     return 0;
468 }
469
470 /* EFFECT
471  * applies gravity to the skeleton
472  *
473  * USES:
474  * Person/Person::DoStuff
475  */
476 void Skeleton::DoGravity(float *scale)
477 {
478     for (unsigned i = 0; i < joints.size(); i++) {
479         if (
480                 (
481                     ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
482                     (lowforward.y > -.1) ||
483                     (joints[i].mass < 5)
484                 ) && (
485                     ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
486                     (forward.y < .3)
487                 )
488             ) {
489             joints[i].velocity.y += gravity * multiplier / (*scale);
490         }
491     }
492 }
493
494 /* EFFECT
495  * set muscles[which].rotate1
496  *     .rotate2
497  *     .rotate3
498  *
499  * special case if animation == hanganim
500  */
501 void Skeleton::FindRotationMuscle(int which, int animation)
502 {
503     XYZ p1, p2, fwd;
504     float dist;
505
506     p1 = muscles[which].parent1->position;
507     p2 = muscles[which].parent2->position;
508     dist = findDistance(&p1, &p2);
509     if (p1.y - p2.y <= dist)
510         muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
511     if (p1.y - p2.y > dist)
512         muscles[which].rotate2 = asin(1.f);
513     muscles[which].rotate2 *= 360.0 / 6.2831853;
514
515     p1.y = 0;
516     p2.y = 0;
517     dist = findDistance(&p1, &p2);
518     if (p1.z - p2.z <= dist)
519         muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
520     if (p1.z - p2.z > dist)
521         muscles[which].rotate1 = acos(1.f);
522     muscles[which].rotate1 *= 360.0 / 6.2831853;
523     if (p1.x > p2.x)
524         muscles[which].rotate1 = 360 - muscles[which].rotate1;
525     if (!isnormal(muscles[which].rotate1))
526         muscles[which].rotate1 = 0;
527     if (!isnormal(muscles[which].rotate2))
528         muscles[which].rotate2 = 0;
529
530     const int label1 = muscles[which].parent1->label;
531     const int label2 = muscles[which].parent2->label;
532     switch (label1) {
533     case head:
534         fwd = specialforward[0];
535         break;
536     case rightshoulder:
537     case rightelbow:
538     case rightwrist:
539     case righthand:
540         fwd = specialforward[1];
541         break;
542     case leftshoulder:
543     case leftelbow:
544     case leftwrist:
545     case lefthand:
546         fwd = specialforward[2];
547         break;
548     case righthip:
549     case rightknee:
550     case rightankle:
551     case rightfoot:
552         fwd = specialforward[3];
553         break;
554     case lefthip:
555     case leftknee:
556     case leftankle:
557     case leftfoot:
558         fwd = specialforward[4];
559         break;
560     default:
561         if (muscles[which].parent1->lower)
562             fwd = lowforward;
563         else
564             fwd = forward;
565         break;
566     }
567
568     if (animation == hanganim) {
569         if (label1 == righthand || label2 == righthand) {
570             fwd = 0;
571             fwd.x = -1;
572         }
573         if (label1 == lefthand || label2 == lefthand) {
574             fwd = 0;
575             fwd.x = 1;
576         }
577     }
578
579     if (free == 0) {
580         if (label1 == rightfoot || label2 == rightfoot) {
581             fwd.y -= .3;
582         }
583         if (label1 == leftfoot || label2 == leftfoot) {
584             fwd.y -= .3;
585         }
586     }
587
588     fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
589     fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
590     fwd.y = 0;
591     fwd /= findLength(&fwd);
592     if (fwd.z <= 1 && fwd.z >= -1)
593         muscles[which].rotate3 = acos(0 - fwd.z);
594     else
595         muscles[which].rotate3 = acos(-1.f);
596     muscles[which].rotate3 *= 360.0 / 6.2831853;
597     if (0 > fwd.x)
598         muscles[which].rotate3 = 360 - muscles[which].rotate3;
599     if (!isnormal(muscles[which].rotate3))
600         muscles[which].rotate3 = 0;
601 }
602
603 /* EFFECT
604  * load skeleton
605  * takes filenames for three skeleton files and various models
606  */
607 void Skeleton::Load(const std::string& filename,       const std::string& lowfilename, const std::string& clothesfilename,
608                     const std::string& modelfilename,  const std::string& model2filename,
609                     const std::string& model3filename, const std::string& model4filename,
610                     const std::string& model5filename, const std::string& model6filename,
611                     const std::string& model7filename, const std::string& modellowfilename,
612                     const std::string& modelclothesfilename, bool clothes)
613 {
614     GLfloat M[16];
615     FILE *tfile;
616     float lSize;
617     int j, num_joints, num_muscles;
618
619     LOGFUNC;
620
621     num_models = 7;
622
623     // load various models
624     // rotate, scale, do normals, do texcoords for each as needed
625
626     model[0].loadnotex(modelfilename);
627     model[1].loadnotex(model2filename);
628     model[2].loadnotex(model3filename);
629     model[3].loadnotex(model4filename);
630     model[4].loadnotex(model5filename);
631     model[5].loadnotex(model6filename);
632     model[6].loadnotex(model7filename);
633
634     for (int i = 0; i < num_models; i++) {
635         model[i].Rotate(180, 0, 0);
636         model[i].Scale(.04, .04, .04);
637         model[i].CalculateNormals(0);
638     }
639
640     drawmodel.load(modelfilename);
641     drawmodel.Rotate(180, 0, 0);
642     drawmodel.Scale(.04, .04, .04);
643     drawmodel.FlipTexCoords();
644     if ((Tutorial::active) && (id != 0)) {
645         drawmodel.UniformTexCoords();
646         drawmodel.ScaleTexCoords(0.1);
647     }
648     drawmodel.CalculateNormals(0);
649
650     modellow.loadnotex(modellowfilename);
651     modellow.Rotate(180, 0, 0);
652     modellow.Scale(.04, .04, .04);
653     modellow.CalculateNormals(0);
654
655     drawmodellow.load(modellowfilename);
656     drawmodellow.Rotate(180, 0, 0);
657     drawmodellow.Scale(.04, .04, .04);
658     drawmodellow.FlipTexCoords();
659     if (Tutorial::active && id != 0)
660         drawmodellow.UniformTexCoords();
661     if (Tutorial::active && id != 0)
662         drawmodellow.ScaleTexCoords(0.1);
663     drawmodellow.CalculateNormals(0);
664
665     if (clothes) {
666         modelclothes.loadnotex(modelclothesfilename);
667         modelclothes.Rotate(180, 0, 0);
668         modelclothes.Scale(.041, .04, .041);
669         modelclothes.CalculateNormals(0);
670
671         drawmodelclothes.load(modelclothesfilename);
672         drawmodelclothes.Rotate(180, 0, 0);
673         drawmodelclothes.Scale(.04, .04, .04);
674         drawmodelclothes.FlipTexCoords();
675         drawmodelclothes.CalculateNormals(0);
676     }
677
678     // FIXME: three similar blocks follow, one for each of:
679     // filename, lowfilename, clothesfilename
680
681     // load skeleton
682
683     tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
684
685     // read num_joints
686     funpackf(tfile, "Bi", &num_joints);
687
688     joints.resize(num_joints);
689
690     // read info for each joint
691     for (int i = 0; i < num_joints; i++) {
692         joints[i].load(tfile, joints);
693     }
694
695     // read num_muscles
696     funpackf(tfile, "Bi", &num_muscles);
697
698     // allocate memory
699     muscles.resize(num_muscles);
700
701     // for each muscle...
702     for (int i = 0; i < num_muscles; i++) {
703         muscles[i].load(tfile, model[0].vertexNum, joints);
704     }
705
706     // read forwardjoints (?)
707     for (j = 0; j < 3; j++) {
708         funpackf(tfile, "Bi", &forwardjoints[j]);
709     }
710     // read lowforwardjoints (?)
711     for (j = 0; j < 3; j++) {
712         funpackf(tfile, "Bi", &lowforwardjoints[j]);
713     }
714
715     // ???
716     for (j = 0; j < num_muscles; j++) {
717         for (unsigned i = 0; i < muscles[j].vertices.size(); i++) {
718             for (int k = 0; k < num_models; k++) {
719                 if (muscles[j].vertices[i] < model[k].vertexNum) {
720                     model[k].owner[muscles[j].vertices[i]] = j;
721                 }
722             }
723         }
724     }
725
726     // calculate some stuff
727     FindForwards();
728     for (int i = 0; i < num_muscles; i++) {
729         FindRotationMuscle(i, -1);
730     }
731     // this seems to use opengl purely for matrix calculations
732     for (int k = 0; k < num_models; k++) {
733         for (int i = 0; i < model[k].vertexNum; i++) {
734             model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
735             glMatrixMode(GL_MODELVIEW);
736             glPushMatrix();
737             glLoadIdentity();
738             glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
739             glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
740             glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
741             glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
742             glGetFloatv(GL_MODELVIEW_MATRIX, M);
743             model[k].vertex[i].x = M[12] * 1;
744             model[k].vertex[i].y = M[13] * 1;
745             model[k].vertex[i].z = M[14] * 1;
746             glPopMatrix();
747         }
748         model[k].CalculateNormals(0);
749     }
750     fclose(tfile);
751
752     // load ???
753
754     tfile = Folders::openMandatoryFile( Folders::getResourcePath(lowfilename), "rb" );
755
756     // skip joints section
757
758     fseek(tfile, sizeof(num_joints), SEEK_CUR);
759     for (int i = 0; i < num_joints; i++) {
760         // skip joint info
761         lSize = sizeof(XYZ)
762                 + sizeof(float)
763                 + sizeof(float)
764                 + 1 //sizeof(bool)
765                 + 1 //sizeof(bool)
766                 + sizeof(int)
767                 + 1 //sizeof(bool)
768                 + 1 //sizeof(bool)
769                 + sizeof(int)
770                 + sizeof(int)
771                 + 1 //sizeof(bool)
772                 + sizeof(int);
773         fseek(tfile, lSize, SEEK_CUR);
774     }
775
776     // skip num_muscles
777     fseek(tfile, sizeof(num_muscles), SEEK_CUR);
778
779     for (int i = 0; i < num_muscles; i++) {
780         // skip muscle info
781         lSize = sizeof(float)
782                 + sizeof(float)
783                 + sizeof(float)
784                 + sizeof(float)
785                 + sizeof(float)
786                 + sizeof(int);
787         fseek(tfile, lSize, SEEK_CUR);
788
789         muscles[i].loadVerticesLow(tfile, modellow.vertexNum);
790
791         // skip more stuff
792         lSize = 1; //sizeof(bool);
793         fseek ( tfile, lSize, SEEK_CUR);
794         lSize = sizeof(int);
795         fseek ( tfile, lSize, SEEK_CUR);
796         fseek ( tfile, lSize, SEEK_CUR);
797     }
798
799     for (j = 0; j < num_muscles; j++) {
800         for (unsigned i = 0; i < muscles[j].verticeslow.size(); i++) {
801             if (muscles[j].verticeslow[i] < modellow.vertexNum) {
802                 modellow.owner[muscles[j].verticeslow[i]] = j;
803             }
804         }
805     }
806
807     // use opengl for its matrix math
808     for (int i = 0; i < modellow.vertexNum; i++) {
809         modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
810         glMatrixMode(GL_MODELVIEW);
811         glPushMatrix();
812         glLoadIdentity();
813         glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
814         glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
815         glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
816         glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
817         glGetFloatv(GL_MODELVIEW_MATRIX, M);
818         modellow.vertex[i].x = M[12];
819         modellow.vertex[i].y = M[13];
820         modellow.vertex[i].z = M[14];
821         glPopMatrix();
822     }
823
824     modellow.CalculateNormals(0);
825
826     // load clothes
827
828     if (clothes) {
829         tfile = Folders::openMandatoryFile( Folders::getResourcePath(clothesfilename), "rb" );
830
831         // skip num_joints
832         fseek(tfile, sizeof(num_joints), SEEK_CUR);
833
834         for (int i = 0; i < num_joints; i++) {
835             // skip joint info
836             lSize = sizeof(XYZ)
837                     + sizeof(float)
838                     + sizeof(float)
839                     + 1 //sizeof(bool)
840                     + 1 //sizeof(bool)
841                     + sizeof(int)
842                     + 1 //sizeof(bool)
843                     + 1 //sizeof(bool)
844                     + sizeof(int)
845                     + sizeof(int)
846                     + 1 //sizeof(bool)
847                     + sizeof(int);
848             fseek(tfile, lSize, SEEK_CUR);
849         }
850
851         // skip num_muscles
852         fseek(tfile, sizeof(num_muscles), SEEK_CUR);
853
854         for (int i = 0; i < num_muscles; i++) {
855             // skip muscle info
856             lSize = sizeof(float)
857                     + sizeof(float)
858                     + sizeof(float)
859                     + sizeof(float)
860                     + sizeof(float)
861                     + sizeof(int);
862             fseek(tfile, lSize, SEEK_CUR);
863
864             muscles[i].loadVerticesClothes(tfile, modelclothes.vertexNum);
865
866             // skip more stuff
867             lSize = 1; //sizeof(bool);
868             fseek ( tfile, lSize, SEEK_CUR);
869             lSize = sizeof(int);
870             fseek ( tfile, lSize, SEEK_CUR);
871             fseek ( tfile, lSize, SEEK_CUR);
872         }
873
874         // ???
875         lSize = sizeof(int);
876         for (j = 0; j < num_muscles; j++) {
877             for (unsigned i = 0; i < muscles[j].verticesclothes.size(); i++) {
878                 if (muscles[j].verticesclothes.size() && muscles[j].verticesclothes[i] < modelclothes.vertexNum) {
879                     modelclothes.owner[muscles[j].verticesclothes[i]] = j;
880                 }
881             }
882         }
883
884         // use opengl for its matrix math
885         for (int i = 0; i < modelclothes.vertexNum; i++) {
886             modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
887             glMatrixMode(GL_MODELVIEW);
888             glPushMatrix();
889             glLoadIdentity();
890             glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
891             glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
892             glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
893             glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
894             glGetFloatv(GL_MODELVIEW_MATRIX, M);
895             modelclothes.vertex[i].x = M[12];
896             modelclothes.vertex[i].y = M[13];
897             modelclothes.vertex[i].z = M[14];
898             glPopMatrix();
899         }
900
901         modelclothes.CalculateNormals(0);
902     }
903     fclose(tfile);
904
905     for (int i = 0; i < num_joints; i++) {
906         for (j = 0; j < num_joints; j++) {
907             if (joints[i].label == j)
908                 jointlabels[j] = i;
909         }
910     }
911
912     free = 0;
913 }