|
| 1 | +package RayTracing.Hittable; |
| 2 | + |
| 3 | +import RayTracing.HitInfo.HitRecord; |
| 4 | +import RayTracing.HitInfo.Interval; |
| 5 | +import RayTracing.Material.Material; |
| 6 | +import RayTracing.Point; |
| 7 | +import RayTracing.Ray; |
| 8 | +import RayTracing.Vec2; |
| 9 | +import RayTracing.Vec3; |
| 10 | + |
| 11 | +import java.io.BufferedReader; |
| 12 | +import java.io.FileInputStream; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.io.InputStreamReader; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class Model3D implements Hittable { |
| 19 | + public List<Hittable> triangles; |
| 20 | + public AABB box; |
| 21 | + |
| 22 | + public Model3D() { |
| 23 | + box = new AABB(); |
| 24 | + triangles = new ArrayList<>(); |
| 25 | + } |
| 26 | + |
| 27 | + public void add(Triangle object) { |
| 28 | + triangles.add(object); |
| 29 | + box = new AABB(box, object.boundingBox()); |
| 30 | + } |
| 31 | + |
| 32 | + public void translate(double x, double y, double z){ |
| 33 | + box = new AABB(); |
| 34 | + Vec3 move_vec = new Vec3(x,y,z); |
| 35 | + for (Hittable triangle : triangles) { |
| 36 | + if (triangle instanceof Triangle tri) { |
| 37 | + ((Triangle) triangle).vertices[0] = tri.vertices[0].add(move_vec); |
| 38 | + ((Triangle) triangle).vertices[1] = tri.vertices[1].add(move_vec); |
| 39 | + ((Triangle) triangle).vertices[2] = tri.vertices[2].add(move_vec); |
| 40 | + box = new AABB(box, triangle.boundingBox()); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public void scale(double s){ |
| 46 | + box = new AABB(); |
| 47 | + for (int i = 0; i < triangles.size(); ++i) { |
| 48 | + if (triangles.get(i) instanceof Triangle tri) { |
| 49 | + Vec3[] new_vertexes = new Vec3[]{ |
| 50 | + tri.vertices[0].multiply(s), |
| 51 | + tri.vertices[1].multiply(s), |
| 52 | + tri.vertices[2].multiply(s) |
| 53 | + }; |
| 54 | + triangles.set(i, new Triangle(new_vertexes, tri.uvs, tri.mat)); |
| 55 | + box = new AABB(box, triangles.get(i).boundingBox()); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void rotate(double angle, int axis) { |
| 61 | + box = new AABB(); |
| 62 | + for (int i = 0; i < triangles.size(); ++i) { |
| 63 | + if (triangles.get(i) instanceof Triangle tri) { |
| 64 | + Vec3[] new_vertexes = new Vec3[]{ |
| 65 | + tri.vertices[0].rotate(angle, axis), |
| 66 | + tri.vertices[1].rotate(angle, axis), |
| 67 | + tri.vertices[2].rotate(angle, axis) |
| 68 | + }; |
| 69 | + triangles.set(i, new Triangle(new_vertexes, tri.uvs, tri.mat)); |
| 70 | + box = new AABB(box, triangles.get(i).boundingBox()); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean hit(Ray r, Interval t, HitRecord rec) { |
| 78 | + boolean hitAnything = false; |
| 79 | + for (Hittable object : triangles) { |
| 80 | + if (object.hit(r, t, rec)) { |
| 81 | + t = new Interval(t.min(), rec.t); |
| 82 | + hitAnything = true; |
| 83 | + } |
| 84 | + } |
| 85 | + return hitAnything; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public AABB boundingBox() { |
| 90 | + return box; |
| 91 | + } |
| 92 | + |
| 93 | + public static Model3D loadModel(String path, Material mat) { |
| 94 | + Model3D model = new Model3D(); |
| 95 | + |
| 96 | + try (InputStream fileData = new FileInputStream(path); |
| 97 | + InputStreamReader InputStreamReader = new InputStreamReader(fileData); |
| 98 | + BufferedReader buffer = new BufferedReader(InputStreamReader)) |
| 99 | + { |
| 100 | + String line; |
| 101 | + String[] text; |
| 102 | + int[] index = new int[3]; |
| 103 | + |
| 104 | + Point[] vertex; |
| 105 | + Vec2[] uv_data; |
| 106 | + Triangle triangle; |
| 107 | + |
| 108 | + ArrayList<Double> vertexData = new ArrayList<>(); |
| 109 | + ArrayList<Double> uData = new ArrayList<>(); |
| 110 | + ArrayList<Double> vData = new ArrayList<>(); |
| 111 | + while((line=buffer.readLine())!=null) |
| 112 | + { |
| 113 | + text = line.split("\\s+"); |
| 114 | + switch (text[0].trim()) { |
| 115 | + case "v" -> { |
| 116 | + // Vertex |
| 117 | + vertexData.add(Double.parseDouble(text[1])); |
| 118 | + //System.out.println(text[1]); |
| 119 | + vertexData.add(Double.parseDouble(text[2])); |
| 120 | + vertexData.add(Double.parseDouble(text[3])); |
| 121 | + } |
| 122 | + case "vt" -> { |
| 123 | + // UV |
| 124 | + uData.add(Double.parseDouble(text[1])); |
| 125 | + vData.add(Double.parseDouble(text[2])); |
| 126 | + } |
| 127 | + case "f" -> { |
| 128 | + // Face |
| 129 | + String[] info1, info2, info3; |
| 130 | + if (text[1].contains("/")) { |
| 131 | + info1 = text[1].split("/"); |
| 132 | + info2 = text[2].split("/"); |
| 133 | + info3 = text[3].split("/"); |
| 134 | + |
| 135 | + vertex = new Point[3]; |
| 136 | + index[0] = Integer.parseInt(info1[0]) - 1; |
| 137 | + vertex[0] = new Point(vertexData.get(index[0] * 3), vertexData.get(index[0] * 3 + 1), vertexData.get(index[0] * 3 + 2)); |
| 138 | + index[1] = Integer.parseInt(info2[0]) - 1; |
| 139 | + vertex[1] = new Point(vertexData.get(index[1] * 3), vertexData.get(index[1] * 3 + 1), vertexData.get(index[1] * 3 + 2)); |
| 140 | + index[2] = Integer.parseInt(info3[0]) - 1; |
| 141 | + vertex[2] = new Point(vertexData.get(index[2] * 3), vertexData.get(index[2] * 3 + 1), vertexData.get(index[2] * 3 + 2)); |
| 142 | + |
| 143 | + uv_data = new Vec2[3]; |
| 144 | + index[0] = Integer.parseInt(info1[1]) - 1; |
| 145 | + uv_data[0] = new Vec2(uData.get(index[0]), vData.get(index[0])); |
| 146 | + index[1] = Integer.parseInt(info2[1]) - 1; |
| 147 | + uv_data[1] = new Vec2(uData.get(index[1]), vData.get(index[1])); |
| 148 | + index[2] = Integer.parseInt(info3[1]) - 1; |
| 149 | + uv_data[2] = new Vec2(uData.get(index[2]), vData.get(index[2])); |
| 150 | + |
| 151 | + triangle = new Triangle(vertex, uv_data, mat); |
| 152 | + model.add(triangle); |
| 153 | + }else { |
| 154 | + vertex = new Point[3]; |
| 155 | + index[0] = Integer.parseInt(text[1]) - 1; |
| 156 | + vertex[0] = new Point(vertexData.get(index[0] * 3), vertexData.get(index[0] * 3 + 1), vertexData.get(index[0] * 3 + 2)); |
| 157 | + index[1] = Integer.parseInt(text[2]) - 1; |
| 158 | + vertex[1] = new Point(vertexData.get(index[1] * 3), vertexData.get(index[1] * 3 + 1), vertexData.get(index[1] * 3 + 2)); |
| 159 | + index[2] = Integer.parseInt(text[3]) - 1; |
| 160 | + vertex[2] = new Point(vertexData.get(index[2] * 3), vertexData.get(index[2] * 3 + 1), vertexData.get(index[2] * 3 + 2)); |
| 161 | + |
| 162 | + triangle = new Triangle(vertex, mat); |
| 163 | + model.add(triangle); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } catch(Exception e) { |
| 169 | + throw new RuntimeException(e); |
| 170 | + } |
| 171 | + |
| 172 | + return model; |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | + |
0 commit comments