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