ccdemos/common_ply.h
author Radek Brich <radek.brich@devl.cz>
Sun, 27 Apr 2008 09:44:49 +0200
branchpyrit
changeset 84 6f7fe14782c2
parent 80 907929fa9b59
child 91 9d66d323c354
permissions -rw-r--r--
prepare kd-tree traversal for packet tracing (4 rays at once) RayPacket and VectorPacket structures for packet tracing remove some redundant const's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#include <iostream>
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
#include <fstream>
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
#include <iomanip>
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
void load_ply(Raytracer &rt, const char *filename, Material *mat, Vector3 scale, Vector3 transp)
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
{
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
	vector<NormalVertex*> vertices;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
	vector<Vector3> normals;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
	vector<int> vertex_face_num;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
	ifstream f(filename);
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
	string token = "a";
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
	if (!f.is_open())
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
	{
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
		cout << "File not found: " << filename <<endl;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
		exit(1);
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
	}
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
	// read header
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
	int vertex_num, face_num;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
	while (token != "end_header")
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
	{
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
		f >> token;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
		if (token == "element")
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
		{
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
			f >> token;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
			if (token == "vertex")
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
				f >> vertex_num;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
			if (token == "face")
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
				f >> face_num;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
		}
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
		f.ignore(1000,'\n');
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	}
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
	// read vertices
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
	Vector3 P;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
	int num = vertex_num;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
	while (num--)
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
	{
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		f >> P.x >> P.y >> P.z;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
		P.x = scale.x*P.x + transp.x;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		P.y = scale.y*P.y + transp.y;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		P.z = scale.z*P.z + transp.z;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
		vertices.push_back(new NormalVertex(P));
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		normals.push_back(Vector3());
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
		vertex_face_num.push_back(0);
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
		f.ignore(1000,'\n');
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	}
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
	// read faces
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
	Triangle *face;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
	int v1, v2, v3;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
	while (face_num--)
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	{
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
		f >> num;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
		if (num != 3)
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
		{
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
			printf("ply error: faces of %d vertices not supported", num);
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
			continue;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
		}
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
		f >> v1 >> v2 >> v3;
80
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    60
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    61
		// check for invalid faces and ignore them
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    62
		if (vertices[v1]->P == vertices[v2]->P
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    63
		 || vertices[v1]->P == vertices[v3]->P
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    64
		 || vertices[v2]->P == vertices[v3]->P)
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    65
		{
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    66
			f.ignore(1000,'\n');
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    67
			continue;
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    68
		}
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    69
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    70
		face = new Triangle(vertices[v1], vertices[v3], vertices[v2], mat);
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
    71
		rt.addShape(face);
39
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
		normals.at(v1) += face->getNormal();
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
		vertex_face_num.at(v1)++;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
		normals.at(v2) += face->getNormal();
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
		vertex_face_num.at(v2)++;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
		normals.at(v3) += face->getNormal();
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
		vertex_face_num.at(v3)++;
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
		f.ignore(1000,'\n');
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
	}
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
	for (int i = 0; i < vertex_num; i++)
41
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    83
		if (vertex_face_num.at(i))
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    84
		{
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    85
			normals.at(i) /= vertex_face_num.at(i);
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    86
			normals.at(i).normalize();
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    87
			vertices.at(i)->N = normals.at(i);
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    88
		}
39
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
	f.close();
7079dcc3bd74 ccdemos: put the common code to header files, common_ply.h and common_sdl.h
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
}