/// <summary>
        /// Carga una animación a un modelo ya cargado, a partir del string del XML.
        /// La animación se agrega al modelo.
        /// </summary>
        /// <param name="mesh">Modelo ya cargado</param>
        /// <param name="xmlString">contenido del XML</param>
        public TgcKeyFrameAnimation loadAnimationFromString(TgcKeyFrameMesh mesh, string xmlString)
        {
            TgcKeyFrameParser        parser        = new TgcKeyFrameParser();
            TgcKeyFrameAnimationData animationData = parser.parseAnimationFromString(xmlString);

            return(loadAnimation(mesh, animationData));
        }
Beispiel #2
0
        /// <summary>
        /// Levanta la informacion de una animacion a partir del XML
        /// </summary>
        /// <param name="xmlString">Contenido que el XML</param>
        public TgcKeyFrameAnimationData parseAnimationFromString(string xmlString)
        {
            XmlDocument dom = new XmlDocument();

            dom.LoadXml(xmlString);
            XmlElement root = dom.DocumentElement;

            TgcKeyFrameAnimationData animation = new TgcKeyFrameAnimationData();

            //Parsear informacion general de animation
            XmlElement animationNode = (XmlElement)root.GetElementsByTagName("animation")[0];

            animation.name           = animationNode.Attributes["name"].InnerText;
            animation.verticesCount  = int.Parse(animationNode.Attributes["verticesCount"].InnerText);
            animation.framesCount    = int.Parse(animationNode.Attributes["framesCount"].InnerText);
            animation.keyFramesCount = int.Parse(animationNode.Attributes["keyFramesCount"].InnerText);
            animation.frameRate      = int.Parse(animationNode.Attributes["frameRate"].InnerText);
            animation.startFrame     = int.Parse(animationNode.Attributes["startFrame"].InnerText);
            animation.endFrame       = int.Parse(animationNode.Attributes["endFrame"].InnerText);

            //Parsear boundingBox, si esta
            XmlNodeList boundingBoxNodes = animationNode.GetElementsByTagName("boundingBox");

            if (boundingBoxNodes != null && boundingBoxNodes.Count == 1)
            {
                XmlNode boundingBoxNode = boundingBoxNodes[0];
                animation.pMin = TgcParserUtils.parseFloat3Array(boundingBoxNode.Attributes["min"].InnerText);
                animation.pMax = TgcParserUtils.parseFloat3Array(boundingBoxNode.Attributes["max"].InnerText);
            }

            XmlNodeList frameNodes = animationNode.GetElementsByTagName("frame");

            animation.keyFrames = new TgcKeyFrameFrameData[frameNodes.Count];
            int i = 0;

            foreach (XmlElement frameNode in frameNodes)
            {
                TgcKeyFrameFrameData frame = new TgcKeyFrameFrameData();
                int frameNumber            = (int)TgcParserUtils.parseFloat(frameNode.Attributes["time"].InnerText);
                frame.relativeTime = (float)frameNumber / animation.framesCount;


                //parsear vertices
                frame.verticesCoordinates = TgcParserUtils.parseFloatStream(frameNode.InnerText, animation.verticesCount);

                animation.keyFrames[i++] = frame;
            }



            return(animation);
        }
        /// <summary>
        /// Devuelve el cuadro actual de animacion
        /// </summary>
        /// <returns></returns>
        private int getCurrentFrame()
        {
            float position = currentTime / animationTimeLenght;
            TgcKeyFrameAnimationData data = currentAnimation.Data;

            for (int i = 0; i < data.keyFrames.Length; i++)
            {
                if (position < data.keyFrames[i].relativeTime)
                {
                    return(i - 1);
                }
            }

            return(data.keyFrames.Length - 2);
        }
        /// <summary>
        /// Levanta la informacion de una animacion a partir del XML
        /// </summary>
        /// <param name="xmlString">Contenido que el XML</param>
        public TgcKeyFrameAnimationData parseAnimationFromString(string xmlString)
        {
            XmlDocument dom = new XmlDocument();
            dom.LoadXml(xmlString);
            XmlElement root = dom.DocumentElement;

            TgcKeyFrameAnimationData animation = new TgcKeyFrameAnimationData();

            //Parsear informacion general de animation
            XmlElement animationNode = (XmlElement)root.GetElementsByTagName("animation")[0];
            animation.name = animationNode.Attributes["name"].InnerText;
            animation.verticesCount = int.Parse(animationNode.Attributes["verticesCount"].InnerText);
            animation.framesCount = int.Parse(animationNode.Attributes["framesCount"].InnerText);
            animation.keyFramesCount = int.Parse(animationNode.Attributes["keyFramesCount"].InnerText);
            animation.frameRate = int.Parse(animationNode.Attributes["frameRate"].InnerText);
            animation.startFrame = int.Parse(animationNode.Attributes["startFrame"].InnerText);
            animation.endFrame = int.Parse(animationNode.Attributes["endFrame"].InnerText);

            //Parsear boundingBox, si esta
            XmlNodeList boundingBoxNodes = animationNode.GetElementsByTagName("boundingBox");
            if (boundingBoxNodes != null && boundingBoxNodes.Count == 1)
            {
                XmlNode boundingBoxNode = boundingBoxNodes[0];
                animation.pMin = TgcParserUtils.parseFloat3Array(boundingBoxNode.Attributes["min"].InnerText);
                animation.pMax = TgcParserUtils.parseFloat3Array(boundingBoxNode.Attributes["max"].InnerText);
            }

            XmlNodeList frameNodes = animationNode.GetElementsByTagName("frame");
            animation.keyFrames = new TgcKeyFrameFrameData[frameNodes.Count];
            int i = 0;
            foreach (XmlElement frameNode in frameNodes)
            {
                TgcKeyFrameFrameData frame = new TgcKeyFrameFrameData();
                int frameNumber = (int)TgcParserUtils.parseFloat(frameNode.Attributes["time"].InnerText);
                frame.relativeTime = (float)frameNumber / animation.framesCount;

                //parsear vertices
                frame.verticesCoordinates = TgcParserUtils.parseFloatStream(frameNode.InnerText, animation.verticesCount);

                animation.keyFrames[i++] = frame;
            }

            return animation;
        }
        /// <summary>
        /// Carga una animación a un modelo ya cargado, a partir de un objeto TgcKeyFrameAnimationData ya parseado
        /// La animación se agrega al modelo.
        /// </summary>
        /// <param name="mesh">Modelo ya cargado</param>
        /// <param name="animationData">Objeto de animacion con datos ya cargados</param>
        public TgcKeyFrameAnimation loadAnimation(TgcKeyFrameMesh mesh, TgcKeyFrameAnimationData animationData)
        {
            //BoundingBox de la animación, aprovechar lo que viene en el XML o utilizar el de la malla estática
            TgcBoundingBox boundingBox = null;
            if (animationData.pMin != null && animationData.pMax != null)
            {
                boundingBox = new TgcBoundingBox(
                    TgcParserUtils.float3ArrayToVector3(animationData.pMin),
                    TgcParserUtils.float3ArrayToVector3(animationData.pMax));
            }
            else
            {
                boundingBox = mesh.BoundingBox;
            }

            TgcKeyFrameAnimation animation = new TgcKeyFrameAnimation(animationData, boundingBox);
            mesh.Animations.Add(animationData.name, animation);
            return animation;
        }
        /// <summary>
        /// Carga una animación a un modelo ya cargado, a partir de un objeto TgcKeyFrameAnimationData ya parseado
        /// La animación se agrega al modelo.
        /// </summary>
        /// <param name="mesh">Modelo ya cargado</param>
        /// <param name="animationData">Objeto de animacion con datos ya cargados</param>
        public TgcKeyFrameAnimation loadAnimation(TgcKeyFrameMesh mesh, TgcKeyFrameAnimationData animationData)
        {
            //BoundingBox de la animación, aprovechar lo que viene en el XML o utilizar el de la malla estática
            TgcBoundingBox boundingBox = null;

            if (animationData.pMin != null && animationData.pMax != null)
            {
                boundingBox = new TgcBoundingBox(
                    TgcParserUtils.float3ArrayToVector3(animationData.pMin),
                    TgcParserUtils.float3ArrayToVector3(animationData.pMax));
            }
            else
            {
                boundingBox = mesh.BoundingBox;
            }

            TgcKeyFrameAnimation animation = new TgcKeyFrameAnimation(animationData, boundingBox);

            mesh.Animations.Add(animationData.name, animation);
            return(animation);
        }
 public TgcKeyFrameAnimation(TgcKeyFrameAnimationData data, TgcBoundingBox boundingBox)
 {
     this.data = data;
     this.boundingBox = boundingBox;
 }
 public TgcKeyFrameAnimation(TgcKeyFrameAnimationData data, TgcBoundingBox boundingBox)
 {
     this.data        = data;
     this.boundingBox = boundingBox;
 }