ccdemos/realtime_dragon.cc
branchpyrit
changeset 28 ffe83ca074f3
parent 24 d0d76e8a5203
child 35 fb170fccb19f
equal deleted inserted replaced
27:e9bb83c2b8b9 28:ffe83ca074f3
    11 Raytracer rt;
    11 Raytracer rt;
    12 Camera cam;
    12 Camera cam;
    13 
    13 
    14 void load_ply(const char *filename, Material *mat, Float scale)
    14 void load_ply(const char *filename, Material *mat, Float scale)
    15 {
    15 {
    16 	vector<Vector3> vertices;
    16 	vector<NormalVertex*> vertices;
       
    17 	vector<Vector3> normals;
       
    18 	vector<int> vertex_face_num;
    17 	ifstream f(filename);
    19 	ifstream f(filename);
    18 	string token = "a";
    20 	string token = "a";
    19 	if (!f.is_open())
    21 	if (!f.is_open())
    20 	{
    22 	{
    21 		cout << "File not found: " << filename <<endl;
    23 		cout << "File not found: " << filename <<endl;
    35 				f >> face_num;
    37 				f >> face_num;
    36 		}
    38 		}
    37 	}
    39 	}
    38 
    40 
    39 	// read vertices
    41 	// read vertices
    40 	Vector3 v;
    42 	Vector3 P;
    41 	while (vertex_num--)
    43 	int num = vertex_num;
    42 	{
    44 	while (num--)
    43 		f >> v.x >> v.y >> v.z;
    45 	{
    44 		v.x = -scale*v.x;
    46 		f >> P.x >> P.y >> P.z;
    45 		v.y = scale*v.y - 3.6;
    47 		P.x = -scale*P.x;
    46 		v.z = -scale*v.z;
    48 		P.y = scale*P.y - 3.6;
    47 		vertices.push_back(v);
    49 		P.z = -scale*P.z;
       
    50 		vertices.push_back(new NormalVertex(P));
       
    51 		normals.push_back(Vector3());
       
    52 		vertex_face_num.push_back(0);
    48 	}
    53 	}
    49 
    54 
    50 	// read faces
    55 	// read faces
    51 	Triangle *face;
    56 	Triangle *face;
    52 	int num, v1, v2, v3;
    57 	int v1, v2, v3;
    53 	while (face_num--)
    58 	while (face_num--)
    54 	{
    59 	{
    55 		f >> num;
    60 		f >> num;
    56 		if (num != 3)
    61 		if (num != 3)
    57 		{
    62 		{
    59 			return;
    64 			return;
    60 		}
    65 		}
    61 		f >> v1 >> v2 >> v3;
    66 		f >> v1 >> v2 >> v3;
    62 		face = new Triangle(vertices.at(v1), vertices.at(v2), vertices.at(v3), mat);
    67 		face = new Triangle(vertices.at(v1), vertices.at(v2), vertices.at(v3), mat);
    63 		rt.addshape(face);
    68 		rt.addshape(face);
       
    69 		face->setSmooth();
       
    70 
       
    71 		normals.at(v1) += face->getNormal();
       
    72 		vertex_face_num.at(v1)++;
       
    73 		normals.at(v2) += face->getNormal();
       
    74 		vertex_face_num.at(v2)++;
       
    75 		normals.at(v3) += face->getNormal();
       
    76 		vertex_face_num.at(v3)++;
       
    77 	}
       
    78 
       
    79 	for (int i; i < vertex_num; i++)
       
    80 	{
       
    81 		normals.at(i) /= vertex_face_num.at(i);
       
    82 		normals.at(i).normalize();
       
    83 		vertices.at(i)->N = normals.at(i);
    64 	}
    84 	}
    65 
    85 
    66 	f.close();
    86 	f.close();
    67 }
    87 }
    68 
    88 
   117 	}
   137 	}
   118 
   138 
   119 	/* initialize raytracer and prepare scene */
   139 	/* initialize raytracer and prepare scene */
   120 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
   140 	render_buffer = (Float *) malloc(w*h*3*sizeof(Float));
   121 
   141 
   122 	rt.setThreads(2);
   142 	rt.setThreads(1);
   123 	rt.setMaxDepth(3);
   143 	rt.setMaxDepth(3);
   124 
   144 
   125 	KdTree top;
   145 	KdTree top;
   126 	rt.setTop(&top);
   146 	rt.setTop(&top);
   127 
   147