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