Beispiel #1
0
        private static Bitmap ToBitmap(byte[] xifile)
        {
            Level5_XI xi = new Level5_XI();

            xi.Open(xifile);
            return(xi.ToBitmap());
        }
Beispiel #2
0
        public static GenericTexture ToGenericTexture(byte[] xifile)
        {
            GenericTexture tex = new GenericTexture();
            Level5_XI      xi  = new Level5_XI();

            xi.Open(xifile);
            if (xi.SwitchFile && xi.ImageFormat == 0x1D)
            {
                tex.Mipmaps.Add(xi.BuildImageDataFromBlock(8)[0]);
                tex.InternalFormat = OpenTK.Graphics.OpenGL.PixelInternalFormat.CompressedRgbS3tcDxt1Ext;
                tex.Width          = (uint)xi.Height;
                tex.Height         = (uint)xi.Width;
            }
            else
            if (xi.SwitchFile && xi.ImageFormat == 0x1F)
            {
                tex.Mipmaps.Add(xi.BuildImageDataFromBlock(16)[0]);
                tex.InternalFormat = OpenTK.Graphics.OpenGL.PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                tex.Width          = (uint)xi.Height;
                tex.Height         = (uint)xi.Width;
            }
            else
            if (xi.SwitchFile && xi.ImageFormat == 0x1)
            {
                tex.Mipmaps.Add(xi.BuildImageData()[0]);
                tex.InternalFormat = OpenTK.Graphics.OpenGL.PixelInternalFormat.Rgb8;
                tex.PixelFormat    = OpenTK.Graphics.OpenGL.PixelFormat.Rgb;
                tex.Width          = (uint)xi.Height;
                tex.Height         = (uint)xi.Width;
            }
            else
            {
                var texture = xi.ToBitmap();
                tex.FromBitmap(texture);
                texture.Dispose();
            }
            return(tex);
        }
Beispiel #3
0
        public GenericModel ToGenericModel()
        {
            var model = new GenericModel();
            var skel  = new GenericSkeleton();

            model.Skeleton = skel;

            Level5_Resource resourceFile = null;
            var             textureList  = new List <GenericTexture>();

            foreach (var f in Files)
            {
                //Console.WriteLine(f.Key);
                if (f.Key.EndsWith("RES.bin"))
                {
                    resourceFile = new Level5_Resource(f.Value);
                    model.Name   = resourceFile.ModelName;
                }
                if (f.Key.EndsWith(".mbn"))
                {
                    skel.Bones.Add(Level5_MBN.ToBone(f.Value));
                }
                if (f.Key.EndsWith(".prm"))
                {
                    model.Meshes.Add(Level5_PRM.ToGenericMesh(f.Value));
                }
                if (f.Key.EndsWith(".atr"))
                {
                }
                if (f.Key.EndsWith(".xi"))
                {
                    var tex = Level5_XI.ToGenericTexture(f.Value);
                    tex.Name = f.Key;
                    textureList.Add(tex);
                }
            }

            if (resourceFile == null)
            {
                return(model);
            }


            // add materials
            foreach (var mat in resourceFile.Materials)
            {
                GenericMaterial material = new GenericMaterial();
                material.TextureDiffuse = mat.TexName;
                model.MaterialBank.Add(mat.Name, material);
            }

            // add textures
            for (int i = 0; i < textureList.Count; i++)
            {
                model.TextureBank.Add(resourceFile.TextureNames[i], textureList[i]);
            }

            // fix bones
            foreach (var bone in skel.Bones)
            {
                bone.Name = resourceFile.GetResourceName((uint)bone.ID);
                bone.ID   = 0;
            }
            foreach (var bone in skel.Bones)
            {
                if (bone.ParentIndex == 0)
                {
                    bone.ParentIndex = -1;
                }
                else
                {
                    var parentName = resourceFile.GetResourceName((uint)bone.ParentIndex);
                    bone.ParentIndex = skel.Bones.FindIndex(e => e.Name.Equals(parentName));
                }
            }
            var boneIndex = 0;

            foreach (var bone in skel.Bones)
            {
                if (bone.Name.Equals(""))
                {
                    bone.Name = "Bone_" + boneIndex++;
                }
            }

            foreach (var mesh in model.Meshes)
            {
                for (int i = 0; i < mesh.VertexCount; i++)
                {
                    var     vertex   = mesh.Vertices[i];
                    Vector4 newBones = new Vector4();
                    for (int j = 0; j < 4; j++)
                    {
                        if (vertex.Weights[j] > 0)
                        {
                            var hash = BitConverter.ToUInt32(BitConverter.GetBytes(vertex.Bones[j]), 0);
                            newBones[j] = skel.Bones.FindIndex(e => e.Name == resourceFile.GetResourceName(hash));
                        }
                    }
                    vertex.Bones     = newBones;
                    mesh.Vertices[i] = vertex;
                }
            }

            return(model);
        }