Beispiel #1
0
 public remParser(Stream stream)
 {
     try
     {
         using (BinaryReader binaryReader = new BinaryReader(stream))
         {
             string[] sectionNames = { "MATC", "BONC", "MESC", "SKIC" };
             for (int sectionIdx = 0; sectionIdx < sectionNames.Length; sectionIdx++)
             {
                 string sectionName = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
                 if (sectionName.Length < 4)
                 {
                     throw new Exception("file incomplete");
                 }
                 if (sectionName != sectionNames[sectionIdx])
                 {
                     Report.ReportLog("Strange section or order for " + sectionName);
                 }
                 int sectionLength = binaryReader.ReadInt32();
                 int numSubSections = binaryReader.ReadInt32();
                 byte[] sectionBuffer = binaryReader.ReadBytes((int)sectionLength);
                 switch (sectionIdx)
                 {
                 case 0: this.MATC = ReadMaterials(sectionName, sectionLength, numSubSections, sectionBuffer); break;
                 case 1: this.BONC = ReadBones(sectionName, sectionLength, numSubSections, sectionBuffer); break;
                 case 2: this.MESC = ReadMeshes(sectionName, sectionLength, numSubSections, sectionBuffer); break;
                 case 3: this.SKIC = ReadSkin(sectionName, sectionLength, numSubSections, sectionBuffer); break;
                 }
             }
         }
     }
     catch (FileNotFoundException)
     {
         Report.ReportLog("file not found");
     }
 }
Beispiel #2
0
        private static remMESCsection ReadMeshes(string sectionName, int sectionLength, int numMeshes, byte[] sectionBuffer)
        {
            remMESCsection meshSec = new remMESCsection(numMeshes);
            int secBufIdx = 0;
            for (int subSection = 0; subSection < numMeshes; subSection++)
            {
                byte[] type = new byte[4] { sectionBuffer[secBufIdx+0], sectionBuffer[secBufIdx+1], sectionBuffer[secBufIdx+2], sectionBuffer[secBufIdx+3] };
                int length = BitConverter.ToInt32(sectionBuffer, secBufIdx+4);

                remMesh mesh = new remMesh(5);
                Trace.Assert(TypeCheck(remMesh.ClassType, type));
                mesh.frame = GetIdentifier(sectionBuffer, secBufIdx+8);

                int numMats = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256);
                mesh.name = GetIdentifier(sectionBuffer, secBufIdx+8+256+4);
                int numFaces = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256+4+256);
                int numVertices = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256+4+256 + 4);
                for (int i = 0; i < mesh.unknown.Length; i++)
                    mesh.unknown[i] = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256+4+256+8 + i*4);
                for (int i = 0; i < numMats; i++)
                {
                    remId mat = GetIdentifier(sectionBuffer, secBufIdx+8+256+4+256 + 4*4 + i*256);
                    mesh.AddMaterial(mat);
                }

                mesh.vertices = new remVertex[numVertices];
                int vertBufIdx = secBufIdx+8+256+4+256 + 4*4 + mesh.numMats*256;
                for (int i = 0; i < numVertices; i++)
                {
                    remVertex vertex = new remVertex();
                    vertex.Position = new Vector3();
                    vertex.Position[0] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 0);
                    vertex.Position[1] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 4);
                    vertex.Position[2] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 8);

                    vertex.UV = new Vector2();
                    vertex.UV[0]  = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 12);
                    vertex.UV[1]  = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 16);

                    vertex.Normal = new Vector3();
                    vertex.Normal[0] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 20);
                    vertex.Normal[1] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 24);
                    vertex.Normal[2] = BitConverter.ToSingle(sectionBuffer, vertBufIdx + 28);

                    vertex.RGBA = new Color4(BitConverter.ToInt32(sectionBuffer, vertBufIdx + 32));

                    mesh.vertices[i] = vertex;
                    vertBufIdx += 36;
                }

                mesh.faces = new int[numFaces * 3];
                int faceBufIdx = vertBufIdx;
                for (int i = 0; i < numFaces; i++)
                {
                    mesh.faces[i*3+0] = BitConverter.ToInt32(sectionBuffer, faceBufIdx + 0);
                    mesh.faces[i*3+1] = BitConverter.ToInt32(sectionBuffer, faceBufIdx + 4);
                    mesh.faces[i*3+2] = BitConverter.ToInt32(sectionBuffer, faceBufIdx + 8);
                    faceBufIdx += 12;
                }

                mesh.faceMarks = new int[numFaces];
                int faceExtraIdx = faceBufIdx;
                for (int i = 0; i < numFaces; i++)
                {
                    mesh.faceMarks[i] = BitConverter.ToInt32(sectionBuffer, faceExtraIdx);
                    faceExtraIdx += 4;
                }

                meshSec.AddChild(mesh);

                secBufIdx += length;
            }
            if (secBufIdx != sectionLength)
                Report.ReportLog("Warning! MESC section has wrong length.");
            return meshSec;
        }