public static MSHS Load(string path) { FileInfo fi = new FileInfo(path); Logger.LogToFile(Logger.LogLevel.Info, "{0}", path); MSHS mshs = new MSHS(); using (BinaryReader br = new BinaryReader(fi.OpenRead())) { while (br.BaseStream.Position < br.BaseStream.Length) { int faces = br.ReadInt16(); int mode = br.ReadInt16(); int vertCount; TDRMesh mshMesh = new TDRMesh(); switch (mode) { case 0: // PathFollower vertCount = br.ReadInt32(); br.ReadBytes(16); for (int i = 0; i < faces; i++) { // iVertCount = br.ReadInt32() // mshMesh.BeginFace(tdrMesh.FaceMode.VertexList) // mshMesh.SetFaceNormal(New Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle())) // For j As Integer = 0 To iVertCount - 1 // mshMesh.AddFaceVertex(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()) // 'Console.WriteLine(br.ReadSingle() & ", " & br.ReadSingle() & ", " & br.ReadSingle()) // 'Console.WriteLine(br.ReadSingle() & ", " & br.ReadSingle() & ", " & br.ReadSingle()) // br.ReadBytes(12) 'Vector3 - 3x float - No idea // br.ReadBytes(12) 'Vector3 - 3x float - No idea // If br.ReadSingle() <> 1 Then MsgBox("Strange number isn't 1!") // mshMesh.AddFaceUV(br.ReadSingle(), br.ReadSingle()) // Next // mshMesh.EndFace() } break; case 256: // Car var verts = new List<Vector3>(); vertCount = br.ReadInt32(); br.ReadBytes(16); for (int i = 0; i < vertCount; i++) { verts.Add(new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle())); } for (int i = 0; i < faces; i++) { br.ReadBytes(12); //Vector3 - 3x float - Probably face normal var vp0 = verts[br.ReadInt32()]; var vp1 = verts[br.ReadInt32()]; var vp2 = verts[br.ReadInt32()]; br.ReadBytes(16); //4x float - Probably V1 colour br.ReadBytes(16); //4x float - Probably V1 colour br.ReadBytes(16); //4x float - Probably V1 colour var vt0 = new Vector2(br.ReadSingle(), br.ReadSingle()); var vt1 = new Vector2(br.ReadSingle(), br.ReadSingle()); var vt2 = new Vector2(br.ReadSingle(), br.ReadSingle()); var vn0 = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); var vn1 = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); var vn2 = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); mshMesh.Vertexes.Add(new TDRVertex(vp0.X, vp0.Y, vp0.Z, vn0.X, vn0.Y, vn0.Z, 0, vt0.X, vt0.Y)); mshMesh.Vertexes.Add(new TDRVertex(vp1.X, vp1.Y, vp1.Z, vn1.X, vn1.Y, vn1.Z, 0, vt1.X, vt1.Y)); mshMesh.Vertexes.Add(new TDRVertex(vp2.X, vp2.Y, vp2.Z, vn2.X, vn2.Y, vn2.Z, 0, vt2.X, vt2.Y)); mshMesh.Faces.Add(new TDRFace(mshMesh.Vertexes.Count - 3, mshMesh.Vertexes.Count - 2, mshMesh.Vertexes.Count - 1)); } break; case 512: // Map br.ReadBytes(16); vertCount = br.ReadInt32(); for (int i = 0; i < vertCount; i++) { mshMesh.Vertexes.Add( new TDRVertex( br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle() ) ); } for (int i = 0; i < faces; i++) { mshMesh.Faces.Add( new TDRFace( br.ReadInt32(), br.ReadInt32(), br.ReadInt32() ) ); } break; default: Logger.LogToFile(Logger.LogLevel.Error, "Unknown mode: {0}", mode); return null; } mshs.meshes.Add(mshMesh); } } return mshs; }
public static MSHS Load(string path) { FileInfo fi = new FileInfo(path); Logger.LogToFile(Logger.LogLevel.Info, "{0}", path); MSHS mshs = new MSHS(); bool singleMesh = !fi.Extension.EndsWith("s"); using (BinaryReader br = new BinaryReader(fi.OpenRead())) { while (br.BaseStream.Position < br.BaseStream.Length) { TDRMesh msh = new TDRMesh { FaceCount = br.ReadUInt16(), Mode = (TDRMesh.MSHMode)br.ReadUInt16() }; if (msh.Mode == TDRMesh.MSHMode.Tri) { br.ReadBytes(16); } msh.VertexCount = br.ReadInt32(); if (msh.Mode != TDRMesh.MSHMode.Tri) { msh.Centre = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); msh.Radius = br.ReadSingle(); } switch (msh.Mode) { case TDRMesh.MSHMode.NGon: for (int i = 0; i < msh.FaceCount; i++) { TDRFace face = new TDRFace { VertexCount = br.ReadInt32(), Normal = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()) }; for (int j = 0; j < face.VertexCount; j++) { face.Vertices.Add(new TDRVertex { Position = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()), Normal = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()), Colour = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()), UV = new Vector2(br.ReadSingle(), br.ReadSingle()) }); } msh.Faces.Add(face); } break; case TDRMesh.MSHMode.TriIndexedPosition: for (int i = 0; i < msh.VertexCount; i++) { msh.Positions.Add(new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle())); } for (int i = 0; i < msh.FaceCount; i++) { TDRFace face = new TDRFace { Normal = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()) }; TDRVertex v1 = new TDRVertex { PositionIndex = br.ReadInt32() }; TDRVertex v2 = new TDRVertex { PositionIndex = br.ReadInt32() }; TDRVertex v3 = new TDRVertex { PositionIndex = br.ReadInt32() }; v1.Colour = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); v2.Colour = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); v3.Colour = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); v1.UV = new Vector2(br.ReadSingle(), br.ReadSingle()); v2.UV = new Vector2(br.ReadSingle(), br.ReadSingle()); v3.UV = new Vector2(br.ReadSingle(), br.ReadSingle()); v1.Normal = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); v2.Normal = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); v3.Normal = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); face.Vertices.Add(v1); face.Vertices.Add(v2); face.Vertices.Add(v3); msh.Faces.Add(face); } // if (singleMesh) // { // for (int i = 0; i < vertCount; i++) // { // int pointCount = br.ReadUInt16(); // for (int j = 0; j < pointCount; j++) // { // br.ReadUInt16(); // } // } // } break; case TDRMesh.MSHMode.Tri: for (int i = 0; i < msh.VertexCount; i++) { msh.Vertices.Add(new TDRVertex { Position = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()), Colour = new Vector4(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()), UV = new Vector2(br.ReadSingle(), br.ReadSingle()) }); } for (int i = 0; i < msh.FaceCount; i++) { msh.Faces.Add(new TDRFace { V1 = br.ReadInt32(), V2 = br.ReadInt32(), V3 = br.ReadInt32() }); } foreach (TDRFace face in msh.Faces) { Vector3 v0 = msh.Vertices[face.V1].Position; Vector3 v1 = msh.Vertices[face.V2].Position; Vector3 v2 = msh.Vertices[face.V3].Position; Vector3 u = v0 - v1; Vector3 v = v0 - v2; face.Normal = Vector3.Cross(u, v).Normalised; msh.Vertices[face.V1].Normal += face.Normal; msh.Vertices[face.V2].Normal += face.Normal; msh.Vertices[face.V3].Normal += face.Normal; } break; } mshs.Meshes.Add(msh); } } return(mshs); }