public Md5JointValue GetBaseJointValue(int joint, Md5Frame baseframe, Md5Frame thisframe) { Md5JointValue v; int parentjoint = anim.Hierarchy[joint].ParentIndex; if (parentjoint < 0) { v = baseframe.Values[joint]; } else { if (parentjoint >= joint) { throw new Exception(""); } Md5JointValue parent = thisframe.Values[parentjoint]; //Vector3 p = Matrix4Extensions.FromQuaternion(parent.Orientation) * baseframe.Values[joint].Position; Vector3 p = Vector3.Transform(baseframe.Values[joint].Position, parent.Orientation); p += parent.Position; Quaternion q = baseframe.Values[joint].Orientation * parent.Orientation; q.Normalize(); v.Orientation = q; v.Position = p; } return(v); }
/* * public float[] GetJointValues(float[] values, int joint) * { * float[] r = new float[6]; * int i = 0; * int c = 1; * for (int k = 0; k < 6; ++k) * { * //if ((Hierarchy[joint].Components & c) != 0) * if (false) * { * //component is animated * r[k] = values[anim.Hierarchy[joint].FrameIndex + i]; * i++; * } * else * { * //not animated, get from baseframe * //r[k] = values[joint*6+k]; * } * c *= 2; * } * return r; * } */ public Md5JointValue GetJointValue(float[] values, int joint, Md5Frame thisframe, Md5Frame baseframe) { Md5JointValue v = baseframe.Values[joint]; int i = anim.Hierarchy[joint].FrameIndex; int c = anim.Hierarchy[joint].Components; if (Flip) { if ((c & Md5JointAnim.Tx) != 0) { v.Position.X = values[i++]; } if ((c & Md5JointAnim.Ty) != 0) { v.Position.Z = values[i++]; } if ((c & Md5JointAnim.Tz) != 0) { v.Position.Y = values[i++]; } if ((c & Md5JointAnim.Qx) != 0) { v.Orientation.X = values[i++]; } if ((c & Md5JointAnim.Qy) != 0) { v.Orientation.Z = values[i++]; } if ((c & Md5JointAnim.Qz) != 0) { v.Orientation.Y = values[i++]; } } else { if ((c & Md5JointAnim.Tx) != 0) { v.Position.X = values[i++]; } if ((c & Md5JointAnim.Ty) != 0) { v.Position.Y = values[i++]; } if ((c & Md5JointAnim.Tz) != 0) { v.Position.Z = values[i++]; } if ((c & Md5JointAnim.Qx) != 0) { v.Orientation.X = values[i++]; } if ((c & Md5JointAnim.Qy) != 0) { v.Orientation.Y = values[i++]; } if ((c & Md5JointAnim.Qz) != 0) { v.Orientation.Z = values[i++]; } } float term = 1.0f - ((v.Orientation.X * v.Orientation.X) + (v.Orientation.Y * v.Orientation.Y) + (v.Orientation.Z * v.Orientation.Z)); if (term < 0.0f) { v.Orientation.W = 0.0f; } else { v.Orientation.W = (float)Math.Sqrt(term); } //v.Orientation.W = (float)Math.Sqrt(1.0f - ); int parentjoint = anim.Hierarchy[joint].ParentIndex; if (parentjoint < 0) { } else { if (parentjoint >= joint) { throw new Exception(""); } Md5JointValue parent = thisframe.Values[parentjoint]; //Vector3 p = Matrix4Extensions.FromQuaternion(parent.Orientation) * v.Position; Vector3 p = Vector3.Transform(v.Position, parent.Orientation); p += parent.Position; Quaternion q = v.Orientation * parent.Orientation; q.Normalize(); v.Orientation = q; v.Position = p; } return(v); }
Md5Frame LoadFrame(bool baseframe) { string[] line; int i = 0; float[] values; if (baseframe) { values = new float[anim.NumJoints * 6]; } else { values = new float[anim.NumAnimatedComponents]; } while ((line = ReadLineTokens()) != null && line[0] != "}") { foreach (string s in line) { values[i++] = float.Parse(s); } } if (i != values.Length) { throw new Exception(""); } Md5Frame frame = new Md5Frame(); Md5Frame baseframe1 = new Md5Frame(); frame.Values = new Md5JointValue[anim.NumJoints]; baseframe1.Values = new Md5JointValue[anim.NumJoints]; if (baseframe) { for (i = 0; i < anim.NumJoints; ++i) { frame.Values[i] = GetBaseFrameJointValue(i, values); } for (i = 0; i < anim.NumJoints; ++i) { baseframe1.Values[i] = GetBaseFrameJointValue(i, values); } for (i = 0; i < anim.NumJoints; ++i) { frame.Values[i] = GetBaseJointValue(i, frame, frame); } } else { for (i = 0; i < anim.NumJoints; ++i) { frame.Values[i] = GetJointValue(values, i, frame, anim.BaseFrame); } } if (baseframe) { return(baseframe1); } else { return(frame); } }
Md5Frame LoadFrame(bool baseframe) { string[] line; int i = 0; float[] values; if(baseframe) values = new float[anim.NumJoints*6]; else values=new float[anim.NumAnimatedComponents]; while ((line = ReadLineTokens()) != null && line[0] != "}") { foreach (string s in line) { values[i++] = float.Parse(s); } } if (i != values.Length) throw new Exception(""); Md5Frame frame=new Md5Frame(); Md5Frame baseframe1 = new Md5Frame(); frame.Values = new Md5JointValue[anim.NumJoints]; baseframe1.Values = new Md5JointValue[anim.NumJoints]; if (baseframe) { for (i = 0; i < anim.NumJoints; ++i) { frame.Values[i] = GetBaseFrameJointValue(i, values); } for (i = 0; i < anim.NumJoints; ++i) { baseframe1.Values[i] = GetBaseFrameJointValue(i, values); } for (i = 0; i < anim.NumJoints; ++i) { frame.Values[i] = GetBaseJointValue(i, frame, frame); } } else { for (i = 0; i < anim.NumJoints; ++i) { frame.Values[i] = GetJointValue(values, i, frame, anim.BaseFrame); } } if (baseframe) return baseframe1; else return frame; }
/* public float[] GetJointValues(float[] values, int joint) { float[] r = new float[6]; int i = 0; int c = 1; for (int k = 0; k < 6; ++k) { //if ((Hierarchy[joint].Components & c) != 0) if (false) { //component is animated r[k] = values[anim.Hierarchy[joint].FrameIndex + i]; i++; } else { //not animated, get from baseframe //r[k] = values[joint*6+k]; } c *= 2; } return r; } */ public Md5JointValue GetJointValue(float[] values, int joint, Md5Frame thisframe, Md5Frame baseframe) { Md5JointValue v = baseframe.Values[joint]; int i = anim.Hierarchy[joint].FrameIndex; int c = anim.Hierarchy[joint].Components; if (Flip) { if ((c & Md5JointAnim.Tx) != 0) v.Position.X = values[i++]; if ((c & Md5JointAnim.Ty) != 0) v.Position.Z = values[i++]; if ((c & Md5JointAnim.Tz) != 0) v.Position.Y = values[i++]; if ((c & Md5JointAnim.Qx) != 0) v.Orientation.X = values[i++]; if ((c & Md5JointAnim.Qy) != 0) v.Orientation.Z = values[i++]; if ((c & Md5JointAnim.Qz) != 0) v.Orientation.Y = values[i++]; } else { if ((c & Md5JointAnim.Tx) != 0) v.Position.X = values[i++]; if ((c & Md5JointAnim.Ty) != 0) v.Position.Y = values[i++]; if ((c & Md5JointAnim.Tz) != 0) v.Position.Z = values[i++]; if ((c & Md5JointAnim.Qx) != 0) v.Orientation.X = values[i++]; if ((c & Md5JointAnim.Qy) != 0) v.Orientation.Y = values[i++]; if ((c & Md5JointAnim.Qz) != 0) v.Orientation.Z = values[i++]; } float term = 1.0f - ((v.Orientation.X * v.Orientation.X) + (v.Orientation.Y * v.Orientation.Y) + (v.Orientation.Z * v.Orientation.Z)); if (term < 0.0f) v.Orientation.W = 0.0f; else v.Orientation.W = (float)Math.Sqrt(term); //v.Orientation.W = (float)Math.Sqrt(1.0f - ); int parentjoint = anim.Hierarchy[joint].ParentIndex; if (parentjoint < 0) { } else { if (parentjoint >= joint) throw new Exception(""); Md5JointValue parent = thisframe.Values[parentjoint]; //Vector3 p = Matrix4Extensions.FromQuaternion(parent.Orientation) * v.Position; Vector3 p = Vector3.Transform(v.Position, parent.Orientation); p += parent.Position; Quaternion q = v.Orientation * parent.Orientation; q.Normalize(); v.Orientation = q; v.Position = p; } return v; }
public Md5JointValue GetBaseJointValue(int joint,Md5Frame baseframe,Md5Frame thisframe) { Md5JointValue v; int parentjoint=anim.Hierarchy[joint].ParentIndex; if ( parentjoint< 0) { v = baseframe.Values[joint]; } else { if (parentjoint >= joint) throw new Exception(""); Md5JointValue parent = thisframe.Values[parentjoint]; //Vector3 p = Matrix4Extensions.FromQuaternion(parent.Orientation) * baseframe.Values[joint].Position; Vector3 p = Vector3.Transform(baseframe.Values[joint].Position, parent.Orientation); p += parent.Position; Quaternion q = baseframe.Values[joint].Orientation * parent.Orientation; q.Normalize(); v.Orientation = q; v.Position = p; } return v; }