ccdemos/realtime_dragon.cc
branchpyrit
changeset 28 ffe83ca074f3
parent 24 d0d76e8a5203
child 35 fb170fccb19f
--- a/ccdemos/realtime_dragon.cc	Fri Dec 07 16:39:42 2007 +0100
+++ b/ccdemos/realtime_dragon.cc	Sat Dec 08 12:37:45 2007 +0100
@@ -13,7 +13,9 @@
 
 void load_ply(const char *filename, Material *mat, Float scale)
 {
-	vector<Vector3> vertices;
+	vector<NormalVertex*> vertices;
+	vector<Vector3> normals;
+	vector<int> vertex_face_num;
 	ifstream f(filename);
 	string token = "a";
 	if (!f.is_open())
@@ -37,19 +39,22 @@
 	}
 
 	// read vertices
-	Vector3 v;
-	while (vertex_num--)
+	Vector3 P;
+	int num = vertex_num;
+	while (num--)
 	{
-		f >> v.x >> v.y >> v.z;
-		v.x = -scale*v.x;
-		v.y = scale*v.y - 3.6;
-		v.z = -scale*v.z;
-		vertices.push_back(v);
+		f >> P.x >> P.y >> P.z;
+		P.x = -scale*P.x;
+		P.y = scale*P.y - 3.6;
+		P.z = -scale*P.z;
+		vertices.push_back(new NormalVertex(P));
+		normals.push_back(Vector3());
+		vertex_face_num.push_back(0);
 	}
 
 	// read faces
 	Triangle *face;
-	int num, v1, v2, v3;
+	int v1, v2, v3;
 	while (face_num--)
 	{
 		f >> num;
@@ -61,6 +66,21 @@
 		f >> v1 >> v2 >> v3;
 		face = new Triangle(vertices.at(v1), vertices.at(v2), vertices.at(v3), mat);
 		rt.addshape(face);
+		face->setSmooth();
+
+		normals.at(v1) += face->getNormal();
+		vertex_face_num.at(v1)++;
+		normals.at(v2) += face->getNormal();
+		vertex_face_num.at(v2)++;
+		normals.at(v3) += face->getNormal();
+		vertex_face_num.at(v3)++;
+	}
+
+	for (int i; i < vertex_num; i++)
+	{
+		normals.at(i) /= vertex_face_num.at(i);
+		normals.at(i).normalize();
+		vertices.at(i)->N = normals.at(i);
 	}
 
 	f.close();
@@ -119,7 +139,7 @@
 	/* initialize raytracer and prepare scene */
 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
 
-	rt.setThreads(2);
+	rt.setThreads(1);
 	rt.setMaxDepth(3);
 
 	KdTree top;