author | Radek Brich <radek.brich@devl.cz> |
Sun, 31 May 2009 23:06:03 +0200 | |
branch | pyrit |
changeset 100 | c005054bf4c1 |
parent 93 | 96d65f841791 |
permissions | -rw-r--r-- |
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> |
100
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
4 |
#include "raytracer.h" |
c005054bf4c1
new build target 'vcproj' - generate Visual C++ Studio project files; fix GCC warnings in intersect_packet()
Radek Brich <radek.brich@devl.cz>
parents:
93
diff
changeset
|
5 |
#include "mempool.h" |
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
|
6 |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
7 |
void load_ply(Raytracer &rt, const char *filename, Material *mat, const Vector &scale, const Vector &transp) |
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
|
8 |
{ |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
9 |
MemoryPool<Triangle> *mp_tri; |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
10 |
MemoryPool<NormalVertex> *mp_vert; |
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
|
11 |
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
|
12 |
vector<int> vertex_face_num; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
13 |
Vector *normals; |
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
|
14 |
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
|
15 |
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
|
16 |
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
|
17 |
{ |
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 |
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
|
19 |
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
|
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 |
// 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
|
22 |
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
|
23 |
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
|
24 |
{ |
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 |
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
|
26 |
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
|
27 |
{ |
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 >> 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
|
29 |
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
|
30 |
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
|
31 |
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
|
32 |
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
|
33 |
} |
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 |
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
|
35 |
} |
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 |
|
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 |
// read vertices |
91 | 38 |
Vector P; |
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
|
39 |
int num = vertex_num; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
40 |
mp_vert = new MemoryPool<NormalVertex>(vertex_num); |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
41 |
normals = new Vector[vertex_num]; |
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
|
42 |
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
|
43 |
{ |
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 |
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
|
45 |
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
|
46 |
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
|
47 |
P.z = scale.z*P.z + transp.z; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
48 |
vertices.push_back(new (mp_vert->alloc()) NormalVertex(P)); |
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
|
49 |
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
|
50 |
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
|
51 |
} |
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 |
// 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
|
54 |
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
|
55 |
int v1, v2, v3; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
56 |
mp_tri = new MemoryPool<Triangle>(face_num); |
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
|
57 |
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
|
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 >> 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
|
60 |
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
|
61 |
{ |
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
|
62 |
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
|
63 |
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
|
64 |
} |
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
|
65 |
f >> v1 >> v2 >> v3; |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
66 |
|
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
67 |
// check for invalid faces and ignore them |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
68 |
if (vertices[v1]->P == vertices[v2]->P |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
69 |
|| vertices[v1]->P == vertices[v3]->P |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
70 |
|| vertices[v2]->P == vertices[v3]->P) |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
71 |
{ |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
72 |
f.ignore(1000,'\n'); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
73 |
continue; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
74 |
} |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
75 |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
76 |
face = new (mp_tri->alloc()) 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
|
77 |
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
|
78 |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
79 |
normals[v1] += face->getNormal(); |
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
|
80 |
vertex_face_num.at(v1)++; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
81 |
normals[v2] += face->getNormal(); |
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
|
82 |
vertex_face_num.at(v2)++; |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
83 |
normals[v3] += face->getNormal(); |
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
|
84 |
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
|
85 |
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
|
86 |
} |
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
|
87 |
|
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
|
88 |
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
|
89 |
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
|
90 |
{ |
93 | 91 |
normals[i] /= (Float)vertex_face_num.at(i); |
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
92 |
normals[i].normalize(); |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
93 |
vertices.at(i)->N = normals[i]; |
41
c1080cb5bd6d
fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents:
39
diff
changeset
|
94 |
} |
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
|
95 |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
96 |
delete[] normals; |
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
|
97 |
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
|
98 |
} |