private static void ReadVerticesSource(BinaryReader br, string groupName, int bodyPartIndex, int modelIndex, ModelData modelData, DataStructures.Models.Model model, int numMesh, long meshIndex) { br.BaseStream.Position = meshIndex; for (var i = 0; i < numMesh; i++) { var material = br.ReadInt32(); var modelOffset = br.ReadInt32(); var numVerts = br.ReadInt32(); var vertexOffset = br.ReadInt32(); var numFlexes = br.ReadInt32(); var flexIndex = br.ReadInt32(); var materialType = br.ReadInt32(); var materialParam = br.ReadInt32(); var meshId = br.ReadInt32(); var center = br.ReadCoordinateF(); var modelVertexDataPointer = br.ReadInt32(); var numLODVertices = br.ReadIntArray(8); br.ReadIntArray(8); // Unused foreach (var mm in modelData.Meshes.Where(mm => mm.BodyPart == bodyPartIndex && mm.Model == modelIndex && mm.LOD == 0 && mm.MeshIndex == meshId)) { var mesh = new Mesh(mm.LOD); foreach (var point in mm.Mesh.Points) { var vert = modelData.Vertices[point.VertexIndex + vertexOffset]; var boneWeights = new List<BoneWeighting>(); for (var j = 0; j < vert.NumBones; j++) { boneWeights.Add(new BoneWeighting(model.Bones[vert.Bones[j]], vert.BoneWeights[j])); } var mv = new MeshVertex(vert.Position, vert.Normal, boneWeights, vert.TextureS, vert.TextureT); mesh.Vertices.Add(mv); } model.AddMesh(groupName, modelIndex, mesh); } } }
private static void ReadVerticesGoldSource(BinaryReader br, string bodyPartName, int modelIndex, DataStructures.Models.Model model, int numVerts, int vertInfoIndex, int vertIndex, int numMesh, int meshIndex, int numNorms, int normInfoIndex, int normIndex) { br.BaseStream.Position = vertInfoIndex; var vertInfoData = br.ReadByteArray(numVerts); br.BaseStream.Position = normInfoIndex; var normInfoData = br.ReadByteArray(numNorms); br.BaseStream.Position = vertIndex; var vertices = br.ReadCoordinateFArray(numVerts); br.BaseStream.Position = normIndex; var normals = br.ReadCoordinateFArray(numNorms); br.BaseStream.Position = meshIndex; for (var i = 0; i < numMesh; i++) { var mesh = new Mesh(0); // GoldSource meshes don't have LODs var meshNumTris = br.ReadInt32(); var meshTriIndex = br.ReadInt32(); var meshSkinRef = br.ReadInt32(); var meshNumNorms = br.ReadInt32(); var meshNormIndex = br.ReadInt32(); mesh.SkinRef = meshSkinRef; var pos = br.BaseStream.Position; br.BaseStream.Position = meshTriIndex; int sh; // Read all the triangle strips and fans from the mesh and convert into easy-to-render 3-point triangles while ((sh = br.ReadInt16()) != 0) { var list = new List<MdlProviderSequenceDataPoint>(); var fan = sh < 0; if (fan) sh = -sh; // Negative value flags a fan, otherwise it is a strip for (var j = 0; j < sh; j++) // Read the points in the sequence { list.Add(new MdlProviderSequenceDataPoint { Vertex = br.ReadInt16(), // Vertex index in the vertices array Normal = br.ReadInt16(), // Normal index in the normals array TextureS = br.ReadInt16(), TextureT = br.ReadInt16() }); } for (var j = 0; j < list.Count - 2; j++) { // Get the vert indices to use for the various types of strip/fan // |TRIANGLE FAN | |TRIANGLE STRIP (ODD)| |TRIANGLE STRIP (EVEN)| var add = fan ? new[] {0, j + 1, j + 2} : (j % 2 == 1 ? new[] {j + 1, j, j + 2 } : new[] {j, j + 1, j + 2 }); foreach (var idx in add) { var vi = list[idx]; var boneIndex = vertInfoData[vi.Vertex]; // Vertinfo tells what bone the vert belongs to mesh.Vertices.Add(new MeshVertex( vertices[vi.Vertex], normals[vi.Normal], model.Bones[boneIndex], vi.TextureS, vi.TextureT)); } } } model.AddMesh(bodyPartName, modelIndex, mesh); br.BaseStream.Position = pos; } }