Exemple #1
0
        private void ReadIndexBuffers(DataReader r)
        {
            r.Skip(4);
            var size             = r.ReadInt32();
            int IndexBufferCount = r.ReadInt32();

            if (size - 0xC > 0)
            {
                for (int c = 0; c < IndexBufferCount; c++)
                {
                    r.Align(4);
                    int fCount    = r.ReadInt32();
                    var indBuffer = new ushort[fCount];
                    Console.WriteLine(r.ReadInt32() + " " + r.ReadInt32());
                    for (int i = 0; i < fCount; i++)
                    {
                        indBuffer[i] = r.ReadUInt16();
                    }
                    IndexBuffers.Add(indBuffer);
                }
            }
        }
Exemple #2
0
        private void ReadRES(DataReader r)
        {
            if (new string(r.ReadChars(4)) != "$RSF")
            {
                Console.WriteLine("error");
            }

            int resourceCount1 = r.ReadInt32();

            r.ReadInt32();
            r.ReadInt32();
            int resourceCount2 = r.ReadInt32();

            r.ReadInt32();
            r.ReadInt32();
            r.ReadInt32();
            string name = r.ReadString(r.Position, -1);

            r.Position += (uint)name.Length;
            r.Align(0x10);

            Console.WriteLine(name);

            VTX prevVTX = null;
            TXR prevTXR = null;

            while (true)
            {
                var start = r.Position;

                if (r.Position >= r.Length)
                {
                    break;
                }

                r.BigEndian = true;
                string key   = r.ReadString(4);
                var    size  = r.ReadUInt32();
                var    flag1 = r.ReadInt32();
                var    flag2 = r.ReadInt32();

                var sectionStart = r.Position;

                r.BigEndian = false;

                Console.WriteLine(start.ToString("X") + " " + key + " " + size.ToString("X"));

                switch (key)
                {
                case "$CT0":
                    r.Position += 12;
                    break;

                case "$VTX":
                    prevVTX = new VTX(r);
                    VTXs.Add(prevVTX);
                    break;

                case "$TXR":
                    prevTXR = new TXR(r);
                    TXRs.Add(prevTXR);
                    break;

                case "$TXI":
                    break;

                case "$RSI":
                    if (prevVTX != null)
                    {
                        prevVTX.ReadResource(r);
                    }
                    if (prevTXR != null)
                    {
                        prevTXR.ReadResource(r);
                    }
                    prevVTX = null;
                    prevTXR = null;
                    break;

                case "$SKL":
                {
                    var f = r.ReadInt32();
                    r.ReadInt16();         // dunno
                    var boneCount = r.ReadInt16();
                    r.ReadInt16();         // dunno
                    r.ReadInt16();         // dunno
                    Stack <int> boneStack = new Stack <int>();
                    for (int i = 0; i < boneCount; i++)
                    {
                        GenericBone bone = new GenericBone();
                        bone.Name        = r.ReadString(sectionStart + r.ReadUInt32(), -1);
                        bone.ParentIndex = boneStack.Count > 0 ? boneStack.Peek() : -1;
                        r.ReadByte();
                        var depth = r.ReadByte();
                        var pop   = r.ReadSByte() + 1;
                        r.ReadByte();
                        boneStack.Push(i);
                        for (int j = 0; j < pop; j++)
                        {
                            boneStack.Pop();
                        }
                        bone.Transform = new OpenTK.Matrix4(r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), 0,
                                                            r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), 0,
                                                            r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), 0,
                                                            r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), 1);
                        r.Skip(0x48);
                        Skeleton.Bones.Add(bone);
                    }
                }
                break;

                case "$SCN":

                    break;

                case "$MSH":

                    break;

                case "$TRE":

                    break;

                case "$CLT":

                    break;
                }

                r.Seek(start + size + 0x10);

                if (r.Position % 16 != 0)
                {
                    r.Position += 16 - (r.Position % 16);
                }
            }
        }
Exemple #3
0
        public void Open(FileItem File)
        {
            using (DataReader r = new DataReader(File))
            {
                r.BigEndian = false;
                r.Seek(0x34);
                var skelTableOffset = r.Position + r.ReadUInt32();

                Model          = new GenericModel();
                Model.Skeleton = new GenericSkeleton();
                r.Seek(skelTableOffset + 8);
                var boneCount = r.ReadInt32();
                for (int i = 0; i < boneCount; i++)
                {
                    var temp = r.Position + 4;
                    r.Seek(r.Position + r.ReadUInt32());
                    //r.PrintPosition();
                    var unk1 = r.ReadInt32();
                    var unk2 = r.ReadInt32();

                    GenericBone bone = new GenericBone();
                    bone.Transform = new OpenTK.Matrix4(r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle(),
                                                        r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle(),
                                                        r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle(),
                                                        r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle());

                    var boneInfoOffset = r.Position + r.ReadUInt32();
                    var boneFlag       = r.ReadInt32();
                    var parentCount    = r.ReadInt32();
                    var childCount     = r.ReadUInt32();
                    r.Skip(4 * childCount); // parent count intofmration

                    r.Seek(boneInfoOffset);
                    var parentName = r.ReadString(r.ReadInt32());
                    r.Skip(1); r.Align(4);
                    bone.Name = r.ReadString(r.ReadInt32());
                    r.Skip(1); r.Align(4);

                    if (parentName != "")
                    {
                        bone.ParentIndex = Model.Skeleton.IndexOf(Model.Skeleton.Bones.Find(e => e.Name == parentName));
                    }

                    Model.Skeleton.Bones.Add(bone);

                    r.Seek(temp);
                }

                r.Seek(0x48);
                var objectCount = r.ReadInt32();
                var unkOffset   = r.Position + r.ReadUInt32();
                var vcount      = 0;
                for (int i = 0; i < objectCount - 1; i++)
                {
                    GenericMesh mesh = new GenericMesh();
                    Model.Meshes.Add(mesh);

                    var temp = r.Position + 4;
                    r.Seek(r.Position + r.ReadUInt32());
                    r.Skip(8);
                    mesh.Name = r.ReadString(r.Position + r.ReadUInt32() + 4, -1);
                    var nameOff2      = r.Position + r.ReadUInt32();
                    var parentBoneOff = r.Position + r.ReadUInt32();
                    mesh.MaterialName = r.ReadString(r.Position + r.ReadUInt32() + 12, -1);
                    var transform = new OpenTK.Matrix4(r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle(),
                                                       r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle(),
                                                       r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle(),
                                                       r.ReadSingle(), r.ReadSingle(), r.ReadSingle(), r.ReadSingle());

                    var boneTableOffset = r.Position + r.ReadUInt32(); // bonetable offset
                    r.ReadInt32();                                     // some matrix table?

                    r.Skip(0x18);                                      // floats

                    var somecount        = r.ReadInt32();
                    var indexTableOffset = r.Position + r.ReadUInt32();
                    var attributeBuffer  = r.Position + r.ReadUInt32();
                    var vertCount        = r.ReadInt32();
                    var primType         = r.ReadInt32();
                    var unkownOffset2    = r.ReadUInt32();

                    if (unkownOffset2 > 0xFFFF)
                    {
                        r.ReadInt32();
                    }
                    else
                    {
                        r.ReadInt16();
                    }

                    var t = r.Position;
                    r.Seek(attributeBuffer);
                    var attributes = r.ReadStructArray <AttributeBuffer>(r.ReadInt32());
                    foreach (var a in attributes)
                    {
                        Console.WriteLine(a.Format + " " + a.Type + " " + a.Size);
                    }
                    r.Seek(t);

                    for (int v = 0; v < vertCount; v++)
                    {
                        var vert = new GenericVertex();

                        foreach (var a in attributes)
                        {
                            float[] values = new float[a.Size];
                            for (int vi = 0; vi < values.Length; vi++)
                            {
                                switch (a.Format)
                                {
                                case 0x00:
                                    values[vi] = r.ReadSingle();
                                    break;

                                case 0x07:
                                    values[vi] = r.ReadInt16() / (float)short.MaxValue;
                                    break;

                                case 0x06:
                                    values[vi] = r.ReadSByte();
                                    break;

                                case 0x0B:
                                    values[vi] = r.ReadUInt16() / (float)ushort.MaxValue;
                                    break;

                                case 0x0E:
                                    values[vi] = r.ReadByte() / (float)255;
                                    break;

                                default:
                                    throw new NotSupportedException("Unknown Attribute Format " + a.Format);
                                }
                            }

                            switch (a.Type)
                            {
                            case 0:
                                vert.Pos = new OpenTK.Vector3(values[0], values[1], values[2]);
                                break;

                            case 1:
                                vert.Clr = new OpenTK.Vector4(values[0], values[1], values[2], values[3]);
                                break;

                            case 3:
                                vert.UV0 = new OpenTK.Vector2(values[0], values[1]);
                                break;

                            case 15:
                                vert.Bones = new OpenTK.Vector4(values[0], values[1], values[2], values[3]);
                                break;

                            case 16:
                                vert.Weights = new OpenTK.Vector4(values[0], values[1], values[2], values[3]);
                                break;
                                //default:
                                //    throw new NotSupportedException("Unknown vertex attribute " + a.Type);
                            }
                        }

                        mesh.Vertices.Add(vert);
                    }

                    r.Seek(indexTableOffset);
                    var indexBufferSize = r.ReadInt32();
                    var indexCount      = (indexBufferSize - 6) / 2;
                    if (indexBufferSize > 0xFFFF)
                    {
                        r.ReadInt32();
                    }
                    else
                    {
                        r.ReadInt16();
                    }

                    for (int j = 0; j < indexCount; j++)
                    {
                        var v = r.ReadUInt16();
                        mesh.Triangles.Add(mesh.VertexCount < 0xFF ? (uint)(v & 0xFF) : v);
                    }

                    //r.PrintPosition();
                    Console.WriteLine(mesh.Name + " " + mesh.VertexCount.ToString("X") + " " + mesh.Triangles.Min().ToString("X") + " " + mesh.Triangles.Max().ToString("X"));
                    //mesh.PrimitiveType = OpenTK.Graphics.OpenGL.PrimitiveType.TriangleStrip;

                    r.Seek(temp);
                }
            }
        }
Exemple #4
0
        private void ReadMaterial(DataReader r)
        {
            r.PrintPosition();
            Console.WriteLine(r.ReadInt32()); // 9
            // Material Section
            {
                r.Skip(4);
                uint Size = r.ReadUInt32();
                r.Skip(Size - 8);
            }
            {
                r.Skip(4);
                uint Size  = r.ReadUInt32();
                uint ss    = r.Position + Size - 8;
                int  Count = r.ReadInt32();

                for (int i = 0; i < Count; i++)
                {
                    G1TextureBank material = new G1TextureBank();
                    //3ds
                    if (type.Equals("3DS"))
                    {
                        r.Skip(0x10);
                        material.DiffuseTextureIndex = r.ReadInt32();
                        r.Skip(0x14);
                    }
                    else
                    //switch
                    {
                        r.PrintPosition();
                        r.Skip(0x4);
                        int texCount = r.ReadInt32();
                        r.Skip(8);
                        if (texCount > 0)
                        {
                            material.DiffuseTextureIndex = r.ReadInt16();
                            r.Skip((uint)(12 * texCount - 2));
                        }
                    }
                    TextureBanks.Add(material);
                    //Console.WriteLine("Mat" + Materials[i]);
                }
                r.Seek(ss);
            }
            {
                r.Skip(4);
                uint Size = r.ReadUInt32();
                uint ss   = r.Position + Size - 8;

                int Count = r.ReadInt32();

                for (int i = 0; i < Count; i++)
                {
                    int paramCount = r.ReadInt32();

                    G1Material material = new G1Material();

                    //Console.WriteLine("Material " + i);

                    for (int j = 0; j < paramCount; j++)
                    {
                        uint length    = r.ReadUInt32();
                        uint nextParam = r.Position - 4 + length;
                        int  unkCount1 = r.ReadInt32();
                        int  unkCount2 = r.ReadInt32();
                        int  count     = r.ReadInt16();
                        int  type      = r.ReadInt16();

                        string name = r.ReadString(r.Position, -1);
                        r.Skip((uint)name.Length);
                        r.Align(4);

                        //Console.WriteLine("\tParam " + name + " " + j + " " + type + " " + count);

                        object obj = null;

                        if (type == 1)
                        {
                            var val = new float[count];
                            for (int k = 0; k < count; k++)
                            {
                                val[k] = r.ReadSingle();
                            }
                            obj = val;
                        }
                        else
                        {
                            Console.WriteLine("Unknown Material Type: " + r.Position.ToString("X") + " " + type);
                        }

                        material.Parameters.Add(name, obj);

                        r.Seek(nextParam);
                    }

                    Materials.Add(material);
                }

                r.Seek(ss);
            }
        }