[C#] Animated Tiny.x is not completely loaded

Started by
0 comments, last by Lithium4Despair 14 years, 7 months ago
Hi I have problem with my codes.i cannot load a animated mesh (e.g:Tiny.X) completely.he will loose some part of his body i have no idea why. anyone can help me?Please and i have a another question "how can i use animated X files without overrideng AllocateHierarchys methods?"i don't want to use my way anyone has a better way to load animated x file? This is my way but it has a problem:

        private AnimationRootFrame rootFrame;
        private Vector3 objectCenter;
        private float objectRadius;
        private float elapsedTime;
        public class AllocateHierarchyDerived : AllocateHierarchy
        {
            main_class app = null;
            public AllocateHierarchyDerived(main_class  parent)
            {
                app = parent;
            }
            public override Frame CreateFrame(string name)
            {
                FrameDerived frame = new FrameDerived();
                frame.Name = name;
                frame.TransformationMatrix = Matrix.Identity;
                frame.CombinedTransformationMatrix = Matrix.Identity;

                return frame;
            }
            public override MeshContainer CreateMeshContainer(string name,MeshData meshData, ExtendedMaterial[] materials, EffectInstance[] effectInstances, GraphicsStream adjacency,SkinInformation skinInfo)
            {
                if (meshData.Mesh == null)
                    throw new ArgumentException();
                if (meshData.Mesh.VertexFormat == VertexFormats.None)
                    throw new ArgumentException();
                MeshContainerDerived mesh = new MeshContainerDerived();

                mesh.Name = name;
                int numFaces = meshData.Mesh.NumberFaces;
                Device dev = meshData.Mesh.Device;
                if ((meshData.Mesh.VertexFormat & VertexFormats.Normal) == 0)
                {
                    Mesh tempMesh = meshData.Mesh.Clone(meshData.Mesh.Options.Value,
                        meshData.Mesh.VertexFormat | VertexFormats.Normal, dev);

                    meshData.Mesh = tempMesh;
                    meshData.Mesh.ComputeNormals();
                }
                mesh.SetMaterials(materials);
                mesh.SetAdjacency(adjacency);
                Texture[] meshTextures = new Texture[materials.Length];
                for (int i = 0; i < materials.Length; i++)
                {
                    if (materials.TextureFilename != null)
                    {
                         meshTextures = TextureLoader.FromFile(dev, @"E:\Projects\C#\Game\Media\" +
                         materials.TextureFilename);
                    }
                }
                mesh.SetTextures(meshTextures);
                mesh.MeshData = meshData;
                if (skinInfo != null)
                {
                    mesh.SkinInformation = skinInfo;
                    int numBones = skinInfo.NumberBones;
                    Matrix[] offsetMatrices = new Matrix[numBones];

                    for (int i = 0; i < numBones; i++)
                        offsetMatrices = skinInfo.GetBoneOffsetMatrix(i);

                    mesh.SetOffsetMatrices(offsetMatrices);

                    app.GenerateSkinnedMesh(mesh);
                }

                return mesh;
            }

        }
        public void GenerateSkinnedMesh(MeshContainerDerived mesh)
        {
            if (mesh.SkinInformation == null)
                throw new ArgumentException();

            int numInfl = 0;
            BoneCombination[] bones;
            MeshData m = mesh.MeshData;
            m.Mesh = mesh.SkinInformation.ConvertToBlendedMesh(m.Mesh, MeshFlags.Managed
                | MeshFlags.OptimizeVertexCache, mesh.GetAdjacencyStream(), out numInfl,
                out bones);
            mesh.NumberInfluences = numInfl;
            mesh.SetBones(bones);
            mesh.NumberAttributes = bones.Length;
            mesh.MeshData = m;
        }
        public class FrameDerived : Frame
        {
            private Matrix combined = Matrix.Identity;
            public Matrix CombinedTransformationMatrix
            {
                get { return combined; }
                set { combined = value; }
            }
        }
        public class MeshContainerDerived : MeshContainer
        {
            private Texture[] meshTextures = null;
            private int numAttr = 0;
            private int numInfl = 0;
            private BoneCombination[] bones;
            private FrameDerived[] frameMatrices;
            private Matrix[] offsetMatrices;
            public Texture[] GetTextures() { return meshTextures; }
            public void SetTextures(Texture[] textures) { meshTextures = textures; }
            public BoneCombination[] GetBones() { return bones; }
            public void SetBones(BoneCombination[] b) { bones = b; }
            public FrameDerived[] GetFrames() { return frameMatrices; }
            public void SetFrames(FrameDerived[] frames) { frameMatrices = frames; }
            public Matrix[] GetOffsetMatrices() { return offsetMatrices; }
            public void SetOffsetMatrices(Matrix[] matrices) { offsetMatrices = matrices; }
            public int NumberAttributes { get { return numAttr; } set { numAttr = value; } }
            public int NumberInfluences { get { return numInfl; } set { numInfl = value; } }
        }
        private void CreateAnimation(string file, PresentParameters presentParams)
        {
            AllocateHierarchyDerived alloc = new AllocateHierarchyDerived(this);
            rootFrame = Mesh.LoadHierarchyFromFile(file, MeshFlags.Managed,
                graphic, alloc, null);
            objectRadius = Frame.CalculateBoundingSphere(rootFrame.FrameHierarchy,
                out objectCenter);
            SetupBoneMatrices((FrameDerived)rootFrame.FrameHierarchy);
            DXUtil.Timer(DirectXTimer.Start);
        }
        private void SetupBoneMatrices(FrameDerived frame)
        {
            if (frame.MeshContainer != null)
            {
                SetupBoneMatrices((MeshContainerDerived)frame.MeshContainer);
            }

            if (frame.FrameSibling != null)
            {
                SetupBoneMatrices((FrameDerived)frame.FrameSibling);
            }

            if (frame.FrameFirstChild != null)
            {
                SetupBoneMatrices((FrameDerived)frame.FrameFirstChild);
            }
        }

        private void SetupBoneMatrices(MeshContainerDerived mesh)
        {
            if (mesh.SkinInformation != null)
            {
                int numBones = mesh.SkinInformation.NumberBones;

                FrameDerived[] frameMatrices = new FrameDerived[numBones];
                for (int i = 0; i < numBones; i++)
                {
                    FrameDerived frame = (FrameDerived)Frame.Find(
                        rootFrame.FrameHierarchy,
                        mesh.SkinInformation.GetBoneName(i));

                    if (frame == null)
                        throw new ArgumentException();

                    frameMatrices = frame;
                }
                mesh.SetFrames(frameMatrices);
            }
        }
        private void ProcessNextFrame()
        {
            elapsedTime = DXUtil.Timer(DirectXTimer.GetElapsedTime);
            Matrix worldMatrix = Matrix.Translation(objectCenter);
            graphic.Transform.World = worldMatrix;
            if (rootFrame.AnimationController != null)
                rootFrame.AnimationController.AdvanceTime(elapsedTime, null);
            UpdateFrameMatrices((FrameDerived)rootFrame.FrameHierarchy, worldMatrix);
        }
        private void UpdateFrameMatrices(FrameDerived frame, Matrix parentMatrix)
        {
            frame.CombinedTransformationMatrix = frame.TransformationMatrix *
                parentMatrix;
            if (frame.FrameSibling != null)
            {
                UpdateFrameMatrices((FrameDerived)frame.FrameSibling, parentMatrix);
            }
            if (frame.FrameFirstChild != null)
            {
                UpdateFrameMatrices((FrameDerived)frame.FrameFirstChild,
                    frame.CombinedTransformationMatrix);
            }
        }
        private void DrawFrame(FrameDerived frame)
        {
            MeshContainerDerived mesh = (MeshContainerDerived)frame.MeshContainer;
            while (mesh != null)
            {
                DrawMeshContainer(mesh, frame);

                mesh = (MeshContainerDerived)mesh.NextContainer;
            }

            if (frame.FrameSibling != null)
            {
                DrawFrame((FrameDerived)frame.FrameSibling);
            }

            if (frame.FrameFirstChild != null)
            {
                DrawFrame((FrameDerived)frame.FrameFirstChild);
            }
        }
        private void DrawMeshContainer(MeshContainerDerived mesh, FrameDerived frame)
        {
            if (mesh.SkinInformation != null)
            {

                int attribIdPrev = -1;
                for (int iattrib = 0; iattrib < mesh.NumberAttributes; iattrib++)
                {
                    int numBlend = 0;
                    BoneCombination[] bones = mesh.GetBones();
                    for (int i = 0; i < mesh.NumberInfluences; i++)
                    {
                        if (bones[iattrib].BoneId != -1)
                        {
                            numBlend = i;
                        }
                    }

                    if (graphic.DeviceCaps.MaxVertexBlendMatrices >= numBlend + 1)
                    {
                        Matrix[] offsetMatrices = mesh.GetOffsetMatrices();
                        FrameDerived[] frameMatrices = mesh.GetFrames();
                        for (int i = 0; i < mesh.NumberInfluences; i++)
                        {
                            int matrixIndex = bones[iattrib].BoneId;
                            if (matrixIndex != -1)
                            {
                                Matrix tempMatrix = offsetMatrices[matrixIndex] *
                                    frameMatrices[matrixIndex].
                                    CombinedTransformationMatrix;
                                graphic.Transform.SetWorldMatrixByIndex(i, tempMatrix);
                            }
                        }

                        graphic.RenderState.VertexBlend = (VertexBlend)numBlend;
                        if ((attribIdPrev != bones[iattrib].AttributeId) ||
                            (attribIdPrev == -1))
                        {
                            graphic.Material = mesh.GetMaterials()[
                                bones[iattrib].AttributeId].Material3D;
//Here have a problem so i used  graphic.SetTexture(0, TextureLoader.FromF... instead of that
                           // graphic.SetTexture(0, mesh.GetTextures()[bones[iattrib].AttributeId]); 
                            graphic.SetTexture(0, TextureLoader.FromFile(graphic, @"E:\Projects\C#\Game\Media\Tiny_skin.bmp"));
                            attribIdPrev = bones[iattrib].AttributeId;
                        }

                        mesh.MeshData.Mesh.DrawSubset(iattrib);
                    }
                }
            }
            else
            {
                graphic.Transform.World = frame.CombinedTransformationMatrix;

                ExtendedMaterial[] mtrl = mesh.GetMaterials();
                for (int iMaterial = 0; iMaterial < mtrl.Length; iMaterial++)
                {
                    graphic.Material = mtrl[iMaterial].Material3D;
                    graphic.SetTexture(0, mesh.GetTextures()[iMaterial]);
                    mesh.MeshData.Mesh.DrawSubset(iMaterial);
                }
            }
        }
private void RUN()
        {
            while (Created)
            {
                ProcessNextFrame();
                graphic.Clear(ClearFlags.Target,Color.Black, 1.0f, 0);
                graphic.BeginScene();
                graphic.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)(this.Width / this.Height), 1f, 500000000f);
                graphic.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 1000), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
                DrawFrame((FrameDerived)rootFrame.FrameHierarchy);
                graphic.EndScene();
                graphic.Present();
                Application.DoEvents();
            }


Where is my problem in this code?(sorry 4 long length post)
Advertisement
Any idea?

This topic is closed to new replies.

Advertisement