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;
51 memset(forwardjoints, 0, sizeof(forwardjoints));
55 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
57 memset(jointlabels, 0, sizeof(jointlabels));
64 memset(skinText, 0, sizeof(skinText));
95 * sets forward, lowforward, specialforward[]
99 * Person/Person::DoAnimations
100 * Person/Person::DrawSkeleton
102 void Skeleton::FindForwards()
104 //Find forward vectors
105 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
108 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
109 Normalise(&lowforward);
112 specialforward[0] = forward;
114 specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
115 specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
116 specialforward[1] += forward * .4;
117 Normalise(&specialforward[1]);
118 specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
119 specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
120 specialforward[2] += forward * .4;
121 Normalise(&specialforward[2]);
123 specialforward[3] = jointPos(righthip) + jointPos(rightankle);
124 specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
125 specialforward[3] += lowforward * .4;
126 Normalise(&specialforward[3]);
127 specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
128 specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
129 specialforward[4] += lowforward * .4;
130 Normalise(&specialforward[4]);
137 * Person/Person::RagDoll
138 * Person/Person::DoStuff
141 float Skeleton::DoConstraints(XYZ *coords, float *scale)
143 float friction = 1.5;
144 const float elasticity = .3;
146 const int numrepeats = 3;
147 float groundlevel = .15;
156 float damage = 0; // eventually returned from function
157 bool breaking = false;
160 freetime += multiplier;
162 whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
163 whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
165 terrainlight = *coords;
166 objects.SphereCheckPossible(&terrainlight, 1);
169 for (i = 0; i < num_joints; i++) {
170 joints[i].position = joints[i].position + joints[i].velocity * multiplier;
172 switch (joints[i].label) {
189 joints[i].position.y -= groundlevel;
190 joints[i].oldvelocity = joints[i].velocity;
193 float tempmult = multiplier;
194 //multiplier/=numrepeats;
196 for (j = 0; j < numrepeats; j++) {
198 // right leg constraints?
199 if (!joint(rightknee).locked && !joint(righthip).locked) {
200 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
201 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
202 jointPos(rightknee) -= lowforward * .05;
204 jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
206 jointVel(rightknee) -= lowforward * .05;
207 jointPos(rightankle) += lowforward * .025;
209 jointVel(rightankle) += lowforward * .025 / multiplier / 4;
211 jointVel(rightankle) += lowforward * .25;
212 jointPos(righthip) += lowforward * .025;
214 jointVel(righthip) += lowforward * .025 / multiplier / 4;
216 jointVel(righthip) += lowforward * .025;
217 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
221 // left leg constraints?
222 if (!joint(leftknee).locked && !joint(lefthip).locked) {
223 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
224 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
225 jointPos(leftknee) -= lowforward * .05;
227 jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
229 jointVel(leftknee) -= lowforward * .05;
230 jointPos(leftankle) += lowforward * .025;
232 jointVel(leftankle) += lowforward * .025 / multiplier / 4;
234 jointVel(leftankle) += lowforward * .25;
235 jointPos(lefthip) += lowforward * .025;
237 jointVel(lefthip) += lowforward * .025 / multiplier / 4;
239 jointVel(lefthip) += lowforward * .025;
240 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
244 for (i = 0; i < num_joints; i++) {
245 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
246 joints[i].locked = 0;
247 if (spinny && findLengthfast(&joints[i].velocity) > 600)
248 joints[i].locked = 0;
249 if (joints[i].delay > 0) {
251 for (j = 0; j < num_joints; j++) {
252 if (joints[j].locked)
256 joints[i].delay -= multiplier * 3;
261 for (i = 0; i < num_muscles; i++) {
263 muscles[i].DoConstraint(spinny);
266 for (i = 0; i < num_joints; i++) {
270 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) {
273 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
274 joints[i].locked = 1;
276 if (tutoriallevel != 1 || id == 0) {
277 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
282 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
283 joints[i].locked = 1;
285 if (tutoriallevel != 1 || id == 0) {
286 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
290 terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
291 ReflectVector(&joints[i].velocity, &terrainnormal);
292 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
293 if (!joints[i].locked)
294 damage += findLengthfast(&bounceness) / 4000;
295 if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
297 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
298 joints[i].velocity -= bounceness;
299 if (1 - friction * frictionness > 0)
300 joints[i].velocity *= 1 - friction * frictionness;
302 joints[i].velocity = 0;
304 if (tutoriallevel != 1 || id == 0)
305 if (findLengthfast(&bounceness) > 8000 && breaking) {
306 // FIXME: this crashes because k is not initialized!
307 // to reproduce, type 'wolfie' in console and play a while
308 // I'll just comment it out for now
309 //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
310 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
314 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
316 addEnvSound(*coords, 64);
319 if (findLengthfast(&bounceness) > 2500) {
320 Normalise(&bounceness);
321 bounceness = bounceness * 50;
324 joints[i].velocity += bounceness * elasticity;
326 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
328 joints[i].velocity = joints[i].oldvelocity;
332 if (joints[i].locked == 0)
333 if (findLengthfast(&joints[i].velocity) < 1)
334 joints[i].locked = 1;
336 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
337 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
338 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
340 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
341 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
342 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
343 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);
346 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
347 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
348 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);
349 } else if (findLengthfast(&bounceness) > 500)
350 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
353 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
357 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
358 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
359 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
360 if (k < objects.numobjects && k >= 0)
361 if (objects.possible[k]) {
362 friction = objects.friction[k];
363 XYZ start = joints[i].realoldposition;
364 XYZ end = joints[i].position * (*scale) + *coords;
365 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
366 if (whichhit != -1) {
367 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
368 joints[i].locked = 1;
370 if (tutoriallevel != 1 || id == 0) {
371 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
376 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
377 joints[i].locked = 1;
379 if (tutoriallevel != 1 || id == 0) {
380 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
384 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
385 if (terrainnormal.y > .8)
387 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
388 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
390 joints[i].velocity = joints[i].oldvelocity;
392 if (tutoriallevel != 1 || id == 0)
393 if (findLengthfast(&bounceness) > 4000 && breaking) {
394 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
395 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
399 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
401 addEnvSound(*coords, 64);
403 if (objects.type[k] == treetrunktype) {
404 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
405 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
406 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
407 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
409 if (!joints[i].locked)
410 damage += findLengthfast(&bounceness) / 2500;
411 ReflectVector(&joints[i].velocity, &terrainnormal);
412 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
413 joints[i].velocity -= bounceness;
414 if (1 - friction * frictionness > 0)
415 joints[i].velocity *= 1 - friction * frictionness;
417 joints[i].velocity = 0;
418 if (findLengthfast(&bounceness) > 2500) {
419 Normalise(&bounceness);
420 bounceness = bounceness * 50;
422 joints[i].velocity += bounceness * elasticity;
425 if (!joints[i].locked)
426 if (findLengthfast(&joints[i].velocity) < 1) {
427 joints[i].locked = 1;
429 if (findLengthfast(&bounceness) > 500)
430 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
431 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
437 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
440 multiplier = tempmult;
443 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
444 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
445 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
446 if (objects.possible[k]) {
447 for (i = 0; i < 26; i++) {
448 //Make this less stupid
449 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
450 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
451 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
452 if (whichhit != -1) {
453 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
454 for (j = 0; j < num_muscles; j++) {
455 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]))
456 muscles[j].DoConstraint(spinny);
463 for (i = 0; i < num_joints; i++) {
464 switch (joints[i].label) {
480 joints[i].position.y += groundlevel;
482 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)
484 if (joints[i].locked) {
493 for (i = 0; i < num_muscles; i++) {
494 if (muscles[i].type == boneconnect)
495 muscles[i].DoConstraint(0);
503 * applies gravity to the skeleton
506 * Person/Person::DoStuff
508 void Skeleton::DoGravity(float *scale)
511 for (i = 0; i < num_joints; i++) {
514 ((joints[i].label != leftknee) && (joints[i].label != rightknee)) ||
515 (lowforward.y > -.1) ||
518 ((joints[i].label != leftelbow) && (joints[i].label != rightelbow)) ||
522 joints[i].velocity.y += gravity * multiplier / (*scale);
527 * set muscles[which].rotate1
531 * special case if animation == hanganim
533 void Skeleton::FindRotationMuscle(int which, int animation)
538 p1 = muscles[which].parent1->position;
539 p2 = muscles[which].parent2->position;
540 dist = findDistance(&p1, &p2);
541 if (p1.y - p2.y <= dist)
542 muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
543 if (p1.y - p2.y > dist)
544 muscles[which].rotate2 = asin(1.f);
545 muscles[which].rotate2 *= 360.0 / 6.2831853;
549 dist = findDistance(&p1, &p2);
550 if (p1.z - p2.z <= dist)
551 muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
552 if (p1.z - p2.z > dist)
553 muscles[which].rotate1 = acos(1.f);
554 muscles[which].rotate1 *= 360.0 / 6.2831853;
556 muscles[which].rotate1 = 360 - muscles[which].rotate1;
557 if (!isnormal(muscles[which].rotate1))
558 muscles[which].rotate1 = 0;
559 if (!isnormal(muscles[which].rotate2))
560 muscles[which].rotate2 = 0;
562 const int label1 = muscles[which].parent1->label;
563 const int label2 = muscles[which].parent2->label;
566 fwd = specialforward[0];
572 fwd = specialforward[1];
578 fwd = specialforward[2];
584 fwd = specialforward[3];
590 fwd = specialforward[4];
593 if (muscles[which].parent1->lower)
600 if (animation == hanganim) {
601 if (label1 == righthand || label2 == righthand) {
605 if (label1 == lefthand || label2 == lefthand) {
612 if (label1 == rightfoot || label2 == rightfoot) {
615 if (label1 == leftfoot || label2 == leftfoot) {
620 fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
621 fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
623 fwd /= findLength(&fwd);
624 if (fwd.z <= 1 && fwd.z >= -1)
625 muscles[which].rotate3 = acos(0 - fwd.z);
627 muscles[which].rotate3 = acos(-1.f);
628 muscles[which].rotate3 *= 360.0 / 6.2831853;
630 muscles[which].rotate3 = 360 - muscles[which].rotate3;
631 if (!isnormal(muscles[which].rotate3))
632 muscles[which].rotate3 = 0;
637 * takes filenames for three skeleton files and various models
639 void Skeleton::Load(const std::string& filename, const std::string& lowfilename, const std::string& clothesfilename,
640 const std::string& modelfilename, const std::string& model2filename,
641 const std::string& model3filename, const std::string& model4filename,
642 const std::string& model5filename, const std::string& model6filename,
643 const std::string& model7filename, const std::string& modellowfilename,
644 const std::string& modelclothesfilename, bool clothes)
657 // load various models
658 // rotate, scale, do normals, do texcoords for each as needed
660 model[0].loadnotex(modelfilename);
661 model[1].loadnotex(model2filename);
662 model[2].loadnotex(model3filename);
663 model[3].loadnotex(model4filename);
664 model[4].loadnotex(model5filename);
665 model[5].loadnotex(model6filename);
666 model[6].loadnotex(model7filename);
668 for (i = 0; i < num_models; i++) {
669 model[i].Rotate(180, 0, 0);
670 model[i].Scale(.04, .04, .04);
671 model[i].CalculateNormals(0);
674 drawmodel.load(modelfilename, 0);
675 drawmodel.Rotate(180, 0, 0);
676 drawmodel.Scale(.04, .04, .04);
677 drawmodel.FlipTexCoords();
678 if ((tutoriallevel == 1) && (id != 0)) {
679 drawmodel.UniformTexCoords();
680 drawmodel.ScaleTexCoords(0.1);
682 drawmodel.CalculateNormals(0);
684 modellow.loadnotex(modellowfilename);
685 modellow.Rotate(180, 0, 0);
686 modellow.Scale(.04, .04, .04);
687 modellow.CalculateNormals(0);
689 drawmodellow.load(modellowfilename, 0);
690 drawmodellow.Rotate(180, 0, 0);
691 drawmodellow.Scale(.04, .04, .04);
692 drawmodellow.FlipTexCoords();
693 if (tutoriallevel == 1 && id != 0)
694 drawmodellow.UniformTexCoords();
695 if (tutoriallevel == 1 && id != 0)
696 drawmodellow.ScaleTexCoords(0.1);
697 drawmodellow.CalculateNormals(0);
700 modelclothes.loadnotex(modelclothesfilename);
701 modelclothes.Rotate(180, 0, 0);
702 modelclothes.Scale(.041, .04, .041);
703 modelclothes.CalculateNormals(0);
705 drawmodelclothes.load(modelclothesfilename, 0);
706 drawmodelclothes.Rotate(180, 0, 0);
707 drawmodelclothes.Scale(.04, .04, .04);
708 drawmodelclothes.FlipTexCoords();
709 drawmodelclothes.CalculateNormals(0);
712 // FIXME: three similar blocks follow, one for each of:
713 // filename, lowfilename, clothesfilename
717 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
720 funpackf(tfile, "Bi", &num_joints);
724 delete [] joints; //dealloc2(joints);
725 joints = (Joint*)new Joint[num_joints];
727 // read info for each joint
728 for (i = 0; i < num_joints; i++) {
729 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);
730 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
731 funpackf(tfile, "Bi", &joints[i].modelnum);
732 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
733 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
734 funpackf(tfile, "Bb", &joints[i].lower);
735 funpackf(tfile, "Bi", &parentID);
736 if (joints[i].hasparent)
737 joints[i].parent = &joints[parentID];
738 joints[i].velocity = 0;
739 joints[i].oldposition = joints[i].position;
740 joints[i].startpos = joints[i].position;
744 funpackf(tfile, "Bi", &num_muscles);
748 delete [] muscles; //dealloc2(muscles);
749 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
751 // for each muscle...
752 for (i = 0; i < num_muscles; i++) {
753 muscles[i].load(tfile, model[0].vertexNum, joints);
756 // read forwardjoints (?)
757 for (j = 0; j < 3; j++) {
758 funpackf(tfile, "Bi", &forwardjoints[j]);
760 // read lowforwardjoints (?)
761 for (j = 0; j < 3; j++) {
762 funpackf(tfile, "Bi", &lowforwardjoints[j]);
766 for (j = 0; j < num_muscles; j++) {
767 for (i = 0; i < muscles[j].vertices.size(); i++) {
768 for (int k = 0; k < num_models; k++) {
769 if (muscles[j].vertices[i] < model[k].vertexNum)
770 model[k].owner[muscles[j].vertices[i]] = j;
775 // calculate some stuff
777 for (i = 0; i < num_muscles; i++) {
778 FindRotationMuscle(i, -1);
780 // this seems to use opengl purely for matrix calculations
781 for (int k = 0; k < num_models; k++) {
782 for (i = 0; i < model[k].vertexNum; i++) {
783 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
784 glMatrixMode(GL_MODELVIEW);
787 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
788 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
789 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
790 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
791 glGetFloatv(GL_MODELVIEW_MATRIX, M);
792 model[k].vertex[i].x = M[12] * 1;
793 model[k].vertex[i].y = M[13] * 1;
794 model[k].vertex[i].z = M[14] * 1;
797 model[k].CalculateNormals(0);
803 tfile = Folders::openMandatoryFile( Folders::getResourcePath(lowfilename), "rb" );
805 // skip joints section
807 lSize = sizeof(num_joints);
808 fseek(tfile, lSize, SEEK_CUR);
809 for (i = 0; i < num_joints; i++) {
823 fseek(tfile, lSize, SEEK_CUR);
827 funpackf(tfile, "Bi", &num_muscles);
829 for (i = 0; i < num_muscles; i++) {
831 lSize = sizeof(float)
837 fseek(tfile, lSize, SEEK_CUR);
839 muscles[i].loadVerticesLow(tfile, modellow.vertexNum);
842 lSize = 1; //sizeof(bool);
843 fseek ( tfile, lSize, SEEK_CUR);
845 fseek ( tfile, lSize, SEEK_CUR);
846 fseek ( tfile, lSize, SEEK_CUR);
849 for (j = 0; j < num_muscles; j++) {
850 for (i = 0; i < muscles[j].verticeslow.size(); i++) {
851 if (muscles[j].verticeslow[i] < modellow.vertexNum)
852 modellow.owner[muscles[j].verticeslow[i]] = j;
856 // use opengl for its matrix math
857 for (i = 0; i < modellow.vertexNum; i++) {
858 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
859 glMatrixMode(GL_MODELVIEW);
862 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
863 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
864 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
865 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
866 glGetFloatv(GL_MODELVIEW_MATRIX, M);
867 modellow.vertex[i].x = M[12];
868 modellow.vertex[i].y = M[13];
869 modellow.vertex[i].z = M[14];
873 modellow.CalculateNormals(0);
878 tfile = Folders::openMandatoryFile( Folders::getResourcePath(clothesfilename), "rb" );
881 lSize = sizeof(num_joints);
882 fseek ( tfile, lSize, SEEK_CUR);
884 for (i = 0; i < num_joints; i++) {
898 fseek(tfile, lSize, SEEK_CUR);
902 funpackf(tfile, "Bi", &num_muscles);
904 for (i = 0; i < num_muscles; i++) {
906 lSize = sizeof(float)
912 fseek(tfile, lSize, SEEK_CUR);
914 muscles[i].loadVerticesClothes(tfile, modelclothes.vertexNum);
917 lSize = 1; //sizeof(bool);
918 fseek ( tfile, lSize, SEEK_CUR);
920 fseek ( tfile, lSize, SEEK_CUR);
921 fseek ( tfile, lSize, SEEK_CUR);
926 for (j = 0; j < num_muscles; j++) {
927 for (i = 0; i < muscles[j].verticesclothes.size(); i++) {
928 if (muscles[j].verticesclothes.size() && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
929 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
933 // use opengl for its matrix math
934 for (i = 0; i < modelclothes.vertexNum; i++) {
935 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
936 glMatrixMode(GL_MODELVIEW);
939 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
940 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
941 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
942 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
943 glGetFloatv(GL_MODELVIEW_MATRIX, M);
944 modelclothes.vertex[i].x = M[12];
945 modelclothes.vertex[i].y = M[13];
946 modelclothes.vertex[i].z = M[14];
950 modelclothes.CalculateNormals(0);
954 for (i = 0; i < num_joints; i++) {
955 for (j = 0; j < num_joints; j++) {
956 if (joints[i].label == j)
966 // the following functions are not used anywhere
969 * sets forward, lowforward, specialforward[]
974 void Skeleton::FindForwardsfirst()
976 //Find forward vectors
977 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
980 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
981 Normalise(&lowforward);
984 specialforward[0] = forward;
985 specialforward[1] = forward;
986 specialforward[2] = forward;
987 specialforward[3] = forward;
988 specialforward[4] = forward;
997 void Skeleton::Draw(int muscleview)
999 static float jointcolor[4];
1001 if (muscleview == 2) {
1013 //Calc motionblur-ness
1014 for (int i = 0; i < num_joints; i++) {
1015 joints[i].oldposition = joints[i].position;
1016 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1017 if (joints[i].blurred < 1)
1018 joints[i].blurred = 1;
1024 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1026 for (int i = 0; i < num_joints; i++) {
1027 if (joints[i].hasparent) {
1028 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1029 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1030 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1031 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1032 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1033 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1034 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1035 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1038 for (int i = 0; i < num_muscles; i++) {
1039 if (muscles[i].type == boneconnect) {
1040 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1041 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1042 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1043 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1044 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1045 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1046 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1047 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1053 for (int i = 0; i < num_joints; i++) {
1054 if (joints[i].hasparent) {
1055 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1056 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1057 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1058 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1061 for (int i = 0; i < num_muscles; i++) {
1062 if (muscles[i].type == boneconnect) {
1063 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1064 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1065 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1066 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1069 glColor3f(.6, .6, 0);
1070 if (muscleview == 1)
1071 for (int i = 0; i < num_muscles; i++) {
1072 if (muscles[i].type != boneconnect) {
1073 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1074 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1079 if (muscleview != 2) {
1082 for (int i = 0; i < num_joints; i++) {
1084 glColor4f(0, 0, .5, 1);
1086 glColor4f(1, 1, 0, 1);
1087 if (joints[i].locked && i != selected)
1088 glColor4f(1, 0, 0, 1);
1089 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1094 //Set old position to current position
1095 if (muscleview == 2)
1096 for (int i = 0; i < num_joints; i++) {
1097 joints[i].oldposition = joints[i].position;
1107 void Skeleton::AddJoint(float x, float y, float z, int which)
1109 if (num_joints < max_joints - 1) {
1110 joints[num_joints].velocity = 0;
1111 joints[num_joints].position.x = x;
1112 joints[num_joints].position.y = y;
1113 joints[num_joints].position.z = z;
1114 joints[num_joints].mass = 1;
1115 joints[num_joints].locked = 0;
1117 joints[num_joints].hasparent = 0;
1119 if (which < num_joints && which >= 0)
1120 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1129 void Skeleton::DeleteJoint(int whichjoint)
1131 if (whichjoint < num_joints && whichjoint >= 0) {
1132 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1133 joints[whichjoint].position = joints[num_joints - 1].position;
1134 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1135 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1136 joints[whichjoint].parent = joints[num_joints - 1].parent;
1137 joints[whichjoint].length = joints[num_joints - 1].length;
1138 joints[whichjoint].locked = joints[num_joints - 1].locked;
1139 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1140 joints[whichjoint].visible = joints[num_joints - 1].visible;
1142 for (int i = 0; i < num_muscles; i++) {
1143 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1144 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1146 for (int i = 0; i < num_muscles; i++) {
1147 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1148 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1150 for (int i = 0; i < num_joints; i++) {
1151 if (joints[i].parent == &joints[whichjoint])
1152 joints[i].hasparent = 0;
1154 for (int i = 0; i < num_joints; i++) {
1155 if (joints[i].parent == &joints[num_joints - 1])
1156 joints[i].parent = &joints[whichjoint];
1166 * Skeleton::DeleteJoint - UNUSED
1168 void Skeleton::DeleteMuscle(int whichmuscle)
1170 if (whichmuscle < num_muscles) {
1171 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1172 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1173 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1174 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1175 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1176 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1177 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1178 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1179 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1190 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1192 if (whichjoint < num_joints) {
1193 joints[whichjoint].velocity = 0;
1194 joints[whichjoint].position.x = x;
1195 joints[whichjoint].position.y = y;
1196 joints[whichjoint].position.z = z;
1198 if (which >= num_joints || which < 0)
1199 joints[whichjoint].hasparent = 0;
1200 if (which < num_joints && which >= 0) {
1201 joints[whichjoint].parent = &joints[which];
1202 joints[whichjoint].hasparent = 1;
1203 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1211 * Skeleton::AddJoint - UNUSED
1213 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1215 const int max_muscles = 100; // FIXME: Probably can be dropped
1216 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1217 muscles[num_muscles].parent1 = &joints[attach1];
1218 muscles[num_muscles].parent2 = &joints[attach2];
1219 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1220 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1221 muscles[num_muscles].strength = .7;
1222 muscles[num_muscles].type = type;
1223 muscles[num_muscles].minlength = minlength;
1224 muscles[num_muscles].maxlength = maxlength;
1235 void Skeleton::MusclesSet()
1237 for (int i = 0; i < num_muscles; i++) {
1238 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1247 void Skeleton::DoBalance()
1250 newpoint=joints[0].position;
1251 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1252 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1253 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1254 //Move child point to within certain distance of parent point
1255 joints[0].position=newpoint;