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