Example #1
0
            internal static Animation Load(FileSystem fs, string path)
            {
                List <AnimationForBone> afb = new List <AnimationForBone>();

                using (Stream s = fs.open(path))
                {
                    BinaryReader br        = new BinaryReader(s);
                    int          version   = br.ReadInt32();
                    int          bonecount = br.ReadInt32();
                    for (int boneid = 0; boneid < bonecount; ++boneid)
                    {
                        AnimationForBone ab = new AnimationForBone();
                        afb.Add(ab);
                        int poscount = br.ReadInt32();
                        for (int posid = 0; posid < poscount; ++posid)
                        {
                            float time = br.ReadSingle();
                            vec3  pos  = vec3.Read(br);
                            ab.addPositon(time, pos);
                        }
                        int rotcount = br.ReadInt32();
                        for (int rotid = 0; rotid < rotcount; ++rotid)
                        {
                            float time = br.ReadSingle();
                            quat  rot  = quat.Read(br);
                            ab.addRotation(time, rot);
                        }
                    }
                }
                return(new Animation(afb));
            }
Example #2
0
        public Shader(FileSystem sys, string path)
        {
            if (false == IsShadersSupported)
            {
                throw new Exception("shaders not supported on your card, sorry");
            }

            XmlElement shader = Xml.Open(Xml.FromStream(sys.open(path)), "shader");

            LoadFrom(path, shader);
        }
Example #3
0
        public override void load(MediaLoader ml, FileSystem fs, string path)
        {
            Bitmap b = new Bitmap(fs.open(path));

            b.RotateFlip(RotateFlipType.RotateNoneFlipY);
            Rectangle  rectangle  = new Rectangle(0, 0, b.Width, b.Height);
            BitmapData bitmapData = b.LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

            img = new Image(false, b.Width, b.Height, bitmapData.Scan0, false, Gl.GL_BGR_EXT);
            b.UnlockBits(bitmapData);
        }
Example #4
0
 private static void LoadMaterialLibrary(MeshDef mesh, FileSystem fs, string path)
 {
     MeshDef.MaterialDef mat = null;
     using (var file = fs.open(path))
     {
         foreach (string l in FileUtil.LinesIn(file))
         {
             string line = l.Trim();
             if (string.IsNullOrEmpty(line) == false && line[0] != '#')
             {
                 string[] data = line.Split(" \t".ToCharArray());
                 if (data[0] == "newmtl")
                 {
                     mat = mesh.addMaterial(data[1].Trim());
                 }
                 else if (data[0] == "Ka")
                 {
                     mat.ambient = ParseColor(data);
                 }
                 else if (data[0] == "Ke")
                 {
                     mat.emissive = ParseColor(data);
                 }
                 else if (data[0] == "Kd")
                 {
                     mat.diffuse = ParseColor(data);
                 }
                 else if (data[0] == "Ks")
                 {
                     mat.specular = ParseColor(data);
                 }
                 else if (data[0] == "d" || data[0] == "Tr")
                 {
                     mat.alpha = floatParse(data[1]);
                 }
                 else if (data[0] == "Ns")
                 {
                     mat.shininess = floatParse(data[1]);
                 }
                 else if (data[0] == "map_Ka" || data[0] == "map_Kd")
                 {
                     mat.texture = Resolve(path, data[1]);
                 }
             }
         }
     }
 }
Example #5
0
            internal static MeshDef Load(FileSystem fs, string path)
            {
                MeshDef def = new MeshDef();

                using (Stream s = fs.open(path))
                {
                    BinaryReader br        = new BinaryReader(s);
                    int          version   = br.ReadInt32();
                    int          materials = br.ReadInt32();
                    for (int materialid = 0; materialid < materials; ++materialid)
                    {
                        MeshDef.MaterialDef m = def.addMaterial("m" + materialid.ToString());
                        m.texture   = br.ReadString();
                        m.ambient   = vec3.Read(br);
                        m.diffuse   = vec3.Read(br);
                        m.specular  = vec3.Read(br);
                        m.emissive  = vec3.Read(br);
                        m.alpha     = br.ReadSingle();
                        m.shininess = br.ReadSingle();
                    }
                    int bonecount = br.ReadInt32();
                    for (int boneid = 0; boneid < bonecount; ++boneid)
                    {
                        MeshDef.Bone bone = def.newBone();
                        bone.name   = br.ReadString();
                        bone.parent = br.ReadInt32();
                        bone.pos    = vec3.Read(br);
                        vec3 qv = vec3.Read(br);
                        bone.rot = new quat(br.ReadSingle(), qv);
                    }
                    int pointcount = br.ReadInt32();
                    for (int pointid = 0; pointid < pointcount; ++pointid)
                    {
                        int  boneid = br.ReadInt32();
                        vec3 p      = new vec3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                        def.addPoint(p, boneid);
                    }
                    int uvcount = br.ReadInt32();
                    for (int uvid = 0; uvid < uvcount; ++uvid)
                    {
                        vec2 v = new vec2(br.ReadSingle(), br.ReadSingle());
                        def.AddUv(v);
                    }
                    int normalcount = br.ReadInt32();
                    for (int normalid = 0; normalid < normalcount; ++normalid)
                    {
                        vec3 p = new vec3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                        def.addNomal(p);
                    }

                    for (int materialid = 0; materialid < materials; ++materialid)
                    {
                        def.selectMaterial("m" + materialid.ToString());
                        int tricount = br.ReadInt32();
                        for (int triid = 0; triid < tricount; ++triid)
                        {
                            MeshDef.VertexData[] data = new MeshDef.VertexData[3];
                            for (int i = 0; i < 3; ++i)
                            {
                                data[i].vertex = br.ReadInt32();
                                data[i].uv     = br.ReadInt32();
                                data[i].normal = br.ReadInt32();
                            }
                            def.addTri(new MeshDef.Tri(data));
                        }
                    }
                }
                return(def);
            }
Example #6
0
            public static MeshDef Load(FileSystem fs, string path)
            {
                MeshDef mesh = new MeshDef();

                using (var file = fs.open(path))
                {
                    foreach (string l in FileUtil.LinesIn(file))
                    {
                        string line = l.Trim();
                        if (string.IsNullOrEmpty(line) == false && line[0] != '#')
                        {
                            string[] data = line.Split(" \t".ToCharArray());
                            if (data[0] == "v")
                            {
                                mesh.addPoint(new vec3(floatParse(data[1]), floatParse(data[2]), floatParse(data[3])), -1);
                            }
                            else if (data[0] == "vt")
                            {
                                mesh.AddUv(new vec2(floatParse(data[1]), floatParse(data[2])));
                            }
                            else if (data[0] == "vn")
                            {
                                mesh.addNomal(new vec3(floatParse(data[1]), floatParse(data[2]), floatParse(data[3])));
                            }
                            else if (data[0] == "f")
                            {
                                List <MeshDef.VertexData> vd = new List <MeshDef.VertexData>();
                                for (int i = 1; i < data.Length; ++i)
                                {
                                    string[]           ind = data[i].Split("/".ToCharArray());
                                    MeshDef.VertexData v   = new MeshDef.VertexData();
                                    v.vertex = int.Parse(ind[0]) - 1;
                                    v.uv     = int.Parse(ind[1]) - 1;
                                    v.normal = int.Parse(ind[2]) - 1;
                                    vd.Add(v);
                                }
                                if (vd.Count < 3)
                                {
                                    throw new Exception("Face data incomplete");
                                }
                                for (int i = 2; i < vd.Count; ++i)
                                {
                                    List <MeshDef.VertexData> arr = new List <MeshDef.VertexData>();
                                    arr.Add(vd[0]);
                                    arr.Add(vd[1]);
                                    arr.Add(vd[i]);
                                    mesh.addTri(new MeshDef.Tri(arr.ToArray()));
                                }
                            }
                            else if (data[0] == "usemtl")
                            {
                                mesh.selectMaterial(data[1].Trim());
                            }
                            else if (data[0] == "mtllib")
                            {
                                LoadMaterialLibrary(mesh, fs, Resolve(path, data[1].Trim()));
                            }
                        }
                    }
                }
                return(mesh);
            }
Example #7
0
        public static Actor Load(FileSystem fs, string p)
        {
            XmlElement xactor = Xml.Open(Xml.FromStream(fs.open(p)), "actor");
            XmlElement xmesh  = xactor["mesh"];

            List <AnimationInformation> animinfo = ParseAnimationInfo(xmesh);
            List <string> bonesToIgnore          = ParseIgnoreBone(xmesh);

            Dictionary <string, string> texmap    = ParseSetTexture(xmesh);
            Dictionary <string, string> overrides = ParseOverrides(xmesh);

            fs.setOverrides(overrides);

            string meshpath = Xml.GetAttributeString(xmesh, "file");
            float  scale    = Xml.GetAttribute <float>(xmesh, "scale", math1.ParseFloat, 1.0f);

            MeshDef   def;
            Animation animation;

            string ext = Path.GetExtension(meshpath);

            if (ext == ".txt")
            {
                SimpleEngine.load.MilkshapeAscii.Load(fs, meshpath, out def, out animation, 1);
            }
            else if (ext == ".ms3d")
            {
                SimpleEngine.load.MilkshapeBinary.Load(fs, meshpath, out def, out animation);
            }
            else if (animinfo.Count == 0)
            {
                animation = null;
                def       = MeshFile.Load(fs, meshpath);
            }
            else
            {
                throw new Exception("Unhandled format " + ext + " for " + meshpath);
            }

            foreach (string ignoreThisBone in bonesToIgnore)
            {
                for (int i = 0; i < def.bones.Count; ++i)
                {
                    if (ignoreThisBone == def.bones[i].name)
                    {
                        def.bones.RemoveAt(i);
                        animation.bones.RemoveAt(i);
                    }
                }
            }

            foreach (MeshDef.MaterialDef mat in def.Materials)
            {
                string matname = mat.name.ToLower();
                if (texmap.ContainsKey(matname))
                {
                    mat.texture = texmap[matname];
                    texmap.Remove(matname);
                }
            }

            if (texmap.Count != 0)
            {
                throw new Exception("Some materials was not mapped");
            }

            def.scale(scale);
            if (animation != null)
            {
                animation.scale(scale);
            }

            def.translateFiles(overrides);

            Actor actor = new Actor(def.mapBones());

            if (animation != null)
            {
                foreach (AnimationInformation ai in animinfo)
                {
                    Animation an = animation.subanim(ai);
                    actor.add(ai.name, an);
                }
            }

            fs.clearOverrides(overrides);
            return(actor);
        }