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