Ejemplo n.º 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");
     }
 }
Ejemplo n.º 2
0
        private static remSKICsection ReadSkin(string sectionName, int sectionLength, int numSkins, byte[] sectionBuffer)
        {
            remSKICsection skinSec = new remSKICsection(numSkins);
            int secBufIdx = 0;
            for (int subSection = 0; subSection < numSkins; 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);

                remId mesh = GetIdentifier(sectionBuffer, secBufIdx+8);
                int numWeights = BitConverter.ToInt32(sectionBuffer, secBufIdx+8+256);
                remSkin skin = new remSkin(numWeights);
                Trace.Assert(TypeCheck(remSkin.ClassType, type));
                skin.mesh = mesh;
                int weightBufIdx = secBufIdx+8+256+4;
                for (int weightIdx = 0; weightIdx < numWeights; weightIdx++)
                {
                    remBoneWeights weights = new remBoneWeights();
                    weights.bone = GetIdentifier(sectionBuffer, weightBufIdx);
                    weightBufIdx += 256;
                    int numVertIdxWts = BitConverter.ToInt32(sectionBuffer, weightBufIdx);
                    weightBufIdx += 4;

                    Matrix matrix = new Matrix();
                    for (int i = 0; i < 4; i++)
                    {
                        Vector4 row = new Vector4();
                        for (int j = 0; j < 4; j++)
                        {
                            row[j] = BitConverter.ToSingle(sectionBuffer, weightBufIdx);
                            weightBufIdx += 4;
                        }
                        matrix.set_Rows(i, row);
                    }
                    weights.matrix = matrix;

                    weights.vertexIndices = new int[numVertIdxWts];
                    for (int i = 0; i < numVertIdxWts; i++)
                    {
                        weights.vertexIndices[i] = BitConverter.ToInt32(sectionBuffer, weightBufIdx);
                        weightBufIdx += 4;
                    }
                    weights.vertexWeights = new float[weights.numVertIdxWts];
                    for (int i = 0; i < numVertIdxWts; i++)
                    {
                        weights.vertexWeights[i] = BitConverter.ToSingle(sectionBuffer, weightBufIdx);
                        weightBufIdx += 4;
                    }

                    skin.AddChild(weights);
                }

                skinSec.AddChild(skin);

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