public static RenderMesh LoadFromFile(string filePath) { var result = new RenderMesh(); var objData = ObjLoader.LoadObjFile(filePath); var lastTriangle = 0; var meshPartsInfo = LoadHeadMeshes(objData, 1.0f, ref lastTriangle); Vector3 A = new Vector3(99999.0f, 99999.0f, 99999.0f); Vector3 B = new Vector3(-99999.0f, -99999.0f, -99999.0f); foreach (var meshPartInfo in meshPartsInfo) { foreach (var p in meshPartInfo.VertexPositions) { A.X = Math.Min(A.X, p.X); A.Y = Math.Min(A.Y, p.Y); A.Z = Math.Min(A.Z, p.Z); B.X = Math.Max(B.X, p.X); B.Y = Math.Max(B.Y, p.Y); B.Z = Math.Max(B.Z, p.Z); } } Vector3 Center = (A + B) * 0.5f; foreach (var meshPartInfo in meshPartsInfo) { var meshPart = new MeshPart(); if (meshPart.Create(meshPartInfo, -Center)) { result.UpdateAABB(meshPart); result.Parts.Add(meshPart); } } foreach (var part in result.Parts) { part.UpdateBuffers(); } return(result); }
public void UpdateAABB(MeshPart part) { var a = AABB.A; var b = AABB.B; foreach (var vertex in part.Vertices) { a.X = Math.Min(vertex.Position.X, a.X); b.X = Math.Max(vertex.Position.X, b.X); a.Y = Math.Min(vertex.Position.Y, a.Y); b.Y = Math.Max(vertex.Position.Y, b.Y); a.Z = Math.Min(vertex.Position.Z, a.Z); b.Z = Math.Max(vertex.Position.Z, b.Z); } AABB.A = a; AABB.B = b; }