Example #1
0
 public BrgFile()
     : base()
 {
     this.FileName   = string.Empty;
     this.Header     = new BrgHeader();
     this.AsetHeader = new BrgAsetHeader();
 }
Example #2
0
        public BrgFile(System.IO.FileStream fileStream)
            : base()
        {
            using (BrgBinaryReader reader = new BrgBinaryReader(new LittleEndianBitConverter(), fileStream))
            {
                this.FileName = fileStream.Name;
                this.Header   = new BrgHeader(reader);
                if (this.Header.Magic != "BANG")
                {
                    throw new Exception("This is not a BRG file!");
                }
                this.AsetHeader = new BrgAsetHeader();

                int asetCount = 0;
                this.Meshes    = new List <BrgMesh>(this.Header.NumMeshes);
                this.Materials = new List <BrgMaterial>();
                while (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    string magic = reader.ReadString(4);
                    if (magic == "ASET")
                    {
                        this.AsetHeader = new BrgAsetHeader(reader);
                        ++asetCount;
                    }
                    else if (magic == "MESI")
                    {
                        if (this.Meshes.Count > 0)
                        {
                            this.Meshes[0].MeshAnimations.Add(new BrgMesh(reader, this));
                        }
                        else
                        {
                            this.Meshes.Add(new BrgMesh(reader, this));
                        }
                    }
                    else if (magic == "MTRL")
                    {
                        BrgMaterial mat = new BrgMaterial(reader, this);
                        Materials.Add(mat);
                        if (!ContainsMaterialID(mat.Id))
                        {
                            //Materials.Add(mat);
                        }
                        else
                        {
                            //throw new Exception("Duplicate material ids!");
                        }
                    }
                    else
                    {
                        throw new Exception("The type tag " + /* magic +*/ " is not recognized!");
                    }
                }

                if (asetCount > 1)
                {
                    //throw new Exception("Multiple ASETs!");
                }

                if (Header.NumMeshes < Meshes.Count)
                {
                    throw new Exception("Inconsistent mesh count!");
                }

                if (Header.NumMaterials < Materials.Count)
                {
                    throw new Exception("Inconsistent material count!");
                }

                if (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    throw new Exception("The end of stream was not reached!");
                }

                this.Animation.Duration = this.Meshes[0].ExtendedHeader.AnimationLength;
                this.Animation.TimeStep = this.Meshes[0].ExtendedHeader.AnimationLength / (float)(this.Meshes[0].MeshAnimations.Count + 1);
                if (this.Meshes[0].Header.AnimationType.HasFlag(BrgMeshAnimType.NonUniform))
                {
                    for (int i = 0; i <= this.Meshes[0].MeshAnimations.Count; ++i)
                    {
                        this.Animation.MeshKeys.Add(this.Meshes[0].NonUniformKeys[i] * this.Animation.Duration);
                    }
                }
                else if (this.Meshes[0].MeshAnimations.Count > 0)
                {
                    for (int i = 0; i <= this.Meshes[0].MeshAnimations.Count; ++i)
                    {
                        this.Animation.MeshKeys.Add((float)i / ((float)this.Meshes[0].MeshAnimations.Count) * this.Animation.Duration);
                    }
                }
                else
                {
                    this.Animation.MeshKeys.Add(0);
                }
            }
        }