ccdemos/common_ply.h
branchpyrit
changeset 92 9af5c039b678
parent 91 9d66d323c354
child 93 96d65f841791
--- a/ccdemos/common_ply.h	Fri May 02 13:27:47 2008 +0200
+++ b/ccdemos/common_ply.h	Mon May 05 15:31:14 2008 +0200
@@ -2,11 +2,13 @@
 #include <fstream>
 #include <iomanip>
 
-void load_ply(Raytracer &rt, const char *filename, Material *mat, Vector scale, Vector transp)
+void load_ply(Raytracer &rt, const char *filename, Material *mat, const Vector &scale, const Vector &transp)
 {
+	MemoryPool<Triangle> *mp_tri;
+	MemoryPool<NormalVertex> *mp_vert;
 	vector<NormalVertex*> vertices;
-	vector<Vector> normals;
 	vector<int> vertex_face_num;
+	Vector *normals;
 	ifstream f(filename);
 	string token = "a";
 	if (!f.is_open())
@@ -33,14 +35,15 @@
 	// read vertices
 	Vector P;
 	int num = vertex_num;
+	mp_vert = new MemoryPool<NormalVertex>(vertex_num);
+	normals = new Vector[vertex_num];
 	while (num--)
 	{
 		f >> P.x >> P.y >> P.z;
 		P.x = scale.x*P.x + transp.x;
 		P.y = scale.y*P.y + transp.y;
 		P.z = scale.z*P.z + transp.z;
-		vertices.push_back(new NormalVertex(P));
-		normals.push_back(Vector());
+		vertices.push_back(new (mp_vert->alloc()) NormalVertex(P));
 		vertex_face_num.push_back(0);
 		f.ignore(1000,'\n');
 	}
@@ -48,6 +51,7 @@
 	// read faces
 	Triangle *face;
 	int v1, v2, v3;
+	mp_tri = new MemoryPool<Triangle>(face_num);
 	while (face_num--)
 	{
 		f >> num;
@@ -67,14 +71,14 @@
 			continue;
 		}
 
-		face = new Triangle(vertices[v1], vertices[v3], vertices[v2], mat);
+		face = new (mp_tri->alloc()) Triangle(vertices[v1], vertices[v3], vertices[v2], mat);
 		rt.addShape(face);
 
-		normals.at(v1) += face->getNormal();
+		normals[v1] += face->getNormal();
 		vertex_face_num.at(v1)++;
-		normals.at(v2) += face->getNormal();
+		normals[v2] += face->getNormal();
 		vertex_face_num.at(v2)++;
-		normals.at(v3) += face->getNormal();
+		normals[v3] += face->getNormal();
 		vertex_face_num.at(v3)++;
 		f.ignore(1000,'\n');
 	}
@@ -82,10 +86,11 @@
 	for (int i = 0; i < vertex_num; i++)
 		if (vertex_face_num.at(i))
 		{
-			normals.at(i) /= vertex_face_num.at(i);
-			normals.at(i).normalize();
-			vertices.at(i)->N = normals.at(i);
+			normals[i] /= vertex_face_num.at(i);
+			normals[i].normalize();
+			vertices.at(i)->N = normals[i];
 		}
 
+	delete[] normals;
 	f.close();
 }