]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Skeleton.cpp
Moved visibleloading check inside LoadingScreen
[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     float friction = 1.5;
111     const float elasticity = .3;
112     XYZ bounceness;
113     const int numrepeats = 3;
114     float groundlevel = .15;
115     int k, m;
116     unsigned i;
117     XYZ temp;
118     XYZ terrainnormal;
119     int whichhit;
120     float frictionness;
121     XYZ terrainlight;
122     int whichpatchx;
123     int whichpatchz;
124     float damage = 0; // eventually returned from function
125     bool breaking = false;
126
127     if (free) {
128         freetime += multiplier;
129
130         whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
131         whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
132
133         terrainlight = *coords;
134         Object::SphereCheckPossible(&terrainlight, 1);
135
136         //Add velocity
137         for (i = 0; i < joints.size(); i++) {
138             joints[i].position = joints[i].position + joints[i].velocity * multiplier;
139
140             switch (joints[i].label) {
141             case head:
142                 groundlevel = .8;
143                 break;
144             case righthand:
145             case rightwrist:
146             case rightelbow:
147             case lefthand:
148             case leftwrist:
149             case leftelbow:
150                 groundlevel = .2;
151                 break;
152             default:
153                 groundlevel = .15;
154                 break;
155             }
156
157             joints[i].position.y -= groundlevel;
158             joints[i].oldvelocity = joints[i].velocity;
159         }
160
161         float tempmult = multiplier;
162         //multiplier/=numrepeats;
163
164         for (int j = 0; j < numrepeats; j++) {
165             float r = .05;
166             // right leg constraints?
167             if (!joint(rightknee).locked && !joint(righthip).locked) {
168                 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
169                 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
170                     jointPos(rightknee) -= lowforward * .05;
171                     if (spinny)
172                         jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
173                     else
174                         jointVel(rightknee) -= lowforward * .05;
175                     jointPos(rightankle) += lowforward * .025;
176                     if (spinny)
177                         jointVel(rightankle) += lowforward * .025 / multiplier / 4;
178                     else
179                         jointVel(rightankle) += lowforward * .25;
180                     jointPos(righthip) += lowforward * .025;
181                     if (spinny)
182                         jointVel(righthip) += lowforward * .025 / multiplier / 4;
183                     else
184                         jointVel(righthip) += lowforward * .025;
185                     temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
186                 }
187             }
188
189             // left leg constraints?
190             if (!joint(leftknee).locked && !joint(lefthip).locked) {
191                 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
192                 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
193                     jointPos(leftknee) -= lowforward * .05;
194                     if (spinny)
195                         jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
196                     else
197                         jointVel(leftknee) -= lowforward * .05;
198                     jointPos(leftankle) += lowforward * .025;
199                     if (spinny)
200                         jointVel(leftankle) += lowforward * .025 / multiplier / 4;
201                     else
202                         jointVel(leftankle) += lowforward * .25;
203                     jointPos(lefthip) += lowforward * .025;
204                     if (spinny)
205                         jointVel(lefthip) += lowforward * .025 / multiplier / 4;
206                     else
207                         jointVel(lefthip) += lowforward * .025;
208                     temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
209                 }
210             }
211
212             for (i = 0; i < joints.size(); i++) {
213                 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
214                     joints[i].locked = 0;
215                 if (spinny && findLengthfast(&joints[i].velocity) > 600)
216                     joints[i].locked = 0;
217                 if (joints[i].delay > 0) {
218                     bool freely = true;
219                     for (unsigned j = 0; j < joints.size(); j++) {
220                         if (joints[j].locked)
221                             freely = false;
222                     }
223                     if (freely)
224                         joints[i].delay -= multiplier * 3;
225                 }
226             }
227
228             for (i = 0; i < muscles.size(); i++) {
229                 //Length constraints
230                 muscles[i].DoConstraint(spinny);
231             }
232
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                         k = terrain.patchobjects[whichpatchx][whichpatchz][m];
327                         if (k < Object::objects.size() && k >= 0)
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                 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
405             }
406         }
407         multiplier = tempmult;
408
409
410         if (terrain.patchobjectnum[whichpatchx][whichpatchz])
411             for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
412                 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
413                 if (Object::objects[k]->possible) {
414                     for (i = 0; i < 26; i++) {
415                         //Make this less stupid
416                         XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
417                         XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
418                         whichhit = Object::objects[k]->model.LineCheckSlidePossible(&start, &end, &temp, &Object::objects[k]->position, &Object::objects[k]->yaw);
419                         if (whichhit != -1) {
420                             joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
421                             for (unsigned j = 0; j < muscles.size(); j++) {
422                                 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]))
423                                     muscles[j].DoConstraint(spinny);
424                             }
425                         }
426                     }
427                 }
428             }
429
430         for (i = 0; i < joints.size(); i++) {
431             switch (joints[i].label) {
432             case head:
433                 groundlevel = .8;
434                 break;
435             case righthand:
436             case rightwrist:
437             case rightelbow:
438             case lefthand:
439             case leftwrist:
440             case leftelbow:
441                 groundlevel = .2;
442                 break;
443             default:
444                 groundlevel = .15;
445                 break;
446             }
447             joints[i].position.y += groundlevel;
448             joints[i].mass = 1;
449             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)
450                 joints[i].mass = 2;
451             if (joints[i].locked) {
452                 joints[i].mass = 4;
453             }
454         }
455
456         return damage;
457     }
458
459     if (!free) {
460         for (i = 0; i < muscles.size(); i++) {
461             if (muscles[i].type == boneconnect)
462                 muscles[i].DoConstraint(0);
463         }
464     }
465
466     return 0;
467 }
468
469 /* EFFECT
470  * applies gravity to the skeleton
471  *
472  * USES:
473  * Person/Person::DoStuff
474  */
475 void Skeleton::DoGravity(float *scale)
476 {
477     for (unsigned i = 0; i < joints.size(); i++) {
478         if (
479                 (
480                     ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
481                     (lowforward.y > -.1) ||
482                     (joints[i].mass < 5)
483                 ) && (
484                     ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
485                     (forward.y < .3)
486                 )
487             ) {
488             joints[i].velocity.y += gravity * multiplier / (*scale);
489         }
490     }
491 }
492
493 /* EFFECT
494  * set muscles[which].rotate1
495  *     .rotate2
496  *     .rotate3
497  *
498  * special case if animation == hanganim
499  */
500 void Skeleton::FindRotationMuscle(int which, int animation)
501 {
502     XYZ p1, p2, fwd;
503     float dist;
504
505     p1 = muscles[which].parent1->position;
506     p2 = muscles[which].parent2->position;
507     dist = findDistance(&p1, &p2);
508     if (p1.y - p2.y <= dist)
509         muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
510     if (p1.y - p2.y > dist)
511         muscles[which].rotate2 = asin(1.f);
512     muscles[which].rotate2 *= 360.0 / 6.2831853;
513
514     p1.y = 0;
515     p2.y = 0;
516     dist = findDistance(&p1, &p2);
517     if (p1.z - p2.z <= dist)
518         muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
519     if (p1.z - p2.z > dist)
520         muscles[which].rotate1 = acos(1.f);
521     muscles[which].rotate1 *= 360.0 / 6.2831853;
522     if (p1.x > p2.x)
523         muscles[which].rotate1 = 360 - muscles[which].rotate1;
524     if (!isnormal(muscles[which].rotate1))
525         muscles[which].rotate1 = 0;
526     if (!isnormal(muscles[which].rotate2))
527         muscles[which].rotate2 = 0;
528
529     const int label1 = muscles[which].parent1->label;
530     const int label2 = muscles[which].parent2->label;
531     switch (label1) {
532     case head:
533         fwd = specialforward[0];
534         break;
535     case rightshoulder:
536     case rightelbow:
537     case rightwrist:
538     case righthand:
539         fwd = specialforward[1];
540         break;
541     case leftshoulder:
542     case leftelbow:
543     case leftwrist:
544     case lefthand:
545         fwd = specialforward[2];
546         break;
547     case righthip:
548     case rightknee:
549     case rightankle:
550     case rightfoot:
551         fwd = specialforward[3];
552         break;
553     case lefthip:
554     case leftknee:
555     case leftankle:
556     case leftfoot:
557         fwd = specialforward[4];
558         break;
559     default:
560         if (muscles[which].parent1->lower)
561             fwd = lowforward;
562         else
563             fwd = forward;
564         break;
565     }
566
567     if (animation == hanganim) {
568         if (label1 == righthand || label2 == righthand) {
569             fwd = 0;
570             fwd.x = -1;
571         }
572         if (label1 == lefthand || label2 == lefthand) {
573             fwd = 0;
574             fwd.x = 1;
575         }
576     }
577
578     if (free == 0) {
579         if (label1 == rightfoot || label2 == rightfoot) {
580             fwd.y -= .3;
581         }
582         if (label1 == leftfoot || label2 == leftfoot) {
583             fwd.y -= .3;
584         }
585     }
586
587     fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
588     fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
589     fwd.y = 0;
590     fwd /= findLength(&fwd);
591     if (fwd.z <= 1 && fwd.z >= -1)
592         muscles[which].rotate3 = acos(0 - fwd.z);
593     else
594         muscles[which].rotate3 = acos(-1.f);
595     muscles[which].rotate3 *= 360.0 / 6.2831853;
596     if (0 > fwd.x)
597         muscles[which].rotate3 = 360 - muscles[which].rotate3;
598     if (!isnormal(muscles[which].rotate3))
599         muscles[which].rotate3 = 0;
600 }
601
602 /* EFFECT
603  * load skeleton
604  * takes filenames for three skeleton files and various models
605  */
606 void Skeleton::Load(const std::string& filename,       const std::string& lowfilename, const std::string& clothesfilename,
607                     const std::string& modelfilename,  const std::string& model2filename,
608                     const std::string& model3filename, const std::string& model4filename,
609                     const std::string& model5filename, const std::string& model6filename,
610                     const std::string& model7filename, const std::string& modellowfilename,
611                     const std::string& modelclothesfilename, bool clothes)
612 {
613     GLfloat M[16];
614     FILE *tfile;
615     float lSize;
616     int j, num_joints, num_muscles;
617
618     LOGFUNC;
619
620     num_models = 7;
621
622     // load various models
623     // rotate, scale, do normals, do texcoords for each as needed
624
625     model[0].loadnotex(modelfilename);
626     model[1].loadnotex(model2filename);
627     model[2].loadnotex(model3filename);
628     model[3].loadnotex(model4filename);
629     model[4].loadnotex(model5filename);
630     model[5].loadnotex(model6filename);
631     model[6].loadnotex(model7filename);
632
633     for (int i = 0; i < num_models; i++) {
634         model[i].Rotate(180, 0, 0);
635         model[i].Scale(.04, .04, .04);
636         model[i].CalculateNormals(0);
637     }
638
639     drawmodel.load(modelfilename, 0);
640     drawmodel.Rotate(180, 0, 0);
641     drawmodel.Scale(.04, .04, .04);
642     drawmodel.FlipTexCoords();
643     if ((Tutorial::active) && (id != 0)) {
644         drawmodel.UniformTexCoords();
645         drawmodel.ScaleTexCoords(0.1);
646     }
647     drawmodel.CalculateNormals(0);
648
649     modellow.loadnotex(modellowfilename);
650     modellow.Rotate(180, 0, 0);
651     modellow.Scale(.04, .04, .04);
652     modellow.CalculateNormals(0);
653
654     drawmodellow.load(modellowfilename, 0);
655     drawmodellow.Rotate(180, 0, 0);
656     drawmodellow.Scale(.04, .04, .04);
657     drawmodellow.FlipTexCoords();
658     if (Tutorial::active && id != 0)
659         drawmodellow.UniformTexCoords();
660     if (Tutorial::active && id != 0)
661         drawmodellow.ScaleTexCoords(0.1);
662     drawmodellow.CalculateNormals(0);
663
664     if (clothes) {
665         modelclothes.loadnotex(modelclothesfilename);
666         modelclothes.Rotate(180, 0, 0);
667         modelclothes.Scale(.041, .04, .041);
668         modelclothes.CalculateNormals(0);
669
670         drawmodelclothes.load(modelclothesfilename, 0);
671         drawmodelclothes.Rotate(180, 0, 0);
672         drawmodelclothes.Scale(.04, .04, .04);
673         drawmodelclothes.FlipTexCoords();
674         drawmodelclothes.CalculateNormals(0);
675     }
676
677     // FIXME: three similar blocks follow, one for each of:
678     // filename, lowfilename, clothesfilename
679
680     // load skeleton
681
682     tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
683
684     // read num_joints
685     funpackf(tfile, "Bi", &num_joints);
686
687     joints.resize(num_joints);
688
689     // read info for each joint
690     for (int i = 0; i < num_joints; i++) {
691         joints[i].load(tfile, joints);
692     }
693
694     // read num_muscles
695     funpackf(tfile, "Bi", &num_muscles);
696
697     // allocate memory
698     muscles.resize(num_muscles);
699
700     // for each muscle...
701     for (int i = 0; i < num_muscles; i++) {
702         muscles[i].load(tfile, model[0].vertexNum, joints);
703     }
704
705     // read forwardjoints (?)
706     for (j = 0; j < 3; j++) {
707         funpackf(tfile, "Bi", &forwardjoints[j]);
708     }
709     // read lowforwardjoints (?)
710     for (j = 0; j < 3; j++) {
711         funpackf(tfile, "Bi", &lowforwardjoints[j]);
712     }
713
714     // ???
715     for (j = 0; j < num_muscles; j++) {
716         for (unsigned i = 0; i < muscles[j].vertices.size(); i++) {
717             for (int k = 0; k < num_models; k++) {
718                 if (muscles[j].vertices[i] < model[k].vertexNum) {
719                     model[k].owner[muscles[j].vertices[i]] = j;
720                 }
721             }
722         }
723     }
724
725     // calculate some stuff
726     FindForwards();
727     for (int i = 0; i < num_muscles; i++) {
728         FindRotationMuscle(i, -1);
729     }
730     // this seems to use opengl purely for matrix calculations
731     for (int k = 0; k < num_models; k++) {
732         for (int i = 0; i < model[k].vertexNum; i++) {
733             model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
734             glMatrixMode(GL_MODELVIEW);
735             glPushMatrix();
736             glLoadIdentity();
737             glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
738             glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
739             glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
740             glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
741             glGetFloatv(GL_MODELVIEW_MATRIX, M);
742             model[k].vertex[i].x = M[12] * 1;
743             model[k].vertex[i].y = M[13] * 1;
744             model[k].vertex[i].z = M[14] * 1;
745             glPopMatrix();
746         }
747         model[k].CalculateNormals(0);
748     }
749     fclose(tfile);
750
751     // load ???
752
753     tfile = Folders::openMandatoryFile( Folders::getResourcePath(lowfilename), "rb" );
754
755     // skip joints section
756
757     fseek(tfile, sizeof(num_joints), SEEK_CUR);
758     for (int i = 0; i < num_joints; i++) {
759         // skip joint info
760         lSize = sizeof(XYZ)
761                 + sizeof(float)
762                 + sizeof(float)
763                 + 1 //sizeof(bool)
764                 + 1 //sizeof(bool)
765                 + sizeof(int)
766                 + 1 //sizeof(bool)
767                 + 1 //sizeof(bool)
768                 + sizeof(int)
769                 + sizeof(int)
770                 + 1 //sizeof(bool)
771                 + sizeof(int);
772         fseek(tfile, lSize, SEEK_CUR);
773     }
774
775     // skip num_muscles
776     fseek(tfile, sizeof(num_muscles), SEEK_CUR);
777
778     for (int i = 0; i < num_muscles; i++) {
779         // skip muscle info
780         lSize = sizeof(float)
781                 + sizeof(float)
782                 + sizeof(float)
783                 + sizeof(float)
784                 + sizeof(float)
785                 + sizeof(int);
786         fseek(tfile, lSize, SEEK_CUR);
787
788         muscles[i].loadVerticesLow(tfile, modellow.vertexNum);
789
790         // skip more stuff
791         lSize = 1; //sizeof(bool);
792         fseek ( tfile, lSize, SEEK_CUR);
793         lSize = sizeof(int);
794         fseek ( tfile, lSize, SEEK_CUR);
795         fseek ( tfile, lSize, SEEK_CUR);
796     }
797
798     for (j = 0; j < num_muscles; j++) {
799         for (unsigned i = 0; i < muscles[j].verticeslow.size(); i++) {
800             if (muscles[j].verticeslow[i] < modellow.vertexNum) {
801                 modellow.owner[muscles[j].verticeslow[i]] = j;
802             }
803         }
804     }
805
806     // use opengl for its matrix math
807     for (int i = 0; i < modellow.vertexNum; i++) {
808         modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
809         glMatrixMode(GL_MODELVIEW);
810         glPushMatrix();
811         glLoadIdentity();
812         glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
813         glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
814         glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
815         glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
816         glGetFloatv(GL_MODELVIEW_MATRIX, M);
817         modellow.vertex[i].x = M[12];
818         modellow.vertex[i].y = M[13];
819         modellow.vertex[i].z = M[14];
820         glPopMatrix();
821     }
822
823     modellow.CalculateNormals(0);
824
825     // load clothes
826
827     if (clothes) {
828         tfile = Folders::openMandatoryFile( Folders::getResourcePath(clothesfilename), "rb" );
829
830         // skip num_joints
831         fseek(tfile, sizeof(num_joints), SEEK_CUR);
832
833         for (int i = 0; i < num_joints; i++) {
834             // skip joint info
835             lSize = sizeof(XYZ)
836                     + sizeof(float)
837                     + sizeof(float)
838                     + 1 //sizeof(bool)
839                     + 1 //sizeof(bool)
840                     + sizeof(int)
841                     + 1 //sizeof(bool)
842                     + 1 //sizeof(bool)
843                     + sizeof(int)
844                     + sizeof(int)
845                     + 1 //sizeof(bool)
846                     + sizeof(int);
847             fseek(tfile, lSize, SEEK_CUR);
848         }
849
850         // skip num_muscles
851         fseek(tfile, sizeof(num_muscles), SEEK_CUR);
852
853         for (int i = 0; i < num_muscles; i++) {
854             // skip muscle info
855             lSize = sizeof(float)
856                     + sizeof(float)
857                     + sizeof(float)
858                     + sizeof(float)
859                     + sizeof(float)
860                     + sizeof(int);
861             fseek(tfile, lSize, SEEK_CUR);
862
863             muscles[i].loadVerticesClothes(tfile, modelclothes.vertexNum);
864
865             // skip more stuff
866             lSize = 1; //sizeof(bool);
867             fseek ( tfile, lSize, SEEK_CUR);
868             lSize = sizeof(int);
869             fseek ( tfile, lSize, SEEK_CUR);
870             fseek ( tfile, lSize, SEEK_CUR);
871         }
872
873         // ???
874         lSize = sizeof(int);
875         for (j = 0; j < num_muscles; j++) {
876             for (unsigned i = 0; i < muscles[j].verticesclothes.size(); i++) {
877                 if (muscles[j].verticesclothes.size() && muscles[j].verticesclothes[i] < modelclothes.vertexNum) {
878                     modelclothes.owner[muscles[j].verticesclothes[i]] = j;
879                 }
880             }
881         }
882
883         // use opengl for its matrix math
884         for (int i = 0; i < modelclothes.vertexNum; i++) {
885             modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
886             glMatrixMode(GL_MODELVIEW);
887             glPushMatrix();
888             glLoadIdentity();
889             glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
890             glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
891             glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
892             glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
893             glGetFloatv(GL_MODELVIEW_MATRIX, M);
894             modelclothes.vertex[i].x = M[12];
895             modelclothes.vertex[i].y = M[13];
896             modelclothes.vertex[i].z = M[14];
897             glPopMatrix();
898         }
899
900         modelclothes.CalculateNormals(0);
901     }
902     fclose(tfile);
903
904     for (int i = 0; i < num_joints; i++) {
905         for (j = 0; j < num_joints; j++) {
906             if (joints[i].label == j)
907                 jointlabels[j] = i;
908         }
909     }
910
911     free = 0;
912 }