Example #1
0
        public Model(Reader reader)
        {
            if (!reader.ReadBytes(4).SequenceEqual(MAGIC))
            {
                throw new Exception("Invalid config file header magic");
            }

            for (int a = 0; a < 256; a++)
            {
                MySin[a] = -Math.Sin(a * Math.PI / 128);
                MyCos[a] = Math.Cos(a * Math.PI / 128);
            }

            byte flags = reader.ReadByte();

            HasNormals  = GetBit(flags, 0);
            HasTextures = GetBit(flags, 1);
            HasColours  = GetBit(flags, 2);

            Console.WriteLine("MDL READ: FLAGS:" + Pad(flags));

            string Pad(byte b)
            {
                return(Convert.ToString(b, 2).PadLeft(8, '0'));
            }

            FaceVerticiesCount = reader.ReadByte();
            if (FaceVerticiesCount != 3 && FaceVerticiesCount != 4)
            {
                throw new Exception("Detected Vertex Type wasn't Tris or Quads! RSDKv5 doesn't support other N-gons!");
            }

            ushort VertexCount = 0;
            ushort FramesCount = 0;
            ushort FaceCount   = 0;

            VertexCount = reader.ReadUInt16();
            FramesCount = reader.ReadUInt16();

            Console.WriteLine("Frame Count: " + FramesCount + Environment.NewLine + "Vertex Count: " + VertexCount);

            if (HasTextures)
            {
                for (int i = 0; i < VertexCount; ++i)
                {
                    TexturePositions.Add(new TexturePosition(reader));
                }
            }

            if (HasColours)
            {
                for (int i = 0; i < VertexCount; ++i)
                {
                    Colours.Add(new Colour(reader));
                }
            }

            FaceCount = reader.ReadUInt16();
            for (int i = 0; i < FaceCount; ++i)
            {
                Faces.Add(reader.ReadUInt16());
            }

            for (int i = 0; i < FramesCount; ++i)
            {
                Frames.Add(new Frame(reader, VertexCount, HasNormals));
            }

            Console.WriteLine("MDL READ: FileSize: {0}, Position: 0x{1:X8}, DataLeft: {2}", reader.BaseStream.Length, reader.BaseStream.Position, reader.BaseStream.Length - reader.BaseStream.Position);
        }