00001 #include "Skeleton.h"
00002
00003 Skeleton::Skeleton()
00004 {
00005
00006 }
00007
00008 Skeleton::~Skeleton()
00009 {
00010
00011 }
00012
00013 void Skeleton::Load(Tokenizer &tokens)
00014 {
00015 char temp[64];
00016 float skelScale;
00017
00018 BallJoint *currParent = NULL;
00019
00020 tokens.FindToken("numbones");
00021 numJoints = tokens.GetInt();
00022
00023 tokens.FindToken("scale");
00024 skelScale = tokens.GetFloat();
00025
00026 joints = new BallJoint[numJoints];
00027
00028 tokens.FindToken("bone");
00029 for(int i = 0; i < numJoints; i++)
00030 {
00031
00032
00033 tokens.GetToken(temp);
00034 joints[i].parent = currParent;
00035 currParent = &(joints[i]);
00036 tokens.FindToken("offset");
00037 float x = tokens.GetFloat();
00038 float y = tokens.GetFloat();
00039 float z = tokens.GetFloat();
00040 joints[i].local.d.Set(skelScale*x,skelScale*y,skelScale*z);
00041 tokens.FindToken("pose");
00042 joints[i].x.SetVal(x=tokens.GetFloat());
00043 joints[i].y.SetVal(y=tokens.GetFloat());
00044 joints[i].z.SetVal(z=tokens.GetFloat());
00045 joints[i].xBase.SetVal(x);
00046 joints[i].yBase.SetVal(y);
00047 joints[i].zBase.SetVal(z);
00048
00049
00050
00051 x = tokens.GetFloat();
00052 y = tokens.GetFloat();
00053 z = tokens.GetFloat();
00054 joints[i].binding.a.Set(x,y,z);
00055 x = tokens.GetFloat();
00056 y = tokens.GetFloat();
00057 z = tokens.GetFloat();
00058 joints[i].binding.b.Set(x,y,z);
00059 x = tokens.GetFloat();
00060 y = tokens.GetFloat();
00061 z = tokens.GetFloat();
00062 joints[i].binding.c.Set(x,y,z);
00063 x = tokens.GetFloat();
00064 y = tokens.GetFloat();
00065 z = tokens.GetFloat();
00066 joints[i].binding.d.Set(skelScale*x,skelScale*y,skelScale*z);
00067 joints[i].binding.Inverse();
00068
00069
00070 tokens.GetToken(temp);
00071 while(strcmp(temp,"endbone") == 0)
00072 {
00073 currParent = currParent->parent;
00074 if(currParent == NULL)
00075 break;
00076 tokens.GetToken(temp);
00077 }
00078 }
00079 }
00080
00081 void Skeleton::Update()
00082 {
00083 joints[0].world = world;
00084
00085 joints[0].world.d.Add(rootPos);
00086
00087 for(int i=0;i<numJoints;i++)
00088 joints[i].Update();
00089 }
00090
00091 void Skeleton::Draw()
00092 {
00093
00094 for(int i=0;i<numJoints;i++)
00095 joints[i].Draw();
00096 }
00097
00098 void Skeleton::SetWorld(Matrix34 &w)
00099 {
00100 world = w;
00101
00102 world.MakeLevel();
00103 }
00104
00105 void Skeleton::Pose(AnimPose *pose)
00106 {
00107 int numBones = pose->GetNumBones();
00108 Vector3 *p = pose->GetPose();
00109 if(pose->GetRootTranslation())
00110 rootPos.Subtract(pose->GetRootPos(),joints[0].local.d);
00111 else
00112 rootPos.Zero();
00113
00114 for(int i = 0; i < numBones; i++)
00115 {
00116 joints[i].x.SetVal(p[i].x);
00117 joints[i].y.SetVal(p[i].y);
00118 joints[i].z.SetVal(p[i].z);
00119 }
00120 }
00121
00122 void Skeleton::BlendPose(AnimPose *pose1, AnimPose *pose2)
00123 {
00124 int numBones = pose1->GetNumBones();
00125 Vector3 *p1 = pose1->GetPose();
00126 Vector3 *p2 = pose2->GetPose();
00127 rootPos.Zero();
00128
00129 for(int i = 0; i < numBones; i++)
00130 {
00131 joints[i].x.SetVal((p1[i].x-joints[i].xBase.GetVal()) +
00132 (p2[i].x-joints[i].xBase.GetVal()) + joints[i].xBase.GetVal());
00133 joints[i].y.SetVal((p1[i].y-joints[i].yBase.GetVal()) +
00134 (p2[i].y-joints[i].yBase.GetVal()) + joints[i].yBase.GetVal());
00135 joints[i].z.SetVal((p1[i].z-joints[i].zBase.GetVal()) +
00136 (p2[i].z-joints[i].zBase.GetVal()) + joints[i].zBase.GetVal());
00137 }
00138 }