2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
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.
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.
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/>.
21 /**> HEADER FILES <**/
23 #include "Animation/Skeleton.h"
24 #include "openal_wrapper.h"
25 #include "Animation/Animation.h"
26 #include "Utils/Folders.h"
28 extern float multiplier;
30 extern Terrain terrain;
31 extern Objects objects;
32 extern int environment;
33 extern float camerashake;
36 extern int tutoriallevel;
38 extern int whichjointstartarray[26];
39 extern int whichjointendarray[26];
41 extern bool visibleloading;
45 void dealloc2(void* param)
50 enum {boneconnect, constraint, muscle};
54 * sets strength, length,
55 * parent1->position, parent2->position,
56 * parent1->velocity, parent2->velocity
60 * Skeleton::DoConstraints
62 void Muscle::DoConstraint(bool spinny)
64 // FIXME: relaxlength shouldn't be static, but may not always be set
65 // so I don't want to change the existing behavior even though it's probably a bug
66 static float relaxlength;
68 float oldlength = length;
70 if (type != boneconnect)
71 relaxlength = findDistance(&parent1->position, &parent2->position);
73 if (type == boneconnect)
75 if (type == constraint)
84 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
85 length -= (length - targetlength) * (strength) * multiplier * 10000;
89 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
93 if (length < minlength)
95 if (length > maxlength)
98 if (length == relaxlength)
104 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
106 //Find vector from midpoint to second vector
107 XYZ vel = parent2->position - midp;
109 //Change to unit vector
112 //Apply velocity change
113 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
114 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
115 if (!freeze && spinny) {
116 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
117 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
119 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
120 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
123 //Move child point to within certain distance of parent point
124 parent1->position = newpoint1;
125 parent2->position = newpoint2;
129 * sets forward, lowforward, specialforward[]
133 * Person/Person::DoAnimations
134 * Person/Person::DrawSkeleton
136 void Skeleton::FindForwards()
138 //Find forward vectors
139 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
142 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
143 Normalise(&lowforward);
146 specialforward[0] = forward;
148 specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
149 specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
150 specialforward[1] += forward * .4;
151 Normalise(&specialforward[1]);
152 specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
153 specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
154 specialforward[2] += forward * .4;
155 Normalise(&specialforward[2]);
157 specialforward[3] = jointPos(righthip) + jointPos(rightankle);
158 specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
159 specialforward[3] += lowforward * .4;
160 Normalise(&specialforward[3]);
161 specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
162 specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
163 specialforward[4] += lowforward * .4;
164 Normalise(&specialforward[4]);
171 * Person/Person::RagDoll
172 * Person/Person::DoStuff
175 float Skeleton::DoConstraints(XYZ *coords, float *scale)
177 float friction = 1.5;
178 const float elasticity = .3;
180 const int numrepeats = 3;
181 float groundlevel = .15;
190 float damage = 0; // eventually returned from function
191 bool breaking = false;
194 freetime += multiplier;
196 whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
197 whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
199 terrainlight = *coords;
200 objects.SphereCheckPossible(&terrainlight, 1);
203 for (i = 0; i < num_joints; i++) {
204 joints[i].position = joints[i].position + joints[i].velocity * multiplier;
206 switch (joints[i].label) {
223 joints[i].position.y -= groundlevel;
224 joints[i].oldvelocity = joints[i].velocity;
227 float tempmult = multiplier;
228 //multiplier/=numrepeats;
230 for (j = 0; j < numrepeats; j++) {
232 // right leg constraints?
233 if (!joint(rightknee).locked && !joint(righthip).locked) {
234 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
235 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
236 jointPos(rightknee) -= lowforward * .05;
238 jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
240 jointVel(rightknee) -= lowforward * .05;
241 jointPos(rightankle) += lowforward * .025;
243 jointVel(rightankle) += lowforward * .025 / multiplier / 4;
245 jointVel(rightankle) += lowforward * .25;
246 jointPos(righthip) += lowforward * .025;
248 jointVel(righthip) += lowforward * .025 / multiplier / 4;
250 jointVel(righthip) += lowforward * .025;
251 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
255 // left leg constraints?
256 if (!joint(leftknee).locked && !joint(lefthip).locked) {
257 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
258 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
259 jointPos(leftknee) -= lowforward * .05;
261 jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
263 jointVel(leftknee) -= lowforward * .05;
264 jointPos(leftankle) += lowforward * .025;
266 jointVel(leftankle) += lowforward * .025 / multiplier / 4;
268 jointVel(leftankle) += lowforward * .25;
269 jointPos(lefthip) += lowforward * .025;
271 jointVel(lefthip) += lowforward * .025 / multiplier / 4;
273 jointVel(lefthip) += lowforward * .025;
274 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
278 for (i = 0; i < num_joints; i++) {
279 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
280 joints[i].locked = 0;
281 if (spinny && findLengthfast(&joints[i].velocity) > 600)
282 joints[i].locked = 0;
283 if (joints[i].delay > 0) {
285 for (j = 0; j < num_joints; j++) {
286 if (joints[j].locked)
290 joints[i].delay -= multiplier * 3;
295 for (i = 0; i < num_muscles; i++) {
297 muscles[i].DoConstraint(spinny);
300 for (i = 0; i < num_joints; i++) {
304 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) {
307 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
308 joints[i].locked = 1;
310 if (tutoriallevel != 1 || id == 0) {
311 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
316 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
317 joints[i].locked = 1;
319 if (tutoriallevel != 1 || id == 0) {
320 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
324 terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
325 ReflectVector(&joints[i].velocity, &terrainnormal);
326 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
327 if (!joints[i].locked)
328 damage += findLengthfast(&bounceness) / 4000;
329 if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
331 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
332 joints[i].velocity -= bounceness;
333 if (1 - friction * frictionness > 0)
334 joints[i].velocity *= 1 - friction * frictionness;
336 joints[i].velocity = 0;
338 if (tutoriallevel != 1 || id == 0)
339 if (findLengthfast(&bounceness) > 8000 && breaking) {
340 // FIXME: this crashes because k is not initialized!
341 // to reproduce, type 'wolfie' in console and play a while
342 // I'll just comment it out for now
343 //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
344 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
348 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
350 addEnvSound(*coords, 64);
353 if (findLengthfast(&bounceness) > 2500) {
354 Normalise(&bounceness);
355 bounceness = bounceness * 50;
358 joints[i].velocity += bounceness * elasticity;
360 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
362 joints[i].velocity = joints[i].oldvelocity;
366 if (joints[i].locked == 0)
367 if (findLengthfast(&joints[i].velocity) < 1)
368 joints[i].locked = 1;
370 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
371 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
372 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
374 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
375 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
376 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
377 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);
380 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
381 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
382 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);
383 } else if (findLengthfast(&bounceness) > 500)
384 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
387 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
391 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
392 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
393 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
394 if (k < objects.numobjects && k >= 0)
395 if (objects.possible[k]) {
396 friction = objects.friction[k];
397 XYZ start = joints[i].realoldposition;
398 XYZ end = joints[i].position * (*scale) + *coords;
399 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
400 if (whichhit != -1) {
401 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
402 joints[i].locked = 1;
404 if (tutoriallevel != 1 || id == 0) {
405 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
410 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
411 joints[i].locked = 1;
413 if (tutoriallevel != 1 || id == 0) {
414 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
418 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
419 if (terrainnormal.y > .8)
421 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
422 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
424 joints[i].velocity = joints[i].oldvelocity;
426 if (tutoriallevel != 1 || id == 0)
427 if (findLengthfast(&bounceness) > 4000 && breaking) {
428 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
429 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
433 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
435 addEnvSound(*coords, 64);
437 if (objects.type[k] == treetrunktype) {
438 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
439 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
440 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
441 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
443 if (!joints[i].locked)
444 damage += findLengthfast(&bounceness) / 2500;
445 ReflectVector(&joints[i].velocity, &terrainnormal);
446 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
447 joints[i].velocity -= bounceness;
448 if (1 - friction * frictionness > 0)
449 joints[i].velocity *= 1 - friction * frictionness;
451 joints[i].velocity = 0;
452 if (findLengthfast(&bounceness) > 2500) {
453 Normalise(&bounceness);
454 bounceness = bounceness * 50;
456 joints[i].velocity += bounceness * elasticity;
459 if (!joints[i].locked)
460 if (findLengthfast(&joints[i].velocity) < 1) {
461 joints[i].locked = 1;
463 if (findLengthfast(&bounceness) > 500)
464 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
465 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
471 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
474 multiplier = tempmult;
477 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
478 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
479 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
480 if (objects.possible[k]) {
481 for (i = 0; i < 26; i++) {
482 //Make this less stupid
483 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
484 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
485 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
486 if (whichhit != -1) {
487 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
488 for (j = 0; j < num_muscles; j++) {
489 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]))
490 muscles[j].DoConstraint(spinny);
497 for (i = 0; i < num_joints; i++) {
498 switch (joints[i].label) {
514 joints[i].position.y += groundlevel;
516 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)
518 if (joints[i].locked) {
527 for (i = 0; i < num_muscles; i++) {
528 if (muscles[i].type == boneconnect)
529 muscles[i].DoConstraint(0);
537 * applies gravity to the skeleton
540 * Person/Person::DoStuff
542 void Skeleton::DoGravity(float *scale)
545 for (i = 0; i < num_joints; i++) {
548 ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
549 (lowforward.y > -.1) ||
552 ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
556 joints[i].velocity.y += gravity * multiplier / (*scale);
561 * set muscles[which].rotate1
565 * special case if animation == hanganim
567 void Skeleton::FindRotationMuscle(int which, int animation)
572 p1 = muscles[which].parent1->position;
573 p2 = muscles[which].parent2->position;
574 dist = findDistance(&p1, &p2);
575 if (p1.y - p2.y <= dist)
576 muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
577 if (p1.y - p2.y > dist)
578 muscles[which].rotate2 = asin(1.f);
579 muscles[which].rotate2 *= 360.0 / 6.2831853;
583 dist = findDistance(&p1, &p2);
584 if (p1.z - p2.z <= dist)
585 muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
586 if (p1.z - p2.z > dist)
587 muscles[which].rotate1 = acos(1.f);
588 muscles[which].rotate1 *= 360.0 / 6.2831853;
590 muscles[which].rotate1 = 360 - muscles[which].rotate1;
591 if (!isnormal(muscles[which].rotate1))
592 muscles[which].rotate1 = 0;
593 if (!isnormal(muscles[which].rotate2))
594 muscles[which].rotate2 = 0;
596 const int label1 = muscles[which].parent1->label;
597 const int label2 = muscles[which].parent2->label;
600 fwd = specialforward[0];
606 fwd = specialforward[1];
612 fwd = specialforward[2];
618 fwd = specialforward[3];
624 fwd = specialforward[4];
627 if (muscles[which].parent1->lower)
634 if (animation == hanganim) {
635 if (label1 == righthand || label2 == righthand) {
639 if (label1 == lefthand || label2 == lefthand) {
646 if (label1 == rightfoot || label2 == rightfoot) {
649 if (label1 == leftfoot || label2 == leftfoot) {
654 fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
655 fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
657 fwd /= findLength(&fwd);
658 if (fwd.z <= 1 && fwd.z >= -1)
659 muscles[which].rotate3 = acos(0 - fwd.z);
661 muscles[which].rotate3 = acos(-1.f);
662 muscles[which].rotate3 *= 360.0 / 6.2831853;
664 muscles[which].rotate3 = 360 - muscles[which].rotate3;
665 if (!isnormal(muscles[which].rotate3))
666 muscles[which].rotate3 = 0;
671 * takes filenames for three skeleton files and various models
673 void Skeleton::Load(const std::string& filename, const std::string& lowfilename, const std::string& clothesfilename,
674 const std::string& modelfilename, const std::string& model2filename,
675 const std::string& model3filename, const std::string& model4filename,
676 const std::string& model5filename, const std::string& model6filename,
677 const std::string& model7filename, const std::string& modellowfilename,
678 const std::string& modelclothesfilename, bool clothes)
691 // load various models
692 // rotate, scale, do normals, do texcoords for each as needed
694 model[0].loadnotex(modelfilename);
695 model[1].loadnotex(model2filename);
696 model[2].loadnotex(model3filename);
697 model[3].loadnotex(model4filename);
698 model[4].loadnotex(model5filename);
699 model[5].loadnotex(model6filename);
700 model[6].loadnotex(model7filename);
702 for (i = 0; i < num_models; i++) {
703 model[i].Rotate(180, 0, 0);
704 model[i].Scale(.04, .04, .04);
705 model[i].CalculateNormals(0);
708 drawmodel.load(modelfilename, 0);
709 drawmodel.Rotate(180, 0, 0);
710 drawmodel.Scale(.04, .04, .04);
711 drawmodel.FlipTexCoords();
712 if (tutoriallevel == 1 && id != 0)
713 drawmodel.UniformTexCoords();
714 if (tutoriallevel == 1 && id != 0)
715 drawmodel.ScaleTexCoords(0.1);
716 drawmodel.CalculateNormals(0);
718 modellow.loadnotex(modellowfilename);
719 modellow.Rotate(180, 0, 0);
720 modellow.Scale(.04, .04, .04);
721 modellow.CalculateNormals(0);
723 drawmodellow.load(modellowfilename, 0);
724 drawmodellow.Rotate(180, 0, 0);
725 drawmodellow.Scale(.04, .04, .04);
726 drawmodellow.FlipTexCoords();
727 if (tutoriallevel == 1 && id != 0)
728 drawmodellow.UniformTexCoords();
729 if (tutoriallevel == 1 && id != 0)
730 drawmodellow.ScaleTexCoords(0.1);
731 drawmodellow.CalculateNormals(0);
734 modelclothes.loadnotex(modelclothesfilename);
735 modelclothes.Rotate(180, 0, 0);
736 modelclothes.Scale(.041, .04, .041);
737 modelclothes.CalculateNormals(0);
739 drawmodelclothes.load(modelclothesfilename, 0);
740 drawmodelclothes.Rotate(180, 0, 0);
741 drawmodelclothes.Scale(.04, .04, .04);
742 drawmodelclothes.FlipTexCoords();
743 drawmodelclothes.CalculateNormals(0);
746 // FIXME: three similar blocks follow, one for each of:
747 // filename, lowfilename, clothesfilename
751 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
754 funpackf(tfile, "Bi", &num_joints);
758 delete [] joints; //dealloc2(joints);
759 joints = (Joint*)new Joint[num_joints];
761 // read info for each joint
762 for (i = 0; i < num_joints; i++) {
763 funpackf(tfile, "Bf Bf Bf Bf Bf", &joints[i].position.x, &joints[i].position.y, &joints[i].position.z, &joints[i].length, &joints[i].mass);
764 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
765 funpackf(tfile, "Bi", &joints[i].modelnum);
766 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
767 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
768 funpackf(tfile, "Bb", &joints[i].lower);
769 funpackf(tfile, "Bi", &parentID);
770 if (joints[i].hasparent)
771 joints[i].parent = &joints[parentID];
772 joints[i].velocity = 0;
773 joints[i].oldposition = joints[i].position;
777 funpackf(tfile, "Bi", &num_muscles);
781 delete [] muscles; //dealloc2(muscles);
782 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
784 // for each muscle...
785 for (i = 0; i < num_muscles; i++) {
787 funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &muscles[i].length, &muscles[i].targetlength, &muscles[i].minlength, &muscles[i].maxlength, &muscles[i].strength, &muscles[i].type, &muscles[i].numvertices);
789 // allocate memory for vertices
790 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
794 for (j = 0; j < muscles[i].numvertices - edit; j++) {
795 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
796 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
797 muscles[i].numvertices--;
803 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
804 muscles[i].parent1 = &joints[parentID];
805 funpackf(tfile, "Bi", &parentID);
806 muscles[i].parent2 = &joints[parentID];
809 // read forwardjoints (?)
810 for (j = 0; j < 3; j++) {
811 funpackf(tfile, "Bi", &forwardjoints[j]);
813 // read lowforwardjoints (?)
814 for (j = 0; j < 3; j++) {
815 funpackf(tfile, "Bi", &lowforwardjoints[j]);
819 for (j = 0; j < num_muscles; j++) {
820 for (i = 0; i < muscles[j].numvertices; i++) {
821 for (int k = 0; k < num_models; k++) {
822 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
823 model[k].owner[muscles[j].vertices[i]] = j;
828 // calculate some stuff
830 for (i = 0; i < num_joints; i++) {
831 joints[i].startpos = joints[i].position;
833 for (i = 0; i < num_muscles; i++) {
834 FindRotationMuscle(i, -1);
836 // this seems to use opengl purely for matrix calculations
837 for (int k = 0; k < num_models; k++) {
838 for (i = 0; i < model[k].vertexNum; i++) {
839 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
840 glMatrixMode(GL_MODELVIEW);
843 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
844 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
845 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
846 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
847 glGetFloatv(GL_MODELVIEW_MATRIX, M);
848 model[k].vertex[i].x = M[12] * 1;
849 model[k].vertex[i].y = M[13] * 1;
850 model[k].vertex[i].z = M[14] * 1;
853 model[k].CalculateNormals(0);
859 tfile = Folders::openMandatoryFile( Folders::getResourcePath(lowfilename), "rb" );
861 // skip joints section
863 lSize = sizeof(num_joints);
864 fseek(tfile, lSize, SEEK_CUR);
865 for (i = 0; i < num_joints; i++) {
879 fseek(tfile, lSize, SEEK_CUR);
881 if (joints[i].hasparent)
882 joints[i].parent = &joints[parentID];
883 joints[i].velocity = 0;
884 joints[i].oldposition = joints[i].position;
888 funpackf(tfile, "Bi", &num_muscles);
890 for (i = 0; i < num_muscles; i++) {
892 lSize = sizeof(float)
898 fseek(tfile, lSize, SEEK_CUR);
900 // read numverticeslow
901 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
903 if (muscles[i].numverticeslow) {
905 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
909 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
910 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
911 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
912 muscles[i].numverticeslow--;
919 lSize = 1; //sizeof(bool);
920 fseek ( tfile, lSize, SEEK_CUR);
922 fseek ( tfile, lSize, SEEK_CUR);
923 fseek ( tfile, lSize, SEEK_CUR);
926 for (j = 0; j < num_muscles; j++) {
927 for (i = 0; i < muscles[j].numverticeslow; i++) {
928 if (muscles[j].verticeslow[i] < modellow.vertexNum)
929 modellow.owner[muscles[j].verticeslow[i]] = j;
933 // use opengl for its matrix math
934 for (i = 0; i < modellow.vertexNum; i++) {
935 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
936 glMatrixMode(GL_MODELVIEW);
939 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
940 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
941 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
942 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
943 glGetFloatv(GL_MODELVIEW_MATRIX, M);
944 modellow.vertex[i].x = M[12];
945 modellow.vertex[i].y = M[13];
946 modellow.vertex[i].z = M[14];
950 modellow.CalculateNormals(0);
955 tfile = Folders::openMandatoryFile( Folders::getResourcePath(clothesfilename), "rb" );
958 lSize = sizeof(num_joints);
959 fseek ( tfile, lSize, SEEK_CUR);
961 for (i = 0; i < num_joints; i++) {
975 fseek(tfile, lSize, SEEK_CUR);
977 if (joints[i].hasparent)
978 joints[i].parent = &joints[parentID];
979 joints[i].velocity = 0;
980 joints[i].oldposition = joints[i].position;
984 funpackf(tfile, "Bi", &num_muscles);
986 for (i = 0; i < num_muscles; i++) {
988 lSize = sizeof(float)
994 fseek(tfile, lSize, SEEK_CUR);
996 // read numverticesclothes
997 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
999 // read verticesclothes
1000 if (muscles[i].numverticesclothes) {
1001 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1003 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1004 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1005 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1006 muscles[i].numverticesclothes--;
1013 lSize = 1; //sizeof(bool);
1014 fseek ( tfile, lSize, SEEK_CUR);
1015 lSize = sizeof(int);
1016 fseek ( tfile, lSize, SEEK_CUR);
1017 fseek ( tfile, lSize, SEEK_CUR);
1021 lSize = sizeof(int);
1022 for (j = 0; j < num_muscles; j++) {
1023 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1024 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1025 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1029 // use opengl for its matrix math
1030 for (i = 0; i < modelclothes.vertexNum; i++) {
1031 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1032 glMatrixMode(GL_MODELVIEW);
1035 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1036 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1037 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1038 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1039 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1040 modelclothes.vertex[i].x = M[12];
1041 modelclothes.vertex[i].y = M[13];
1042 modelclothes.vertex[i].z = M[14];
1046 modelclothes.CalculateNormals(0);
1050 for (i = 0; i < num_joints; i++) {
1051 for (j = 0; j < num_joints; j++) {
1052 if (joints[i].label == j)
1060 Skeleton::Skeleton()
1068 memset(forwardjoints, 0, sizeof(forwardjoints));
1073 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1076 // XYZ specialforward[5];
1077 memset(jointlabels, 0, sizeof(jointlabels));
1081 // Model modelclothes;
1085 // Model drawmodellow;
1086 // Model drawmodelclothes;
1091 memset(skinText, 0, sizeof(skinText));
1108 Skeleton::~Skeleton()
1125 verticesclothes = 0;
1129 numverticesclothes = 0;
1138 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1139 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1140 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1141 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1149 dealloc2(verticeslow);
1150 dealloc2(verticesclothes);
1155 // the following functions are not used anywhere
1158 * sets forward, lowforward, specialforward[]
1163 void Skeleton::FindForwardsfirst()
1165 //Find forward vectors
1166 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1167 Normalise(&forward);
1169 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1170 Normalise(&lowforward);
1173 specialforward[0] = forward;
1174 specialforward[1] = forward;
1175 specialforward[2] = forward;
1176 specialforward[3] = forward;
1177 specialforward[4] = forward;
1186 void Skeleton::Draw(int muscleview)
1188 static float jointcolor[4];
1190 if (muscleview != 2) {
1197 if (muscleview == 2) {
1203 //Calc motionblur-ness
1204 for (int i = 0; i < num_joints; i++) {
1205 joints[i].oldposition = joints[i].position;
1206 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1207 if (joints[i].blurred < 1)
1208 joints[i].blurred = 1;
1214 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1216 for (int i = 0; i < num_joints; i++) {
1217 if (joints[i].hasparent) {
1218 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1219 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1220 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1221 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1222 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1223 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1224 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1225 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1228 for (int i = 0; i < num_muscles; i++) {
1229 if (muscles[i].type == boneconnect) {
1230 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1231 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1232 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1233 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1234 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1235 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1236 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1237 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1243 for (int i = 0; i < num_joints; i++) {
1244 if (joints[i].hasparent) {
1245 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1246 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1247 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1248 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1251 for (int i = 0; i < num_muscles; i++) {
1252 if (muscles[i].type == boneconnect) {
1253 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1254 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1255 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1256 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1259 glColor3f(.6, .6, 0);
1260 if (muscleview == 1)
1261 for (int i = 0; i < num_muscles; i++) {
1262 if (muscles[i].type != boneconnect) {
1263 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1264 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1269 if (muscleview != 2) {
1272 for (int i = 0; i < num_joints; i++) {
1274 glColor4f(0, 0, .5, 1);
1276 glColor4f(1, 1, 0, 1);
1277 if (joints[i].locked && i != selected)
1278 glColor4f(1, 0, 0, 1);
1279 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1284 //Set old position to current position
1285 if (muscleview == 2)
1286 for (int i = 0; i < num_joints; i++) {
1287 joints[i].oldposition = joints[i].position;
1297 void Skeleton::AddJoint(float x, float y, float z, int which)
1299 if (num_joints < max_joints - 1) {
1300 joints[num_joints].velocity = 0;
1301 joints[num_joints].position.x = x;
1302 joints[num_joints].position.y = y;
1303 joints[num_joints].position.z = z;
1304 joints[num_joints].mass = 1;
1305 joints[num_joints].locked = 0;
1307 joints[num_joints].hasparent = 0;
1309 if (which < num_joints && which >= 0)
1310 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1319 void Skeleton::DeleteJoint(int whichjoint)
1321 if (whichjoint < num_joints && whichjoint >= 0) {
1322 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1323 joints[whichjoint].position = joints[num_joints - 1].position;
1324 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1325 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1326 joints[whichjoint].parent = joints[num_joints - 1].parent;
1327 joints[whichjoint].length = joints[num_joints - 1].length;
1328 joints[whichjoint].locked = joints[num_joints - 1].locked;
1329 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1330 joints[whichjoint].visible = joints[num_joints - 1].visible;
1332 for (int i = 0; i < num_muscles; i++) {
1333 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1334 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1336 for (int i = 0; i < num_muscles; i++) {
1337 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1338 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1340 for (int i = 0; i < num_joints; i++) {
1341 if (joints[i].parent == &joints[whichjoint])
1342 joints[i].hasparent = 0;
1344 for (int i = 0; i < num_joints; i++) {
1345 if (joints[i].parent == &joints[num_joints - 1])
1346 joints[i].parent = &joints[whichjoint];
1356 * Skeleton::DeleteJoint - UNUSED
1358 void Skeleton::DeleteMuscle(int whichmuscle)
1360 if (whichmuscle < num_muscles) {
1361 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1362 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1363 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1364 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1365 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1366 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1367 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1368 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1369 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1380 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1382 if (whichjoint < num_joints) {
1383 joints[whichjoint].velocity = 0;
1384 joints[whichjoint].position.x = x;
1385 joints[whichjoint].position.y = y;
1386 joints[whichjoint].position.z = z;
1388 if (which >= num_joints || which < 0)
1389 joints[whichjoint].hasparent = 0;
1390 if (which < num_joints && which >= 0) {
1391 joints[whichjoint].parent = &joints[which];
1392 joints[whichjoint].hasparent = 1;
1393 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1401 * Skeleton::AddJoint - UNUSED
1403 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1405 const int max_muscles = 100; // FIXME: Probably can be dropped
1406 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1407 muscles[num_muscles].parent1 = &joints[attach1];
1408 muscles[num_muscles].parent2 = &joints[attach2];
1409 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1410 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1411 muscles[num_muscles].strength = .7;
1412 muscles[num_muscles].type = type;
1413 muscles[num_muscles].minlength = minlength;
1414 muscles[num_muscles].maxlength = maxlength;
1425 void Skeleton::MusclesSet()
1427 for (int i = 0; i < num_muscles; i++) {
1428 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1437 void Skeleton::DoBalance()
1440 newpoint=joints[0].position;
1441 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1442 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1443 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1444 //Move child point to within certain distance of parent point
1445 joints[0].position=newpoint;