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 // FIXME: this crashes because k is not initialized!
347 // to reproduce, type 'wolfie' in console and play a while
348 // I'll just comment it out for now
349 //objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
350 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
354 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
356 envsound[numenvsounds] = *coords;
357 envsoundvol[numenvsounds] = 64;
358 envsoundlife[numenvsounds] = .4;
362 if (findLengthfast(&bounceness) > 2500) {
363 Normalise(&bounceness);
364 bounceness = bounceness * 50;
367 joints[i].velocity += bounceness * elasticity;
369 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
371 joints[i].velocity = joints[i].oldvelocity;
375 if (joints[i].locked == 0)
376 if (findLengthfast(&joints[i].velocity) < 1)
377 joints[i].locked = 1;
379 if (environment == snowyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
380 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
381 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .7);
383 terrain.MakeDecal(bodyprintdecal, joints[i].position * (*scale) + *coords, .4, .4, 0);
384 } else if (environment == desertenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
385 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
386 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);
389 else if (environment == grassyenvironment && findLengthfast(&bounceness) > 500 && terrain.getOpacity(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) < .2) {
390 terrainlight = terrain.getLighting(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z);
391 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);
392 } else if (findLengthfast(&bounceness) > 500)
393 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, terrainlight.x, terrainlight.y, terrainlight.z, .5, .2);
396 joints[i].position.y = (terrain.getHeight(joints[i].position.x * (*scale) + coords->x, joints[i].position.z * (*scale) + coords->z) + groundlevel - coords->y) / (*scale);
400 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
401 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
402 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
403 if (k < objects.numobjects && k >= 0)
404 if (objects.possible[k]) {
405 friction = objects.friction[k];
406 XYZ start = joints[i].realoldposition;
407 XYZ end = joints[i].position * (*scale) + *coords;
408 whichhit = objects.model[k].LineCheckPossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
409 if (whichhit != -1) {
410 if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) {
411 joints[i].locked = 1;
413 if (tutoriallevel != 1 || id == 0) {
414 emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.);
419 if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) {
420 joints[i].locked = 1;
422 if (tutoriallevel != 1 || id == 0) {
423 emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.);
427 terrainnormal = DoRotation(objects.model[k].facenormals[whichhit], 0, objects.yaw[k], 0) * -1;
428 if (terrainnormal.y > .8)
430 bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
431 if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) {
433 joints[i].velocity = joints[i].oldvelocity;
435 if (tutoriallevel != 1 || id == 0)
436 if (findLengthfast(&bounceness) > 4000 && breaking) {
437 objects.model[k].MakeDecal(breakdecal, DoRotation(temp - objects.position[k], 0, -objects.yaw[k], 0), .4, .5, Random() % 360);
438 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2);
442 emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords);
444 envsound[numenvsounds] = *coords;
445 envsoundvol[numenvsounds] = 64;
446 envsoundlife[numenvsounds] = .4;
449 if (objects.type[k] == treetrunktype) {
450 objects.rotx[k] += joints[i].velocity.x * multiplier * .4;
451 objects.roty[k] += joints[i].velocity.z * multiplier * .4;
452 objects.rotx[k + 1] += joints[i].velocity.x * multiplier * .4;
453 objects.roty[k + 1] += joints[i].velocity.z * multiplier * .4;
455 if (!joints[i].locked)
456 damage += findLengthfast(&bounceness) / 2500;
457 ReflectVector(&joints[i].velocity, &terrainnormal);
458 frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal));
459 joints[i].velocity -= bounceness;
460 if (1 - friction * frictionness > 0)
461 joints[i].velocity *= 1 - friction * frictionness;
463 joints[i].velocity = 0;
464 if (findLengthfast(&bounceness) > 2500) {
465 Normalise(&bounceness);
466 bounceness = bounceness * 50;
468 joints[i].velocity += bounceness * elasticity;
471 if (!joints[i].locked)
472 if (findLengthfast(&joints[i].velocity) < 1) {
473 joints[i].locked = 1;
475 if (findLengthfast(&bounceness) > 500)
476 Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2);
477 joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005;
483 joints[i].realoldposition = joints[i].position * (*scale) + *coords;
486 multiplier = tempmult;
489 if (terrain.patchobjectnum[whichpatchx][whichpatchz])
490 for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) {
491 k = terrain.patchobjects[whichpatchx][whichpatchz][m];
492 if (objects.possible[k]) {
493 for (i = 0; i < 26; i++) {
494 //Make this less stupid
495 XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords;
496 XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords;
497 whichhit = objects.model[k].LineCheckSlidePossible(&start, &end, &temp, &objects.position[k], &objects.yaw[k]);
498 if (whichhit != -1) {
499 joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale);
500 for (j = 0; j < num_muscles; j++) {
501 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]))
502 muscles[j].DoConstraint(spinny);
509 for (i = 0; i < num_joints; i++) {
510 switch (joints[i].label) {
526 joints[i].position.y += groundlevel;
528 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)
530 if (joints[i].locked) {
539 for (i = 0; i < num_muscles; i++) {
540 if (muscles[i].type == boneconnect)
541 muscles[i].DoConstraint(0);
549 * applies gravity to the skeleton
552 * Person/Person::DoStuff
554 void Skeleton::DoGravity(float *scale)
557 for (i = 0; i < num_joints; i++) {
558 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))
559 joints[i].velocity.y += gravity * multiplier / (*scale);
564 * set muscles[which].rotate1
568 * special case if animation == hanganim
570 void Skeleton::FindRotationMuscle(int which, int animation)
575 p1 = muscles[which].parent1->position;
576 p2 = muscles[which].parent2->position;
577 dist = findDistance(&p1, &p2);
578 if (p1.y - p2.y <= dist)
579 muscles[which].rotate2 = asin((p1.y - p2.y) / dist);
580 if (p1.y - p2.y > dist)
581 muscles[which].rotate2 = asin(1.f);
582 muscles[which].rotate2 *= 360.0 / 6.2831853;
586 dist = findDistance(&p1, &p2);
587 if (p1.z - p2.z <= dist)
588 muscles[which].rotate1 = acos((p1.z - p2.z) / dist);
589 if (p1.z - p2.z > dist)
590 muscles[which].rotate1 = acos(1.f);
591 muscles[which].rotate1 *= 360.0 / 6.2831853;
593 muscles[which].rotate1 = 360 - muscles[which].rotate1;
594 if (!isnormal(muscles[which].rotate1))
595 muscles[which].rotate1 = 0;
596 if (!isnormal(muscles[which].rotate2))
597 muscles[which].rotate2 = 0;
599 const int label1 = muscles[which].parent1->label;
600 const int label2 = muscles[which].parent2->label;
603 fwd = specialforward[0];
609 fwd = specialforward[1];
615 fwd = specialforward[2];
621 fwd = specialforward[3];
627 fwd = specialforward[4];
630 if (muscles[which].parent1->lower)
637 if (animation == hanganim) {
638 if (label1 == righthand || label2 == righthand) {
642 if (label1 == lefthand || label2 == lefthand) {
649 if (label1 == rightfoot || label2 == rightfoot) {
652 if (label1 == leftfoot || label2 == leftfoot) {
657 fwd = DoRotation(fwd, 0, muscles[which].rotate1 - 90, 0);
658 fwd = DoRotation(fwd, 0, 0, muscles[which].rotate2 - 90);
660 fwd /= findLength(&fwd);
661 if (fwd.z <= 1 && fwd.z >= -1)
662 muscles[which].rotate3 = acos(0 - fwd.z);
664 muscles[which].rotate3 = acos(-1.f);
665 muscles[which].rotate3 *= 360.0 / 6.2831853;
667 muscles[which].rotate3 = 360 - muscles[which].rotate3;
668 if (!isnormal(muscles[which].rotate3))
669 muscles[which].rotate3 = 0;
673 * load an animation from file
675 void Animation::Load(const char *filename, int aheight, int aattack)
679 XYZ startoffset, endoffset;
682 const char *anim_prefix = ":Data:Animations:";
687 // concatenate anim_prefix + filename
688 int len = strlen(anim_prefix) + strlen(filename);
689 char *buf = new char[len + 1];
690 snprintf(buf, len + 1, "%s%s", anim_prefix, filename);
691 // Changing the filename into something the OS can understand
692 char *fixedFN = ConvertFileName(buf);
695 LOG(std::string("Loading animation...") + fixedFN);
697 // clear existing data
704 Game::LoadingScreen();
706 // read file in binary mode
707 tfile = fopen( fixedFN, "rb" );
709 // read numframes, joints to know how much memory to allocate
710 funpackf(tfile, "Bi Bi", &numframes, &joints);
712 for(i = 0; i < joints; i++){
713 if(position[i])dealloc2(position[i]);
714 if(twist[i])dealloc2(twist[i]);
715 if(twist2[i])dealloc2(twist2[i]);
716 if(onground[i])dealloc2(onground[i]);
719 if(position)dealloc2(position);
720 if(twist)dealloc2(twist);
721 if(twist2)dealloc2(twist2);
722 if(speed)dealloc2(speed);
723 if(onground)dealloc2(onground);
724 if(forward)dealloc2(forward);
725 if(weapontarget)dealloc2(weapontarget);
726 if(label)dealloc2(label);*/
729 // allocate memory for everything
731 position = (XYZ**)malloc(sizeof(XYZ*) * joints);
732 for (i = 0; i < joints; i++)
733 position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
735 twist = (float**)malloc(sizeof(float*) * joints);
736 for (i = 0; i < joints; i++)
737 twist[i] = (float*)malloc(sizeof(float) * numframes);
739 twist2 = (float**)malloc(sizeof(float*) * joints);
740 for (i = 0; i < joints; i++)
741 twist2[i] = (float*)malloc(sizeof(float) * numframes);
743 speed = (float*)malloc(sizeof(float) * numframes);
745 onground = (bool**)malloc(sizeof(bool*) * joints);
746 for (i = 0; i < joints; i++)
747 onground[i] = (bool*)malloc(sizeof(bool) * numframes);
749 forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
750 weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
751 label = (int*)malloc(sizeof(int) * numframes);
753 /*position = new XYZ[joints][numframes];
754 twist = new float[joints][numframes];
755 twist2 = new float[joints][numframes];
756 speed = new float[numframes];
757 onground = new bool[joints][numframes];
758 forward = new XYZ[numframes];
759 label = new int[numframes];*/
762 // read binary data as animation
765 for (i = 0; i < numframes; i++) {
766 // for each joint in the skeleton...
767 for (j = 0; j < joints; j++) {
768 // read joint position
769 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
771 for (j = 0; j < joints; j++) {
773 funpackf(tfile, "Bf", &twist[j][i]);
775 for (j = 0; j < joints; j++) {
776 // read onground (boolean)
778 funpackf(tfile, "Bb", &uch);
779 onground[j][i] = (uch != 0);
781 // read frame speed (?)
782 funpackf(tfile, "Bf", &speed[i]);
784 // read twist2 for whole animation
785 for (i = 0; i < numframes; i++) {
786 for (j = 0; j < joints; j++) {
787 funpackf(tfile, "Bf", &twist2[j][i]);
790 // read label for each frame
791 for (i = 0; i < numframes; i++) {
792 funpackf(tfile, "Bf", &label[i]);
794 // read weapontargetnum
795 funpackf(tfile, "Bi", &weapontargetnum);
796 // read weapontarget positions for each frame
797 for (i = 0; i < numframes; i++) {
798 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
806 // find average position of certain joints on first and last frames
807 // and save in startoffset, endoffset
808 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
809 for (j = 0; j < joints; j++) {
810 if (position[j][0].y < 1)
811 startoffset += position[j][0];
812 if (position[j][numframes - 1].y < 1)
813 endoffset += position[j][numframes - 1];
815 startoffset /= joints;
824 * takes filenames for three skeleton files and various models
826 void Skeleton::Load(const char *filename, const char *lowfilename, const char *clothesfilename,
827 const char *modelfilename, const char *model2filename,
828 const char *model3filename, const char *model4filename,
829 const char *model5filename, const char *model6filename,
830 const char *model7filename, const char *modellowfilename,
831 const char *modelclothesfilename, bool clothes)
837 int i, j, tempmuscle;
849 // load various models
850 // rotate, scale, do normals, do texcoords for each as needed
852 model[0].loadnotex(modelfilename);
853 model[1].loadnotex(model2filename);
854 model[2].loadnotex(model3filename);
855 model[3].loadnotex(model4filename);
856 model[4].loadnotex(model5filename);
857 model[5].loadnotex(model6filename);
858 model[6].loadnotex(model7filename);
860 for (i = 0; i < num_models; i++) {
861 model[i].Rotate(180, 0, 0);
862 model[i].Scale(.04, .04, .04);
863 model[i].CalculateNormals(0);
866 drawmodel.load(modelfilename, 0);
867 drawmodel.Rotate(180, 0, 0);
868 drawmodel.Scale(.04, .04, .04);
869 drawmodel.FlipTexCoords();
870 if (tutoriallevel == 1 && id != 0)
871 drawmodel.UniformTexCoords();
872 if (tutoriallevel == 1 && id != 0)
873 drawmodel.ScaleTexCoords(0.1);
874 drawmodel.CalculateNormals(0);
876 modellow.loadnotex(modellowfilename);
877 modellow.Rotate(180, 0, 0);
878 modellow.Scale(.04, .04, .04);
879 modellow.CalculateNormals(0);
881 drawmodellow.load(modellowfilename, 0);
882 drawmodellow.Rotate(180, 0, 0);
883 drawmodellow.Scale(.04, .04, .04);
884 drawmodellow.FlipTexCoords();
885 if (tutoriallevel == 1 && id != 0)
886 drawmodellow.UniformTexCoords();
887 if (tutoriallevel == 1 && id != 0)
888 drawmodellow.ScaleTexCoords(0.1);
889 drawmodellow.CalculateNormals(0);
892 modelclothes.loadnotex(modelclothesfilename);
893 modelclothes.Rotate(180, 0, 0);
894 modelclothes.Scale(.041, .04, .041);
895 modelclothes.CalculateNormals(0);
897 drawmodelclothes.load(modelclothesfilename, 0);
898 drawmodelclothes.Rotate(180, 0, 0);
899 drawmodelclothes.Scale(.04, .04, .04);
900 drawmodelclothes.FlipTexCoords();
901 drawmodelclothes.CalculateNormals(0);
904 // FIXME: three similar blocks follow, one for each of:
905 // filename, lowfilename, clothesfilename
909 tfile = fopen( ConvertFileName(filename), "rb" );
911 if (1) { // FIXME: should this be if(tfile) ?
913 funpackf(tfile, "Bi", &num_joints);
916 //joints.resize(num_joints);
918 delete [] joints; //dealloc2(joints);
919 joints = (Joint*)new Joint[num_joints]; //malloc(sizeof(Joint)*num_joints);
921 // read info for each joint
922 for (i = 0; i < num_joints; i++) {
923 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);
924 funpackf(tfile, "Bb Bb", &joints[i].hasparent, &joints[i].locked);
925 funpackf(tfile, "Bi", &joints[i].modelnum);
926 funpackf(tfile, "Bb Bb", &joints[i].visible, &joints[i].sametwist);
927 funpackf(tfile, "Bi Bi", &joints[i].label, &joints[i].hasgun);
928 funpackf(tfile, "Bb", &joints[i].lower);
929 funpackf(tfile, "Bi", &parentID);
930 if (joints[i].hasparent)
931 joints[i].parent = &joints[parentID];
932 joints[i].velocity = 0;
933 joints[i].oldposition = joints[i].position;
937 tempmuscle = num_muscles;
938 funpackf(tfile, "Bi", &num_muscles);
943 delete [] muscles; //dealloc2(muscles);
944 muscles = (Muscle*)new Muscle[num_muscles]; //malloc(sizeof(Muscle)*num_muscles);
948 // for each muscle...
949 for (i = 0; i < num_muscles; i++) {
951 tempmuscle = muscles[i].numvertices;
952 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);
954 // allocate memory for vertices
955 //muscles[i].vertices.clear();
956 //muscles[i].vertices.resize(muscles[i].numvertices);
957 //if(muscles[i].vertices)dealloc2(muscles[i].vertices);
958 muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices);
962 for (j = 0; j < muscles[i].numvertices - edit; j++) {
963 funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]);
964 if (muscles[i].vertices[j + edit] >= model[0].vertexNum) {
965 muscles[i].numvertices--;
971 funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID);
972 muscles[i].parent1 = &joints[parentID];
973 funpackf(tfile, "Bi", &parentID);
974 muscles[i].parent2 = &joints[parentID];
977 // read forwardjoints (?)
978 for (j = 0; j < 3; j++) {
979 funpackf(tfile, "Bi", &forwardjoints[j]);
981 // read lowforwardjoints (?)
982 for (j = 0; j < 3; j++) {
983 funpackf(tfile, "Bi", &lowforwardjoints[j]);
987 for (j = 0; j < num_muscles; j++) {
988 for (i = 0; i < muscles[j].numvertices; i++) {
989 for (int k = 0; k < num_models; k++) {
990 if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum)
991 model[k].owner[muscles[j].vertices[i]] = j;
996 // calculate some stuff
998 for (i = 0; i < num_joints; i++) {
999 joints[i].startpos = joints[i].position;
1001 for (i = 0; i < num_muscles; i++) {
1002 FindRotationMuscle(i, -1);
1004 // this seems to use opengl purely for matrix calculations
1005 for (int k = 0; k < num_models; k++) {
1006 for (i = 0; i < model[k].vertexNum; i++) {
1007 model[k].vertex[i] = model[k].vertex[i] - (muscles[model[k].owner[i]].parent1->position + muscles[model[k].owner[i]].parent2->position) / 2;
1008 glMatrixMode(GL_MODELVIEW);
1011 glRotatef(muscles[model[k].owner[i]].rotate3, 0, 1, 0);
1012 glRotatef(muscles[model[k].owner[i]].rotate2 - 90, 0, 0, 1);
1013 glRotatef(muscles[model[k].owner[i]].rotate1 - 90, 0, 1, 0);
1014 glTranslatef(model[k].vertex[i].x, model[k].vertex[i].y, model[k].vertex[i].z);
1015 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1016 model[k].vertex[i].x = M[12] * 1;
1017 model[k].vertex[i].y = M[13] * 1;
1018 model[k].vertex[i].z = M[14] * 1;
1021 model[k].CalculateNormals(0);
1028 tfile = fopen( ConvertFileName(lowfilename), "rb" );
1030 if (1) { // FIXME: should this be if(tfile) ?
1031 // skip joints section
1033 lSize = sizeof(num_joints);
1034 fseek(tfile, lSize, SEEK_CUR);
1035 //joints = new Joint[num_joints];
1036 //jointlabels = new int[num_joints];
1037 for (i = 0; i < num_joints; i++) {
1051 fseek(tfile, lSize, SEEK_CUR);
1053 if (joints[i].hasparent)
1054 joints[i].parent = &joints[parentID];
1055 joints[i].velocity = 0;
1056 joints[i].oldposition = joints[i].position;
1060 funpackf(tfile, "Bi", &num_muscles);
1061 //muscles = new Muscle[num_muscles];
1063 for (i = 0; i < num_muscles; i++) {
1065 lSize = sizeof(float)
1071 fseek(tfile, lSize, SEEK_CUR);
1073 // read numverticeslow
1074 tempmuscle = muscles[i].numverticeslow;
1075 funpackf(tfile, "Bi", &muscles[i].numverticeslow);
1077 if (muscles[i].numverticeslow) {
1079 //muscles[i].verticeslow.clear();
1080 //muscles[i].verticeslow.resize(muscles[i].numverticeslow);
1081 //if(muscles[i].verticeslow)dealloc2(muscles[i].verticeslow);
1082 muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow);
1086 for (j = 0; j < muscles[i].numverticeslow - edit; j++) {
1087 funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]);
1088 if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) {
1089 muscles[i].numverticeslow--;
1096 lSize = 1; //sizeof(bool);
1097 fseek ( tfile, lSize, SEEK_CUR);
1098 lSize = sizeof(int);
1099 fseek ( tfile, lSize, SEEK_CUR);
1100 fseek ( tfile, lSize, SEEK_CUR);
1104 lSize = sizeof(int);
1105 for (j = 0; j < num_muscles; j++) {
1106 for (i = 0; i < muscles[j].numverticeslow; i++) {
1107 if (muscles[j].numverticeslow && muscles[j].verticeslow[i] < modellow.vertexNum)
1108 modellow.owner[muscles[j].verticeslow[i]] = j;
1113 for(i=0;i<num_joints;i++){
1114 joints[i].startpos=joints[i].position;
1116 for(i=0;i<num_muscles;i++){
1117 FindRotationMuscle(i,-1);
1120 // use opengl for its matrix math
1121 for (i = 0; i < modellow.vertexNum; i++) {
1122 modellow.vertex[i] = modellow.vertex[i] - (muscles[modellow.owner[i]].parent1->position + muscles[modellow.owner[i]].parent2->position) / 2;
1123 glMatrixMode(GL_MODELVIEW);
1126 glRotatef(muscles[modellow.owner[i]].rotate3, 0, 1, 0);
1127 glRotatef(muscles[modellow.owner[i]].rotate2 - 90, 0, 0, 1);
1128 glRotatef(muscles[modellow.owner[i]].rotate1 - 90, 0, 1, 0);
1129 glTranslatef(modellow.vertex[i].x, modellow.vertex[i].y, modellow.vertex[i].z);
1130 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1131 modellow.vertex[i].x = M[12];
1132 modellow.vertex[i].y = M[13];
1133 modellow.vertex[i].z = M[14];
1137 modellow.CalculateNormals(0);
1143 tfile = fopen( ConvertFileName(clothesfilename), "rb" ); // FIXME: where's the check for valid load
1146 lSize = sizeof(num_joints);
1147 fseek ( tfile, lSize, SEEK_CUR);
1148 //joints = new Joint[num_joints];
1149 //jointlabels = new int[num_joints];
1151 for (i = 0; i < num_joints; i++) {
1165 fseek(tfile, lSize, SEEK_CUR);
1167 if (joints[i].hasparent)
1168 joints[i].parent = &joints[parentID];
1169 joints[i].velocity = 0;
1170 joints[i].oldposition = joints[i].position;
1174 funpackf(tfile, "Bi", &num_muscles);
1175 //muscles = new Muscle[num_muscles];
1177 for (i = 0; i < num_muscles; i++) {
1179 lSize = sizeof(float)
1185 fseek(tfile, lSize, SEEK_CUR);
1187 // read numverticesclothes
1188 tempmuscle = muscles[i].numverticesclothes;
1189 funpackf(tfile, "Bi", &muscles[i].numverticesclothes);
1191 // read verticesclothes
1192 if (muscles[i].numverticesclothes) {
1193 //muscles[i].verticesclothes.clear();
1194 //muscles[i].verticesclothes.resize(muscles[i].numverticesclothes);
1195 //if(muscles[i].verticesclothes)dealloc2(muscles[i].verticesclothes);
1196 muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes);
1198 for (j = 0; j < muscles[i].numverticesclothes - edit; j++) {
1199 funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]);
1200 if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) {
1201 muscles[i].numverticesclothes--;
1208 lSize = 1; //sizeof(bool);
1209 fseek ( tfile, lSize, SEEK_CUR);
1210 lSize = sizeof(int);
1211 fseek ( tfile, lSize, SEEK_CUR);
1212 fseek ( tfile, lSize, SEEK_CUR);
1216 lSize = sizeof(int);
1217 for (j = 0; j < num_muscles; j++) {
1218 for (i = 0; i < muscles[j].numverticesclothes; i++) {
1219 if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum)
1220 modelclothes.owner[muscles[j].verticesclothes[i]] = j;
1225 for(i=0;i<num_joints;i++){
1226 joints[i].startpos=joints[i].position;
1228 for(i=0;i<num_muscles;i++){
1229 FindRotationMuscle(i,-1);
1232 // use opengl for its matrix math
1233 for (i = 0; i < modelclothes.vertexNum; i++) {
1234 modelclothes.vertex[i] = modelclothes.vertex[i] - (muscles[modelclothes.owner[i]].parent1->position + muscles[modelclothes.owner[i]].parent2->position) / 2;
1235 glMatrixMode(GL_MODELVIEW);
1238 glRotatef(muscles[modelclothes.owner[i]].rotate3, 0, 1, 0);
1239 glRotatef(muscles[modelclothes.owner[i]].rotate2 - 90, 0, 0, 1);
1240 glRotatef(muscles[modelclothes.owner[i]].rotate1 - 90, 0, 1, 0);
1241 glTranslatef(modelclothes.vertex[i].x, modelclothes.vertex[i].y, modelclothes.vertex[i].z);
1242 glGetFloatv(GL_MODELVIEW_MATRIX, M);
1243 modelclothes.vertex[i].x = M[12];
1244 modelclothes.vertex[i].y = M[13];
1245 modelclothes.vertex[i].z = M[14];
1249 modelclothes.CalculateNormals(0);
1253 for (i = 0; i < num_joints; i++) {
1254 for (j = 0; j < num_joints; j++) {
1255 if (joints[i].label == j)
1263 Animation::Animation()
1269 weapontargetnum = 0;
1281 Animation::~Animation()
1286 void Animation::deallocate()
1291 for (i = 0; i < joints; i++)
1292 dealloc2(position[i]);
1299 for (i = 0; i < joints; i++)
1307 for (i = 0; i < joints; i++)
1308 dealloc2(twist2[i]);
1315 for (i = 0; i < joints; i++)
1316 dealloc2(onground[i]);
1331 dealloc2(weapontarget);
1341 Skeleton::Skeleton()
1349 memset(forwardjoints, 0, sizeof(forwardjoints));
1354 memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
1357 // XYZ specialforward[5];
1358 memset(jointlabels, 0, sizeof(jointlabels));
1362 // Model modelclothes;
1366 // Model drawmodellow;
1367 // Model drawmodelclothes;
1372 memset(skinText, 0, sizeof(skinText));
1389 Skeleton::~Skeleton()
1406 verticesclothes = 0;
1410 numverticesclothes = 0;
1419 rotate1 = 0, rotate2 = 0, rotate3 = 0;
1420 lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
1421 oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
1422 newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
1430 dealloc2(verticeslow);
1431 dealloc2(verticesclothes);
1434 Animation & Animation::operator = (const Animation & ani)
1438 bool allocate = true;
1440 allocate = ((ani.numframes != numframes) || (ani.joints != joints));
1445 numframes = ani.numframes;
1446 height = ani.height;
1447 attack = ani.attack;
1448 joints = ani.joints;
1449 weapontargetnum = ani.weapontargetnum;
1452 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
1453 for (i = 0; i < ani.joints; i++) {
1455 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1456 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
1460 twist = (float**)malloc(sizeof(float*)*ani.joints);
1461 for (i = 0; i < ani.joints; i++) {
1463 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
1464 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
1468 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
1469 for (i = 0; i < ani.joints; i++) {
1471 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
1472 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
1476 speed = (float*)malloc(sizeof(float) * ani.numframes);
1477 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
1480 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
1481 for (i = 0; i < ani.joints; i++) {
1483 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
1484 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
1488 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1489 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
1492 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
1493 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
1496 label = (int*)malloc(sizeof(int) * ani.numframes);
1497 memcpy(label, ani.label, sizeof(int)*ani.numframes);
1507 // the following functions are not used anywhere
1510 * sets forward, lowforward, specialforward[]
1515 void Skeleton::FindForwardsfirst()
1517 //Find forward vectors
1518 CrossProduct(joints[forwardjoints[1]].position - joints[forwardjoints[0]].position, joints[forwardjoints[2]].position - joints[forwardjoints[0]].position, &forward);
1519 Normalise(&forward);
1521 CrossProduct(joints[lowforwardjoints[1]].position - joints[lowforwardjoints[0]].position, joints[lowforwardjoints[2]].position - joints[lowforwardjoints[0]].position, &lowforward);
1522 Normalise(&lowforward);
1525 specialforward[0] = forward;
1526 specialforward[1] = forward;
1527 specialforward[2] = forward;
1528 specialforward[3] = forward;
1529 specialforward[4] = forward;
1538 void Skeleton::Draw(int muscleview)
1540 static float jointcolor[4];
1542 if (muscleview != 2) {
1549 if (muscleview == 2) {
1555 //Calc motionblur-ness
1556 for (int i = 0; i < num_joints; i++) {
1557 joints[i].oldposition = joints[i].position;
1558 joints[i].blurred = findDistance(&joints[i].position, &joints[i].oldposition) * 100;
1559 if (joints[i].blurred < 1)
1560 joints[i].blurred = 1;
1566 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1568 for (int i = 0; i < num_joints; i++) {
1569 if (joints[i].hasparent) {
1570 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1571 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1572 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1573 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1574 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1575 glVertex3f(joints[i].parent->oldposition.x, joints[i].parent->oldposition.y, joints[i].parent->oldposition.z);
1576 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1577 glVertex3f(joints[i].oldposition.x, joints[i].oldposition.y, joints[i].oldposition.z);
1580 for (int i = 0; i < num_muscles; i++) {
1581 if (muscles[i].type == boneconnect) {
1582 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1583 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1584 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1585 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1586 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1587 glVertex3f(muscles[i].parent2->oldposition.x, muscles[i].parent2->oldposition.y, muscles[i].parent2->oldposition.z);
1588 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1589 glVertex3f(muscles[i].parent1->oldposition.x, muscles[i].parent1->oldposition.y, muscles[i].parent1->oldposition.z);
1595 for (int i = 0; i < num_joints; i++) {
1596 if (joints[i].hasparent) {
1597 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].blurred);
1598 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1599 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / joints[i].parent->blurred);
1600 glVertex3f(joints[i].parent->position.x, joints[i].parent->position.y, joints[i].parent->position.z);
1603 /*for(int i=0; i<num_joints; i++){
1604 if(joints[i].hasparent){
1605 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
1606 glVertex3f(joints[i].position.x,joints[i].position.y,joints[i].position.z);
1607 glColor4f(jointcolor[0],jointcolor[1],jointcolor[2],1);
1608 glVertex3f(joints[i].position.x+forward.x,joints[i].position.y+forward.y,joints[i].position.z+forward.z);
1611 for (int i = 0; i < num_muscles; i++) {
1612 if (muscles[i].type == boneconnect) {
1613 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent1->blurred);
1614 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1615 glColor4f(jointcolor[0], jointcolor[1], jointcolor[2], jointcolor[3] / muscles[i].parent2->blurred);
1616 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1619 glColor3f(.6, .6, 0);
1620 if (muscleview == 1)
1621 for (int i = 0; i < num_muscles; i++) {
1622 if (muscles[i].type != boneconnect) {
1623 glVertex3f(muscles[i].parent1->position.x, muscles[i].parent1->position.y, muscles[i].parent1->position.z);
1624 glVertex3f(muscles[i].parent2->position.x, muscles[i].parent2->position.y, muscles[i].parent2->position.z);
1629 if (muscleview != 2) {
1632 for (int i = 0; i < num_joints; i++) {
1634 glColor4f(0, 0, .5, 1);
1636 glColor4f(1, 1, 0, 1);
1637 if (joints[i].locked && i != selected)
1638 glColor4f(1, 0, 0, 1);
1639 glVertex3f(joints[i].position.x, joints[i].position.y, joints[i].position.z);
1644 //Set old position to current position
1645 if (muscleview == 2)
1646 for (int i = 0; i < num_joints; i++) {
1647 joints[i].oldposition = joints[i].position;
1657 void Skeleton::AddJoint(float x, float y, float z, int which)
1659 if (num_joints < max_joints - 1) {
1660 joints[num_joints].velocity = 0;
1661 joints[num_joints].position.x = x;
1662 joints[num_joints].position.y = y;
1663 joints[num_joints].position.z = z;
1664 joints[num_joints].mass = 1;
1665 joints[num_joints].locked = 0;
1667 /*if(which>=num_joints||which<0)*/
1668 joints[num_joints].hasparent = 0;
1669 /*if(which<num_joints&&which>=0){
1670 joints[num_joints].parent=&joints[which];
1671 joints[num_joints].hasparent=1;
1672 joints[num_joints].length=findDistance(joints[num_joints].position,joints[num_joints].parent->position);
1675 if (which < num_joints && which >= 0)
1676 AddMuscle(num_joints - 1, which, 0, 10, boneconnect);
1685 void Skeleton::DeleteJoint(int whichjoint)
1687 if (whichjoint < num_joints && whichjoint >= 0) {
1688 joints[whichjoint].velocity = joints[num_joints - 1].velocity;
1689 joints[whichjoint].position = joints[num_joints - 1].position;
1690 joints[whichjoint].oldposition = joints[num_joints - 1].oldposition;
1691 joints[whichjoint].hasparent = joints[num_joints - 1].hasparent;
1692 joints[whichjoint].parent = joints[num_joints - 1].parent;
1693 joints[whichjoint].length = joints[num_joints - 1].length;
1694 joints[whichjoint].locked = joints[num_joints - 1].locked;
1695 joints[whichjoint].modelnum = joints[num_joints - 1].modelnum;
1696 joints[whichjoint].visible = joints[num_joints - 1].visible;
1698 for (int i = 0; i < num_muscles; i++) {
1699 while (muscles[i].parent1 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1700 while (muscles[i].parent2 == &joints[whichjoint] && i < num_muscles)DeleteMuscle(i);
1702 for (int i = 0; i < num_muscles; i++) {
1703 while (muscles[i].parent1 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent1 = &joints[whichjoint];
1704 while (muscles[i].parent2 == &joints[num_joints - 1] && i < num_muscles)muscles[i].parent2 = &joints[whichjoint];
1706 for (int i = 0; i < num_joints; i++) {
1707 if (joints[i].parent == &joints[whichjoint])
1708 joints[i].hasparent = 0;
1710 for (int i = 0; i < num_joints; i++) {
1711 if (joints[i].parent == &joints[num_joints - 1])
1712 joints[i].parent = &joints[whichjoint];
1722 * Skeleton::DeleteJoint - UNUSED
1724 void Skeleton::DeleteMuscle(int whichmuscle)
1726 if (whichmuscle < num_muscles) {
1727 muscles[whichmuscle].minlength = muscles[num_muscles - 1].minlength;
1728 muscles[whichmuscle].maxlength = muscles[num_muscles - 1].maxlength;
1729 muscles[whichmuscle].strength = muscles[num_muscles - 1].strength;
1730 muscles[whichmuscle].parent1 = muscles[num_muscles - 1].parent1;
1731 muscles[whichmuscle].parent2 = muscles[num_muscles - 1].parent2;
1732 muscles[whichmuscle].length = muscles[num_muscles - 1].length;
1733 muscles[whichmuscle].visible = muscles[num_muscles - 1].visible;
1734 muscles[whichmuscle].type = muscles[num_muscles - 1].type;
1735 muscles[whichmuscle].targetlength = muscles[num_muscles - 1].targetlength;
1746 void Skeleton::SetJoint(float x, float y, float z, int which, int whichjoint)
1748 if (whichjoint < num_joints) {
1749 joints[whichjoint].velocity = 0;
1750 joints[whichjoint].position.x = x;
1751 joints[whichjoint].position.y = y;
1752 joints[whichjoint].position.z = z;
1754 if (which >= num_joints || which < 0)
1755 joints[whichjoint].hasparent = 0;
1756 if (which < num_joints && which >= 0) {
1757 joints[whichjoint].parent = &joints[which];
1758 joints[whichjoint].hasparent = 1;
1759 joints[whichjoint].length = findDistance(&joints[whichjoint].position, &joints[whichjoint].parent->position);
1767 * Skeleton::AddJoint - UNUSED
1769 void Skeleton::AddMuscle(int attach1, int attach2, float minlength, float maxlength, int type)
1771 const int max_muscles = 100; // FIXME: Probably can be dropped
1772 if (num_muscles < max_muscles - 1 && attach1 < num_joints && attach1 >= 0 && attach2 < num_joints && attach2 >= 0 && attach1 != attach2) {
1773 muscles[num_muscles].parent1 = &joints[attach1];
1774 muscles[num_muscles].parent2 = &joints[attach2];
1775 muscles[num_muscles].length = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1776 muscles[num_muscles].targetlength = findDistance(&muscles[num_muscles].parent1->position, &muscles[num_muscles].parent2->position);
1777 muscles[num_muscles].strength = .7;
1778 muscles[num_muscles].type = type;
1779 muscles[num_muscles].minlength = minlength;
1780 muscles[num_muscles].maxlength = maxlength;
1791 void Skeleton::MusclesSet()
1793 for (int i = 0; i < num_muscles; i++) {
1794 muscles[i].length = findDistance(&muscles[i].parent1->position, &muscles[i].parent2->position);
1803 void Skeleton::DoBalance()
1806 newpoint=joints[0].position;
1807 newpoint.x=(joints[2].position.x+joints[4].position.x)/2;
1808 newpoint.z=(joints[2].position.z+joints[4].position.z)/2;
1809 joints[0].velocity=joints[0].velocity+(newpoint-joints[0].position);
1810 //Move child point to within certain distance of parent point
1811 joints[0].position=newpoint;