2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 /**> HEADER FILES <**/
25 #include "openal_wrapper.h"
26 #include "Animation.h"
28 extern float multiplier;
30 extern Skeleton testskeleton;
31 extern Terrain terrain;
32 extern Objects objects;
33 extern int environment;
34 extern float camerashake;
37 extern XYZ envsound[30];
38 extern float envsoundvol[30];
39 extern int numenvsounds;
40 extern float envsoundlife[30];
41 extern int tutoriallevel;
43 extern int whichjointstartarray[26];
44 extern int whichjointendarray[26];
46 extern bool visibleloading;
50 void dealloc2(void* param)
53 param = 0; // FIXME: does this *do* anything???
56 enum {boneconnect, constraint, muscle};
60 * sets strength, length,
61 * parent1->position, parent2->position,
62 * parent1->velocity, parent2->velocity
66 * Skeleton::DoConstraints
68 void Muscle::DoConstraint(bool spinny)
70 // FIXME: relaxlength shouldn't be static, but may not always be set
71 // so I don't want to change the existing behavior even though it's probably a bug
72 static float relaxlength;
74 float oldlength = length;
76 if (type != boneconnect)
77 relaxlength = findDistance(&parent1->position, &parent2->position);
79 if (type == boneconnect)
81 if (type == constraint)
90 length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
91 length -= (length - targetlength) * (strength) * multiplier * 10000;
95 if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
99 if (length < minlength)
101 if (length > maxlength)
104 if (length == relaxlength)
110 XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
112 //Find vector from midpoint to second vector
113 XYZ vel = parent2->position - midp;
115 //Change to unit vector
118 //Apply velocity change
119 XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
120 XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
121 if (!freeze && spinny) {
122 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
123 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
125 parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
126 parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
129 //Move child point to within certain distance of parent point
130 parent1->position = newpoint1;
131 parent2->position = newpoint2;
135 * sets forward, lowforward, specialforward[]
139 * Person/Person::DoAnimations
140 * Person/Person::DrawSkeleton
142 void Skeleton::FindForwards()
144 //Find forward vectors
145 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
148 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
149 Normalise(&lowforward);
152 specialforward[0] = forward;
154 specialforward[1] = jointPos(rightshoulder) + jointPos(rightwrist);
155 specialforward[1] = jointPos(rightelbow) - specialforward[1] / 2;
156 specialforward[1] += forward * .4;
157 Normalise(&specialforward[1]);
158 specialforward[2] = jointPos(leftshoulder) + jointPos(leftwrist);
159 specialforward[2] = jointPos(leftelbow) - specialforward[2] / 2;
160 specialforward[2] += forward * .4;
161 Normalise(&specialforward[2]);
163 specialforward[3] = jointPos(righthip) + jointPos(rightankle);
164 specialforward[3] = specialforward[3] / 2 - jointPos(rightknee);
165 specialforward[3] += lowforward * .4;
166 Normalise(&specialforward[3]);
167 specialforward[4] = jointPos(lefthip) + jointPos(leftankle);
168 specialforward[4] = specialforward[4] / 2 - jointPos(leftknee);
169 specialforward[4] += lowforward * .4;
170 Normalise(&specialforward[4]);
177 * Person/Person::RagDoll
178 * Person/Person::DoStuff
181 float Skeleton::DoConstraints(XYZ *coords, float *scale)
183 float friction = 1.5;
184 const float elasticity = .3;
186 const int numrepeats = 3;
187 float groundlevel = .15;
196 float damage = 0; // eventually returned from function
197 bool breaking = false;
200 freetime += multiplier;
202 whichpatchx = coords->x / (terrain.size / subdivision * terrain.scale);
203 whichpatchz = coords->z / (terrain.size / subdivision * terrain.scale);
205 terrainlight = *coords;
206 objects.SphereCheckPossible(&terrainlight, 1);
209 for (i = 0; i < num_joints; i++) {
210 joints[i].position = joints[i].position + joints[i].velocity * multiplier;
212 switch (joints[i].label) {
229 joints[i].position.y -= groundlevel;
230 joints[i].oldvelocity = joints[i].velocity;
233 float tempmult = multiplier;
234 //multiplier/=numrepeats;
236 for (j = 0; j < numrepeats; j++) {
238 // right leg constraints?
239 if (!joint(rightknee).locked && !joint(righthip).locked) {
240 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
241 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(righthip), &jointPos(rightankle), &jointPos(rightknee), &r)) {
242 jointPos(rightknee) -= lowforward * .05;
244 jointVel(rightknee) -= lowforward * .05 / multiplier / 4;
246 jointVel(rightknee) -= lowforward * .05;
247 jointPos(rightankle) += lowforward * .025;
249 jointVel(rightankle) += lowforward * .025 / multiplier / 4;
251 jointVel(rightankle) += lowforward * .25;
252 jointPos(righthip) += lowforward * .025;
254 jointVel(righthip) += lowforward * .025 / multiplier / 4;
256 jointVel(righthip) += lowforward * .025;
257 temp = jointPos(rightknee) - (jointPos(righthip) + jointPos(rightankle)) / 2;
261 // left leg constraints?
262 if (!joint(leftknee).locked && !joint(lefthip).locked) {
263 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
264 while (normaldotproduct(temp, lowforward) > -.1 && !sphere_line_intersection(&jointPos(lefthip), &jointPos(leftankle), &jointPos(leftknee), &r)) {
265 jointPos(leftknee) -= lowforward * .05;
267 jointVel(leftknee) -= lowforward * .05 / multiplier / 4;
269 jointVel(leftknee) -= lowforward * .05;
270 jointPos(leftankle) += lowforward * .025;
272 jointVel(leftankle) += lowforward * .025 / multiplier / 4;
274 jointVel(leftankle) += lowforward * .25;
275 jointPos(lefthip) += lowforward * .025;
277 jointVel(lefthip) += lowforward * .025 / multiplier / 4;
279 jointVel(lefthip) += lowforward * .025;
280 temp = jointPos(leftknee) - (jointPos(lefthip) + jointPos(leftankle)) / 2;
284 for (i = 0; i < num_joints; i++) {
285 if (joints[i].locked && !spinny && findLengthfast(&joints[i].velocity) > 320)
286 joints[i].locked = 0;
287 if (spinny && findLengthfast(&joints[i].velocity) > 600)
288 joints[i].locked = 0;
289 if (joints[i].delay > 0) {
291 for (j = 0; j < num_joints; j++) {
292 if (joints[j].locked)
296 joints[i].delay -= multiplier * 3;
301 for (i = 0; i < num_muscles; i++) {
303 muscles[i].DoConstraint(spinny);
306 for (i = 0; i < num_joints; i++) {
310 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) {
313 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
314 joints[i].locked = 1;
316 if (tutoriallevel != 1 || id == 0) {
317 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
322 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
323 joints[i].locked = 1;
325 if (tutoriallevel != 1 || id == 0) {
326 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
330 terrainnormal = terrain.getNormal(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
331 ReflectVector(&joints[i].velocity, &terrainnormal);
332 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
333 if (!joints[i].locked)
334 damage += findLengthfast(&bounceness) / 4000;
335 if (findLengthfast(&joints[i].velocity) < findLengthfast(&bounceness))
337 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
338 joints[i].velocity -= bounceness;
339 if (1 - friction * frictionness > 0)
340 joints[i].velocity *= 1 - friction * frictionness;
342 joints[i].velocity = 0;
344 if (tutoriallevel != 1 || id == 0)
345 if (findLengthfast(&bounceness) > 8000 && breaking) {
346 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
347 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
351 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
353 envsound[numenvsounds] = *coords;
354 envsoundvol[numenvsounds] = 64;
355 envsoundlife[numenvsounds] = .4;
359 if (findLengthfast(&bounceness) > 2500) {
360 Normalise(&bounceness);
361 bounceness = bounceness * 50;
364 joints[i].velocity += bounceness * elasticity;
366 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
368 joints[i].velocity = joints[i].oldvelocity;
372 if (joints[i].locked == 0)
373 if (findLengthfast(&joints[i].velocity) < 1)
374 joints[i].locked = 1;
376 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
377 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
378 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
380 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
381 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
382 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
383 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);
386 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
387 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
388 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);
389 } else if (findLengthfast(&bounceness) > 500)
390 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
393 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
397 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
398 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
399 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
400 if (k < objects.numobjects && k >= 0)
401 if (objects.possible[k]) {
402 friction = objects.friction[k];
403 XYZ start = joints[i].realoldposition;
404 XYZ end = joints[i].position * (*scale) + *coords;
405 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
406 if (whichhit != -1) {
407 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
408 joints[i].locked = 1;
410 if (tutoriallevel != 1 || id == 0) {
411 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
416 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
417 joints[i].locked = 1;
419 if (tutoriallevel != 1 || id == 0) {
420 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
424 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
425 if (terrainnormal.y > .8)
427 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
428 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
430 joints[i].velocity = joints[i].oldvelocity;
432 if (tutoriallevel != 1 || id == 0)
433 if (findLengthfast(&bounceness) > 4000 && breaking) {
434 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
435 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
439 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
441 envsound[numenvsounds] = *coords;
442 envsoundvol[numenvsounds] = 64;
443 envsoundlife[numenvsounds] = .4;
446 if (objects.type[k] == treetrunktype) {
447 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
448 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
449 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
450 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
452 if (!joints[i].locked)
453 damage += findLengthfast(&bounceness) / 2500;
454 ReflectVector(&joints[i].velocity, &terrainnormal);
455 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
456 joints[i].velocity -= bounceness;
457 if (1 - friction * frictionness > 0)
458 joints[i].velocity *= 1 - friction * frictionness;
460 joints[i].velocity = 0;
461 if (findLengthfast(&bounceness) > 2500) {
462 Normalise(&bounceness);
463 bounceness = bounceness * 50;
465 joints[i].velocity += bounceness * elasticity;
468 if (!joints[i].locked)
469 if (findLengthfast(&joints[i].velocity) < 1) {
470 joints[i].locked = 1;
472 if (findLengthfast(&bounceness) > 500)
473 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
474 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
480 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
483 multiplier = tempmult;
486 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
487 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
488 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
489 if (objects.possible[k]) {
490 for (i = 0; i < 26; i++) {
491 //Make this less stupid
492 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
493 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
494 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
495 if (whichhit != -1) {
496 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
497 for (j = 0; j < num_muscles; j++) {
498 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]))
499 muscles[j].DoConstraint(spinny);
506 for (i = 0; i < num_joints; i++) {
507 switch (joints[i].label) {
523 joints[i].position.y += groundlevel;
525 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)
527 if (joints[i].locked) {
536 for (i = 0; i < num_muscles; i++) {
537 if (muscles[i].type == boneconnect)
538 muscles[i].DoConstraint(0);
546 * applies gravity to the skeleton
549 * Person/Person::DoStuff
551 void Skeleton::DoGravity(float *scale)
554 for (i = 0; i < num_joints; i++) {
555 if (((joints[i].label != leftknee && joints[i].label != rightknee) || lowforward.y > -.1 || joints[i].mass < 5) && ((joints[i].label != rightelbow && joints[i].label != rightelbow) || forward.y < .3))
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;
670 * load an animation from file
672 void Animation::Load(const char *filename, int aheight, int aattack)
676 XYZ startoffset, endoffset;
679 const char *anim_prefix = ":Data:Animations:";
684 // concatenate anim_prefix + filename
685 int len = strlen(anim_prefix) + strlen(filename);
686 char *buf = new char[len + 1];
687 snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
688 // Changing the filename into something the OS can understand
689 char *fixedFN = ConvertFileName(buf);
692 LOG(std::string("Loading animation...") + fixedFN);
694 // clear existing data
701 Game::LoadingScreen();
703 // read file in binary mode
704 tfile = fopen( fixedFN, "rb" );
706 // read numframes, joints to know how much memory to allocate
707 funpackf(tfile, "Bi Bi", &numframes, &joints);
709 for(i = 0; i < joints; i++){
710 if(position[i])dealloc2(position[i]);
711 if(twist[i])dealloc2(twist[i]);
712 if(twist2[i])dealloc2(twist2[i]);
713 if(onground[i])dealloc2(onground[i]);
716 if(position)dealloc2(position);
717 if(twist)dealloc2(twist);
718 if(twist2)dealloc2(twist2);
719 if(speed)dealloc2(speed);
720 if(onground)dealloc2(onground);
721 if(forward)dealloc2(forward);
722 if(weapontarget)dealloc2(weapontarget);
723 if(label)dealloc2(label);*/
726 // allocate memory for everything
728 position = (XYZ**)malloc(sizeof(XYZ*) * joints);
729 for (i = 0; i < joints; i++)
730 position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
732 twist = (float**)malloc(sizeof(float*) * joints);
733 for (i = 0; i < joints; i++)
734 twist[i] = (float*)malloc(sizeof(float) * numframes);
736 twist2 = (float**)malloc(sizeof(float*) * joints);
737 for (i = 0; i < joints; i++)
738 twist2[i] = (float*)malloc(sizeof(float) * numframes);
740 speed = (float*)malloc(sizeof(float) * numframes);
742 onground = (bool**)malloc(sizeof(bool*) * joints);
743 for (i = 0; i < joints; i++)
744 onground[i] = (bool*)malloc(sizeof(bool) * numframes);
746 forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
747 weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
748 label = (int*)malloc(sizeof(int) * numframes);
750 /*position = new XYZ[joints][numframes];
751 twist = new float[joints][numframes];
752 twist2 = new float[joints][numframes];
753 speed = new float[numframes];
754 onground = new bool[joints][numframes];
755 forward = new XYZ[numframes];
756 label = new int[numframes];*/
759 // read binary data as animation
762 for (i = 0; i < numframes; i++) {
763 // for each joint in the skeleton...
764 for (j = 0; j < joints; j++) {
765 // read joint position
766 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
768 for (j = 0; j < joints; j++) {
770 funpackf(tfile, "Bf", &twist[j][i]);
772 for (j = 0; j < joints; j++) {
773 // read onground (boolean)
775 funpackf(tfile, "Bb", &uch);
776 onground[j][i] = (uch != 0);
778 // read frame speed (?)
779 funpackf(tfile, "Bf", &speed[i]);
781 // read twist2 for whole animation
782 for (i = 0; i < numframes; i++) {
783 for (j = 0; j < joints; j++) {
784 funpackf(tfile, "Bf", &twist2[j][i]);
787 // read label for each frame
788 for (i = 0; i < numframes; i++) {
789 funpackf(tfile, "Bf", &label[i]);
791 // read weapontargetnum
792 funpackf(tfile, "Bi", &weapontargetnum);
793 // read weapontarget positions for each frame
794 for (i = 0; i < numframes; i++) {
795 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
803 // find average position of certain joints on first and last frames
804 // and save in startoffset, endoffset
805 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
806 for (j = 0; j < joints; j++) {
807 if (position[j][0].y < 1)
808 startoffset += position[j][0];
809 if (position[j][numframes - 1].y < 1)
810 endoffset += position[j][numframes - 1];
812 startoffset /= joints;
821 * takes filenames for three skeleton files and various models
823 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
824 const char *modelfilename, const char *model2filename,
825 const char *model3filename, const char *model4filename,
826 const char *model5filename, const char *model6filename,
827 const char *model7filename, const char *modellowfilename,
828 const char *modelclothesfilename, bool clothes)
834 int i, j, tempmuscle;
846 // load various models
847 // rotate, scale, do normals, do texcoords for each as needed
849 model[0].loadnotex(modelfilename);
850 model[1].loadnotex(model2filename);
851 model[2].loadnotex(model3filename);
852 model[3].loadnotex(model4filename);
853 model[4].loadnotex(model5filename);
854 model[5].loadnotex(model6filename);
855 model[6].loadnotex(model7filename);
857 for (i = 0; i < num_models; i++) {
858 model[i].Rotate(180, 0, 0);
859 model[i].Scale(.04, .04, .04);
860 model[i].CalculateNormals(0);
863 drawmodel.load(modelfilename, 0);
864 drawmodel.Rotate(180, 0, 0);
865 drawmodel.Scale(.04, .04, .04);
866 drawmodel.FlipTexCoords();
867 if (tutoriallevel == 1 && id != 0)
868 drawmodel.UniformTexCoords();
869 if (tutoriallevel == 1 && id != 0)
870 drawmodel.ScaleTexCoords(0.1);
871 drawmodel.CalculateNormals(0);
873 modellow.loadnotex(modellowfilename);
874 modellow.Rotate(180, 0, 0);
875 modellow.Scale(.04, .04, .04);
876 modellow.CalculateNormals(0);
878 drawmodellow.load(modellowfilename, 0);
879 drawmodellow.Rotate(180, 0, 0);
880 drawmodellow.Scale(.04, .04, .04);
881 drawmodellow.FlipTexCoords();
882 if (tutoriallevel == 1 && id != 0)
883 drawmodellow.UniformTexCoords();
884 if (tutoriallevel == 1 && id != 0)
885 drawmodellow.ScaleTexCoords(0.1);
886 drawmodellow.CalculateNormals(0);
889 modelclothes.loadnotex(modelclothesfilename);
890 modelclothes.Rotate(180, 0, 0);
891 modelclothes.Scale(.041, .04, .041);
892 modelclothes.CalculateNormals(0);
894 drawmodelclothes.load(modelclothesfilename, 0);
895 drawmodelclothes.Rotate(180, 0, 0);
896 drawmodelclothes.Scale(.04, .04, .04);
897 drawmodelclothes.FlipTexCoords();
898 drawmodelclothes.CalculateNormals(0);
901 // FIXME: three similar blocks follow, one for each of:
902 // filename, lowfilename, clothesfilename
906 tfile = fopen( ConvertFileName(filename), "rb" );
908 if (1) { // FIXME: should this be if(tfile) ?
910 funpackf(tfile, "Bi", &num_joints);
913 //joints.resize(num_joints);
915 delete [] joints; //dealloc2(joints);
916 joints = (Joint*)new Joint[num_joints]; //malloc(sizeof(Joint)*num_joints);
918 // read info for each joint
919 for (i = 0; i < num_joints; i++) {
920 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);
921 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
922 funpackf(tfile, "Bi", &joints[i].modelnum);
923 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
924 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
925 funpackf(tfile, "Bb", &joints[i].lower);
926 funpackf(tfile, "Bi", &parentID);
927 if (joints[i].hasparent)
928 joints[i].parent = &joints[parentID];
929 joints[i].velocity = 0;
930 joints[i].oldposition = joints[i].position;
934 tempmuscle = num_muscles;
935 funpackf(tfile, "Bi", &num_muscles);
940 delete [] muscles; //dealloc2(muscles);
941 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
945 // for each muscle...
946 for (i = 0; i < num_muscles; i++) {
948 tempmuscle = muscles[i].numvertices;
949 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);
951 // allocate memory for vertices
952 //muscles[i].vertices.clear();
953 //muscles[i].vertices.resize(muscles[i].numvertices);
954 //if(muscles[i].vertices)dealloc2(muscles[i].vertices);
955 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
959 for (j = 0; j < muscles[i].numvertices - edit; j++) {
960 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
961 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
962 muscles[i].numvertices--;
968 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
969 muscles[i].parent1 = &joints[parentID];
970 funpackf(tfile, "Bi", &parentID);
971 muscles[i].parent2 = &joints[parentID];
974 // read forwardjoints (?)
975 for (j = 0; j < 3; j++) {
976 funpackf(tfile, "Bi", &forwardjoints[j]);
978 // read lowforwardjoints (?)
979 for (j = 0; j < 3; j++) {
980 funpackf(tfile, "Bi", &lowforwardjoints[j]);
984 for (j = 0; j < num_muscles; j++) {
985 for (i = 0; i < muscles[j].numvertices; i++) {
986 for (int k = 0; k < num_models; k++) {
987 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
988 model[k].owner[muscles[j].vertices[i]] = j;
993 // calculate some stuff
995 for (i = 0; i < num_joints; i++) {
996 joints[i].startpos = joints[i].position;
998 for (i = 0; i < num_muscles; i++) {
999 FindRotationMuscle(i, -1);
1001 // this seems to use opengl purely for matrix calculations
1002 for (int k = 0; k < num_models; k++) {
1003 for (i = 0; i < model[k].vertexNum; i++) {
1004 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
1005 glMatrixMode(GL_MODELVIEW);
1008 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
1009 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
1010 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
1011 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
1012 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1013 model[k].vertex[i].x = M[12] * 1;
1014 model[k].vertex[i].y = M[13] * 1;
1015 model[k].vertex[i].z = M[14] * 1;
1018 model[k].CalculateNormals(0);
1025 tfile = fopen( ConvertFileName(lowfilename), "rb" );
1027 if (1) { // FIXME: should this be if(tfile) ?
1028 // skip joints section
1030 lSize = sizeof(num_joints);
1031 fseek(tfile, lSize, SEEK_CUR);
1032 //joints = new Joint[num_joints];
1033 //jointlabels = new int[num_joints];
1034 for (i = 0; i < num_joints; i++) {
1048 fseek(tfile, lSize, SEEK_CUR);
1050 if (joints[i].hasparent)
1051 joints[i].parent = &joints[parentID];
1052 joints[i].velocity = 0;
1053 joints[i].oldposition = joints[i].position;
1057 funpackf(tfile, "Bi", &num_muscles);
1058 //muscles = new Muscle[num_muscles];
1060 for (i = 0; i < num_muscles; i++) {
1062 lSize = sizeof(float)
1068 fseek(tfile, lSize, SEEK_CUR);
1070 // read numverticeslow
1071 tempmuscle = muscles[i].numverticeslow;
1072 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1074 if (muscles[i].numverticeslow) {
1076 //muscles[i].verticeslow.clear();
1077 //muscles[i].verticeslow.resize(muscles[i].numverticeslow);
1078 //if(muscles[i].verticeslow)dealloc2(muscles[i].verticeslow);
1079 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1083 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1084 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1085 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1086 muscles[i].numverticeslow--;
1093 lSize = 1; //sizeof(bool);
1094 fseek ( tfile, lSize, SEEK_CUR);
1095 lSize = sizeof(int);
1096 fseek ( tfile, lSize, SEEK_CUR);
1097 fseek ( tfile, lSize, SEEK_CUR);
1101 lSize = sizeof(int);
1102 for (j = 0; j < num_muscles; j++) {
1103 for (i = 0; i < muscles[j].numverticeslow; i++) {
1104 if (muscles[j].numverticeslow && muscles[j].verticeslow[i] < modellow.vertexNum)
1105 modellow.owner[muscles[j].verticeslow[i]] = j;
1110 for(i=0;i<num_joints;i++){
1111 joints[i].startpos=joints[i].position;
1113 for(i=0;i<num_muscles;i++){
1114 FindRotationMuscle(i,-1);
1117 // use opengl for its matrix math
1118 for (i = 0; i < modellow.vertexNum; i++) {
1119 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1120 glMatrixMode(GL_MODELVIEW);
1123 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1124 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1125 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1126 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1127 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1128 modellow.vertex[i].x = M[12];
1129 modellow.vertex[i].y = M[13];
1130 modellow.vertex[i].z = M[14];
1134 modellow.CalculateNormals(0);
1140 tfile = fopen( ConvertFileName(clothesfilename), "rb" ); // FIXME: where's the check for valid load
1143 lSize = sizeof(num_joints);
1144 fseek ( tfile, lSize, SEEK_CUR);
1145 //joints = new Joint[num_joints];
1146 //jointlabels = new int[num_joints];
1148 for (i = 0; i < num_joints; i++) {
1162 fseek(tfile, lSize, SEEK_CUR);
1164 if (joints[i].hasparent)
1165 joints[i].parent = &joints[parentID];
1166 joints[i].velocity = 0;
1167 joints[i].oldposition = joints[i].position;
1171 funpackf(tfile, "Bi", &num_muscles);
1172 //muscles = new Muscle[num_muscles];
1174 for (i = 0; i < num_muscles; i++) {
1176 lSize = sizeof(float)
1182 fseek(tfile, lSize, SEEK_CUR);
1184 // read numverticesclothes
1185 tempmuscle = muscles[i].numverticesclothes;
1186 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1188 // read verticesclothes
1189 if (muscles[i].numverticesclothes) {
1190 //muscles[i].verticesclothes.clear();
1191 //muscles[i].verticesclothes.resize(muscles[i].numverticesclothes);
1192 //if(muscles[i].verticesclothes)dealloc2(muscles[i].verticesclothes);
1193 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1195 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1196 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1197 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1198 muscles[i].numverticesclothes--;
1205 lSize = 1; //sizeof(bool);
1206 fseek ( tfile, lSize, SEEK_CUR);
1207 lSize = sizeof(int);
1208 fseek ( tfile, lSize, SEEK_CUR);
1209 fseek ( tfile, lSize, SEEK_CUR);
1213 lSize = sizeof(int);
1214 for (j = 0; j < num_muscles; j++) {
1215 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1216 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1217 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1222 for(i=0;i<num_joints;i++){
1223 joints[i].startpos=joints[i].position;
1225 for(i=0;i<num_muscles;i++){
1226 FindRotationMuscle(i,-1);
1229 // use opengl for its matrix math
1230 for (i = 0; i < modelclothes.vertexNum; i++) {
1231 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1232 glMatrixMode(GL_MODELVIEW);
1235 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1236 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1237 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1238 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1239 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1240 modelclothes.vertex[i].x = M[12];
1241 modelclothes.vertex[i].y = M[13];
1242 modelclothes.vertex[i].z = M[14];
1246 modelclothes.CalculateNormals(0);
1250 for (i = 0; i < num_joints; i++) {
1251 for (j = 0; j < num_joints; j++) {
1252 if (joints[i].label == j)
1260 Animation::Animation()
1266 weapontargetnum = 0;
1278 Animation::~Animation()
1283 void Animation::deallocate()
1288 for (i = 0; i < joints; i++)
1289 dealloc2(position[i]);
1296 for (i = 0; i < joints; i++)
1304 for (i = 0; i < joints; i++)
1305 dealloc2(twist2[i]);
1312 for (i = 0; i < joints; i++)
1313 dealloc2(onground[i]);
1328 dealloc2(weapontarget);
1338 Skeleton::Skeleton()
1346 memset(forwardjoints, 0, sizeof(forwardjoints));
1351 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1354 // XYZ specialforward[5];
1355 memset(jointlabels, 0, sizeof(jointlabels));
1359 // Model modelclothes;
1363 // Model drawmodellow;
1364 // Model drawmodelclothes;
1369 memset(skinText, 0, sizeof(skinText));
1386 Skeleton::~Skeleton()
1403 verticesclothes = 0;
1407 numverticesclothes = 0;
1416 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1417 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1418 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1419 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1427 dealloc2(verticeslow);
1428 dealloc2(verticesclothes);
1431 Animation & Animation::operator = (const Animation & ani)
1435 bool allocate = true;
1437 allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1442 numframes = ani.numframes;
1443 height = ani.height;
1444 attack = ani.attack;
1445 joints = ani.joints;
1446 weapontargetnum = ani.weapontargetnum;
1449 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1450 for (i = 0; i < ani.joints; i++) {
1452 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1453 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1457 twist = (float**)malloc(sizeof(float*)*ani.joints);
1458 for (i = 0; i < ani.joints; i++) {
1460 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1461 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1465 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1466 for (i = 0; i < ani.joints; i++) {
1468 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1469 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1473 speed = (float*)malloc(sizeof(float) * ani.numframes);
1474 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1477 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1478 for (i = 0; i < ani.joints; i++) {
1480 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1481 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1485 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1486 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1489 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1490 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1493 label = (int*)malloc(sizeof(int) * ani.numframes);
1494 memcpy(label, ani.label, sizeof(int)*ani.numframes);
1504 // the following functions are not used anywhere
1507 * sets forward, lowforward, specialforward[]
1512 void Skeleton::FindForwardsfirst()
1514 //Find forward vectors
1515 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1516 Normalise(&forward);
1518 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1519 Normalise(&lowforward);
1522 specialforward[0] = forward;
1523 specialforward[1] = forward;
1524 specialforward[2] = forward;
1525 specialforward[3] = forward;
1526 specialforward[4] = forward;
1535 void Skeleton::Draw(int muscleview)
1537 static float jointcolor[4];
1539 if (muscleview != 2) {
1546 if (muscleview == 2) {
1552 //Calc motionblur-ness
1553 for (int i = 0; i < num_joints; i++) {
1554 joints[i].oldposition = joints[i].position;
1555 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1556 if (joints[i].blurred < 1)
1557 joints[i].blurred = 1;
1563 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1565 for (int i = 0; i < num_joints; i++) {
1566 if (joints[i].hasparent) {
1567 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1568 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1569 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1570 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1571 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1572 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1573 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1574 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1577 for (int i = 0; i < num_muscles; i++) {
1578 if (muscles[i].type == boneconnect) {
1579 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1580 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1581 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1582 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1583 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1584 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1585 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1586 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1592 for (int i = 0; i < num_joints; i++) {
1593 if (joints[i].hasparent) {
1594 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1595 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1596 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1597 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1600 /*for(int i=0; i<num_joints; i++){
1601 if(joints[i].hasparent){
1602 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
1603 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
1604 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
1605 glVertex3f(joints[i].position.x+forward.x,joints[i].position.y+forward.y,joints[i].position.z+forward.z);
1608 for (int i = 0; i < num_muscles; i++) {
1609 if (muscles[i].type == boneconnect) {
1610 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1611 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1612 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1613 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1616 glColor3f(.6, .6, 0);
1617 if (muscleview == 1)
1618 for (int i = 0; i < num_muscles; i++) {
1619 if (muscles[i].type != boneconnect) {
1620 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1621 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1626 if (muscleview != 2) {
1629 for (int i = 0; i < num_joints; i++) {
1631 glColor4f(0, 0, .5, 1);
1633 glColor4f(1, 1, 0, 1);
1634 if (joints[i].locked && i != selected)
1635 glColor4f(1, 0, 0, 1);
1636 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1641 //Set old position to current position
1642 if (muscleview == 2)
1643 for (int i = 0; i < num_joints; i++) {
1644 joints[i].oldposition = joints[i].position;
1654 void Skeleton::AddJoint(float x, float y, float z, int which)
1656 if (num_joints < max_joints - 1) {
1657 joints[num_joints].velocity = 0;
1658 joints[num_joints].position.x = x;
1659 joints[num_joints].position.y = y;
1660 joints[num_joints].position.z = z;
1661 joints[num_joints].mass = 1;
1662 joints[num_joints].locked = 0;
1664 /*if(which>=num_joints||which<0)*/
1665 joints[num_joints].hasparent = 0;
1666 /*if(which<num_joints&&which>=0){
1667 joints[num_joints].parent=&joints[which];
1668 joints[num_joints].hasparent=1;
1669 joints[num_joints].length=findDistance(joints[num_joints].position,joints[num_joints].parent->position);
1672 if (which < num_joints && which >= 0)
1673 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1682 void Skeleton::DeleteJoint(int whichjoint)
1684 if (whichjoint < num_joints && whichjoint >= 0) {
1685 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1686 joints[whichjoint].position = joints[num_joints - 1].position;
1687 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1688 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1689 joints[whichjoint].parent = joints[num_joints - 1].parent;
1690 joints[whichjoint].length = joints[num_joints - 1].length;
1691 joints[whichjoint].locked = joints[num_joints - 1].locked;
1692 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1693 joints[whichjoint].visible = joints[num_joints - 1].visible;
1695 for (int i = 0; i < num_muscles; i++) {
1696 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1697 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1699 for (int i = 0; i < num_muscles; i++) {
1700 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1701 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1703 for (int i = 0; i < num_joints; i++) {
1704 if (joints[i].parent == &joints[whichjoint])
1705 joints[i].hasparent = 0;
1707 for (int i = 0; i < num_joints; i++) {
1708 if (joints[i].parent == &joints[num_joints - 1])
1709 joints[i].parent = &joints[whichjoint];
1719 * Skeleton::DeleteJoint - UNUSED
1721 void Skeleton::DeleteMuscle(int whichmuscle)
1723 if (whichmuscle < num_muscles) {
1724 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1725 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1726 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1727 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1728 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1729 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1730 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1731 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1732 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1743 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1745 if (whichjoint < num_joints) {
1746 joints[whichjoint].velocity = 0;
1747 joints[whichjoint].position.x = x;
1748 joints[whichjoint].position.y = y;
1749 joints[whichjoint].position.z = z;
1751 if (which >= num_joints || which < 0)
1752 joints[whichjoint].hasparent = 0;
1753 if (which < num_joints && which >= 0) {
1754 joints[whichjoint].parent = &joints[which];
1755 joints[whichjoint].hasparent = 1;
1756 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1764 * Skeleton::AddJoint - UNUSED
1766 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1768 const int max_muscles = 100; // FIXME: Probably can be dropped
1769 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1770 muscles[num_muscles].parent1 = &joints[attach1];
1771 muscles[num_muscles].parent2 = &joints[attach2];
1772 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1773 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1774 muscles[num_muscles].strength = .7;
1775 muscles[num_muscles].type = type;
1776 muscles[num_muscles].minlength = minlength;
1777 muscles[num_muscles].maxlength = maxlength;
1788 void Skeleton::MusclesSet()
1790 for (int i = 0; i < num_muscles; i++) {
1791 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1800 void Skeleton::DoBalance()
1803 newpoint=joints[0].position;
1804 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1805 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1806 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1807 //Move child point to within certain distance of parent point
1808 joints[0].position=newpoint;