private void ParseJoint(idLexer lexer, idMD5Joint joint, ref idJointQuaternion defaultPose)
        {
            //
            // parse name
            //
            joint.Name = lexer.ReadToken().ToString();

            //
            // parse parent
            //
            int parentIndex = lexer.ParseInt();

            if (parentIndex >= 0)
            {
                if (parentIndex >= (_joints.Length - 1))
                {
                    lexer.Error("Invalid parent for joint '{0}'", joint.Name);
                }

                joint.Parent = _joints[parentIndex];
            }

            //
            // parse default pose
            //
            float[] tmp = lexer.Parse1DMatrix(3);
            defaultPose.Translation = new Vector3(tmp[0], tmp[1], tmp[2]);

            tmp = lexer.Parse1DMatrix(3);
            defaultPose.Quaternion   = new Quaternion(tmp[0], tmp[1], tmp[2], 0);
            defaultPose.Quaternion.W = idHelper.CalculateW(defaultPose.Quaternion);
        }
		private void ParseJoint(idLexer lexer, idMD5Joint joint, ref idJointQuaternion defaultPose)
		{
			//
			// parse name
			//
			joint.Name = lexer.ReadToken().ToString();

			//
			// parse parent
			//
			int parentIndex = lexer.ParseInt();

			if(parentIndex >= 0)
			{
				if(parentIndex >= (_joints.Length - 1))
				{
					lexer.Error("Invalid parent for joint '{0}'", joint.Name);
				}

				joint.Parent = _joints[parentIndex];
			}
		
			//
			// parse default pose
			//
			float[] tmp = lexer.Parse1DMatrix(3);
			defaultPose.Translation = new Vector3(tmp[0], tmp[1], tmp[2]);

			tmp = lexer.Parse1DMatrix(3);
			defaultPose.Quaternion = new Quaternion(tmp[0], tmp[1], tmp[2], 0);
			defaultPose.Quaternion.W = idHelper.CalculateW(defaultPose.Quaternion);
		}
        public override int GetJointIndex(idMD5Joint joint)
        {
            int jointCount = _joints.Length;

            for (int i = 0; i < jointCount; i++)
            {
                if (_joints[i] == joint)
                {
                    return(i);
                }
            }

            return(-1);
        }
Beispiel #4
0
 /// <summary>
 /// Gets the index of the joint with the given instance.
 /// </summary>
 /// <param name="joint"></param>
 /// <returns></returns>
 public abstract int GetJointIndex(idMD5Joint joint);
 public override int GetJointIndex(idMD5Joint joint)
 {
     return(-1);
 }
        /// <summary>
        /// Used for initial loads, reloadModel, and reloading the data of purged models.
        /// </summary>
        /// <remarks>
        /// Upon exit, the model will absolutely be valid, but possibly as a default model.
        /// </remarks>
        public override void Load()
        {
            if (this.Disposed == true)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            if (_purged == false)
            {
                Purge();
            }

            _purged = false;

            idLexer lexer = new idLexer(LexerOptions.AllowPathNames | LexerOptions.NoStringEscapeCharacters);

            if (lexer.LoadFile(Name) == false)
            {
                MakeDefault();
                return;
            }

            lexer.ExpectTokenString(VersionString);

            int     version = lexer.ParseInt();
            int     count   = 0;
            idToken token;

            if (version != Version)
            {
                lexer.Error("Invalid version {0}. Should be version {1}", version, Version);
            }

            //
            // skip commandline
            //
            lexer.ExpectTokenString("commandline");
            lexer.ReadToken();

            // parse num joints
            lexer.ExpectTokenString("numJoints");

            count = lexer.ParseInt();

            _joints      = new idMD5Joint[count];
            _defaultPose = new idJointQuaternion[count];

            idJointMatrix[] poseMat3 = new idJointMatrix[count];

            // parse num meshes
            lexer.ExpectTokenString("numMeshes");
            count = lexer.ParseInt();

            if (count < 0)
            {
                lexer.Error("Invalid size: {0}", count);
            }

            _meshes = new idMD5Mesh[count];

            //
            // parse joints
            //
            lexer.ExpectTokenString("joints");
            lexer.ExpectTokenString("{");

            int jointCount = _joints.Length;

            for (int i = 0; i < jointCount; i++)
            {
                idMD5Joint        joint = _joints[i] = new idMD5Joint();
                idJointQuaternion pose  = new idJointQuaternion();

                ParseJoint(lexer, joint, ref pose);

                poseMat3[i]             = idJointMatrix.Zero;
                poseMat3[i].Rotation    = Matrix.CreateFromQuaternion(pose.Quaternion);
                poseMat3[i].Translation = pose.Translation;

                if (joint.Parent != null)
                {
                    int parentIndex = GetJointIndex(joint.Parent);

                    pose.Quaternion = Quaternion.CreateFromRotationMatrix(poseMat3[i].ToMatrix()
                                                                          * Matrix.Transpose(poseMat3[parentIndex].ToMatrix()));
                    pose.Translation = Vector3.Transform(poseMat3[i].ToVector3() - poseMat3[parentIndex].ToVector3(),
                                                         Matrix.Transpose(poseMat3[parentIndex].ToMatrix()));
                }

                _defaultPose[i] = pose;
            }

            lexer.ExpectTokenString("}");

            int meshCount = _meshes.Length;

            for (int i = 0; i < meshCount; i++)
            {
                lexer.ExpectTokenString("mesh");

                _meshes[i] = new idMD5Mesh();
                _meshes[i].Parse(lexer, poseMat3);
            }

            //
            // calculate the bounds of the model
            //
            CalculateBounds(poseMat3);

            // set the timestamp for reloadmodels
            idConsole.Warning("TODO: fileSystem->ReadFile( name, NULL, &timeStamp );");
        }
Beispiel #7
0
		/// <summary>
		/// Gets the index of the joint with the given instance.
		/// </summary>
		/// <param name="joint"></param>
		/// <returns></returns>
		public abstract int GetJointIndex(idMD5Joint joint);
		public override int GetJointIndex(idMD5Joint joint)
		{
			return -1;
		}
		/// <summary>
		/// Used for initial loads, reloadModel, and reloading the data of purged models.
		/// </summary>
		/// <remarks>
		/// Upon exit, the model will absolutely be valid, but possibly as a default model.
		/// </remarks>
		public override void Load()
		{
			if(this.Disposed == true)
			{
				throw new ObjectDisposedException(this.GetType().Name);
			}

			if(_purged == false)
			{
				Purge();
			}

			_purged = false;

			idLexer lexer = new idLexer(LexerOptions.AllowPathNames | LexerOptions.NoStringEscapeCharacters);

			if(lexer.LoadFile(Name) == false)
			{
				MakeDefault();
				return;
			}

			lexer.ExpectTokenString(VersionString);

			int version = lexer.ParseInt();
			int count = 0;
			idToken token;

			if(version != Version)
			{
				lexer.Error("Invalid version {0}. Should be version {1}", version, Version);
			}

			//
			// skip commandline
			//
			lexer.ExpectTokenString("commandline");
			lexer.ReadToken();

			// parse num joints
			lexer.ExpectTokenString("numJoints");

			count = lexer.ParseInt();

			_joints = new idMD5Joint[count];
			_defaultPose = new idJointQuaternion[count];
			
			idJointMatrix[] poseMat3 = new idJointMatrix[count];

			// parse num meshes
			lexer.ExpectTokenString("numMeshes");
			count = lexer.ParseInt();

			if(count < 0)
			{
				lexer.Error("Invalid size: {0}", count);
			}

			_meshes = new idMD5Mesh[count];

			//
			// parse joints
			//
			lexer.ExpectTokenString("joints");
			lexer.ExpectTokenString("{");

			int jointCount = _joints.Length;

			for(int i = 0; i < jointCount; i++)
			{
				idMD5Joint joint = _joints[i] = new idMD5Joint();
				idJointQuaternion pose = new idJointQuaternion();

				ParseJoint(lexer, joint, ref pose);

				poseMat3[i] = idJointMatrix.Zero;
				poseMat3[i].Rotation = Matrix.CreateFromQuaternion(pose.Quaternion);
				poseMat3[i].Translation = pose.Translation;

				if(joint.Parent != null)
				{
					int parentIndex = GetJointIndex(joint.Parent);

					pose.Quaternion = Quaternion.CreateFromRotationMatrix(poseMat3[i].ToMatrix() 
										* Matrix.Transpose(poseMat3[parentIndex].ToMatrix()));
					pose.Translation = Vector3.Transform(poseMat3[i].ToVector3() - poseMat3[parentIndex].ToVector3(), 
										Matrix.Transpose(poseMat3[parentIndex].ToMatrix()));
				}

				_defaultPose[i] = pose;
			}

			lexer.ExpectTokenString("}");

			int meshCount = _meshes.Length;

			for(int i = 0; i < meshCount; i++)
			{
				lexer.ExpectTokenString("mesh");

				_meshes[i] = new idMD5Mesh();
				_meshes[i].Parse(lexer, poseMat3);
			}

			//
			// calculate the bounds of the model
			//
			CalculateBounds(poseMat3);

			// set the timestamp for reloadmodels
			idConsole.Warning("TODO: fileSystem->ReadFile( name, NULL, &timeStamp );");
		}
		public override int GetJointIndex(idMD5Joint joint)
		{
			int jointCount = _joints.Length;

			for(int i = 0; i < jointCount; i++)
			{
				if(_joints[i] == joint)
				{
					return i;
				}
			}

			return -1;
		}