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