Loading src/main/java/bouncingshape/MeshLoader.java +48 −49 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MeshLoader { public static VertexBufferObject loadMeshFromFile(String filePath) throws IOException { Loading @@ -12,10 +14,10 @@ public class MeshLoader { String line; List<Vector3D> vertexList = new ArrayList<>(); List<Integer> indexList = new ArrayList<>(); int currentIndex = 0; Map<Vector3D, Integer> vertexMap = new HashMap<>(); int index = 0; while ((line = reader.readLine()) != null) { // 解析一行中的三个顶点坐标 String[] tokens = line.split("]\\s*\\["); for (String token : tokens) { String cleanedToken = token.replace("[", "").replace("]", ""); Loading @@ -24,26 +26,23 @@ public class MeshLoader { float x = Float.parseFloat(coords[0]); float y = Float.parseFloat(coords[1]); float z = Float.parseFloat(coords[2]); vertexList.add(new Vector3D(x, y, z)); indexList.add(currentIndex++); Vector3D newVertex = new Vector3D(x, y, z); if (!vertexMap.containsKey(newVertex)) { vertexList.add(newVertex); vertexMap.put(newVertex, index); indexList.add(index); index++; } else { indexList.add(vertexMap.get(newVertex)); } } } } reader.close(); // 转换列表为数组 Vector3D[] vertices = vertexList.toArray(new Vector3D[0]); int[] indices = indexList.stream().mapToInt(i -> i).toArray(); return new VertexBufferObject(vertices, indices); } public static void main(String[] args) { try { VertexBufferObject vbo = loadMeshFromFile("path/to/your/file.txt"); // 在这里使用vbo,例如渲染等 } catch (IOException e) { e.printStackTrace(); } } } Loading
src/main/java/bouncingshape/MeshLoader.java +48 −49 Original line number Diff line number Diff line Loading @@ -4,7 +4,9 @@ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MeshLoader { public static VertexBufferObject loadMeshFromFile(String filePath) throws IOException { Loading @@ -12,10 +14,10 @@ public class MeshLoader { String line; List<Vector3D> vertexList = new ArrayList<>(); List<Integer> indexList = new ArrayList<>(); int currentIndex = 0; Map<Vector3D, Integer> vertexMap = new HashMap<>(); int index = 0; while ((line = reader.readLine()) != null) { // 解析一行中的三个顶点坐标 String[] tokens = line.split("]\\s*\\["); for (String token : tokens) { String cleanedToken = token.replace("[", "").replace("]", ""); Loading @@ -24,26 +26,23 @@ public class MeshLoader { float x = Float.parseFloat(coords[0]); float y = Float.parseFloat(coords[1]); float z = Float.parseFloat(coords[2]); vertexList.add(new Vector3D(x, y, z)); indexList.add(currentIndex++); Vector3D newVertex = new Vector3D(x, y, z); if (!vertexMap.containsKey(newVertex)) { vertexList.add(newVertex); vertexMap.put(newVertex, index); indexList.add(index); index++; } else { indexList.add(vertexMap.get(newVertex)); } } } } reader.close(); // 转换列表为数组 Vector3D[] vertices = vertexList.toArray(new Vector3D[0]); int[] indices = indexList.stream().mapToInt(i -> i).toArray(); return new VertexBufferObject(vertices, indices); } public static void main(String[] args) { try { VertexBufferObject vbo = loadMeshFromFile("path/to/your/file.txt"); // 在这里使用vbo,例如渲染等 } catch (IOException e) { e.printStackTrace(); } } }