equal
deleted
inserted
replaced
1 #include <iostream> |
1 #include <iostream> |
2 #include <fstream> |
2 #include <fstream> |
3 #include <iomanip> |
3 #include <iomanip> |
4 |
4 |
5 void load_ply(Raytracer &rt, const char *filename, Material *mat, Vector scale, Vector transp) |
5 void load_ply(Raytracer &rt, const char *filename, Material *mat, const Vector &scale, const Vector &transp) |
6 { |
6 { |
|
7 MemoryPool<Triangle> *mp_tri; |
|
8 MemoryPool<NormalVertex> *mp_vert; |
7 vector<NormalVertex*> vertices; |
9 vector<NormalVertex*> vertices; |
8 vector<Vector> normals; |
|
9 vector<int> vertex_face_num; |
10 vector<int> vertex_face_num; |
|
11 Vector *normals; |
10 ifstream f(filename); |
12 ifstream f(filename); |
11 string token = "a"; |
13 string token = "a"; |
12 if (!f.is_open()) |
14 if (!f.is_open()) |
13 { |
15 { |
14 cout << "File not found: " << filename <<endl; |
16 cout << "File not found: " << filename <<endl; |
31 } |
33 } |
32 |
34 |
33 // read vertices |
35 // read vertices |
34 Vector P; |
36 Vector P; |
35 int num = vertex_num; |
37 int num = vertex_num; |
|
38 mp_vert = new MemoryPool<NormalVertex>(vertex_num); |
|
39 normals = new Vector[vertex_num]; |
36 while (num--) |
40 while (num--) |
37 { |
41 { |
38 f >> P.x >> P.y >> P.z; |
42 f >> P.x >> P.y >> P.z; |
39 P.x = scale.x*P.x + transp.x; |
43 P.x = scale.x*P.x + transp.x; |
40 P.y = scale.y*P.y + transp.y; |
44 P.y = scale.y*P.y + transp.y; |
41 P.z = scale.z*P.z + transp.z; |
45 P.z = scale.z*P.z + transp.z; |
42 vertices.push_back(new NormalVertex(P)); |
46 vertices.push_back(new (mp_vert->alloc()) NormalVertex(P)); |
43 normals.push_back(Vector()); |
|
44 vertex_face_num.push_back(0); |
47 vertex_face_num.push_back(0); |
45 f.ignore(1000,'\n'); |
48 f.ignore(1000,'\n'); |
46 } |
49 } |
47 |
50 |
48 // read faces |
51 // read faces |
49 Triangle *face; |
52 Triangle *face; |
50 int v1, v2, v3; |
53 int v1, v2, v3; |
|
54 mp_tri = new MemoryPool<Triangle>(face_num); |
51 while (face_num--) |
55 while (face_num--) |
52 { |
56 { |
53 f >> num; |
57 f >> num; |
54 if (num != 3) |
58 if (num != 3) |
55 { |
59 { |
65 { |
69 { |
66 f.ignore(1000,'\n'); |
70 f.ignore(1000,'\n'); |
67 continue; |
71 continue; |
68 } |
72 } |
69 |
73 |
70 face = new Triangle(vertices[v1], vertices[v3], vertices[v2], mat); |
74 face = new (mp_tri->alloc()) Triangle(vertices[v1], vertices[v3], vertices[v2], mat); |
71 rt.addShape(face); |
75 rt.addShape(face); |
72 |
76 |
73 normals.at(v1) += face->getNormal(); |
77 normals[v1] += face->getNormal(); |
74 vertex_face_num.at(v1)++; |
78 vertex_face_num.at(v1)++; |
75 normals.at(v2) += face->getNormal(); |
79 normals[v2] += face->getNormal(); |
76 vertex_face_num.at(v2)++; |
80 vertex_face_num.at(v2)++; |
77 normals.at(v3) += face->getNormal(); |
81 normals[v3] += face->getNormal(); |
78 vertex_face_num.at(v3)++; |
82 vertex_face_num.at(v3)++; |
79 f.ignore(1000,'\n'); |
83 f.ignore(1000,'\n'); |
80 } |
84 } |
81 |
85 |
82 for (int i = 0; i < vertex_num; i++) |
86 for (int i = 0; i < vertex_num; i++) |
83 if (vertex_face_num.at(i)) |
87 if (vertex_face_num.at(i)) |
84 { |
88 { |
85 normals.at(i) /= vertex_face_num.at(i); |
89 normals[i] /= vertex_face_num.at(i); |
86 normals.at(i).normalize(); |
90 normals[i].normalize(); |
87 vertices.at(i)->N = normals.at(i); |
91 vertices.at(i)->N = normals[i]; |
88 } |
92 } |
89 |
93 |
|
94 delete[] normals; |
90 f.close(); |
95 f.close(); |
91 } |
96 } |