-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
143 lines (133 loc) · 4.04 KB
/
parse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// #include "Object.h"
#include "VCS.h"
#include <fstream>
#include <math.h>
void parse_sphere(int n, vector<Object*> &obj_vector, istream &fin);
void parse_triangles(int n, vector<Object*> &obj_vector, istream &fin);
vector<LightSrc> parse_light_source(int n, istream &fin);
VCS parse_vcs(istream &fin){
// cout << "inside function\n";
string temp;
VCS vcs;
fin >> temp >> vcs.v[0] >> vcs.v[1] >> vcs.v[2];
fin >> temp >> vcs.u[0] >> vcs.u[1] >> vcs.u[2];
fin >> temp >> vcs.n[0] >> vcs.n[1] >> vcs.n[2];
fin >> temp >> vcs.eye_vcs[0] >> vcs.eye_vcs[1] >> vcs.eye_vcs[2];
fin >> temp >> vcs.origin_vcs[0] >> vcs.origin_vcs[1] >> vcs.origin_vcs[2];
fin >> temp >> vcs.window[0] >> vcs.window[1] >> vcs.window[2] >> vcs.window[3];
fin >> temp >> vcs.pixel_x >> vcs.pixel_y;
fin >> temp >> vcs.Ia;
fin >> temp >> vcs.bg_color[0] >> vcs.bg_color[1] >> vcs.bg_color[2];
vcs.set_bg_color();
while((fin >> temp, temp).compare("end") != 0){
if(temp.compare("light-sources:") == 0){
int n;
fin >> n;
cout << "Light " << n << endl;
vcs.lights = parse_light_source(n,fin);
}
else if(temp.compare("spheres:") == 0){
int n;
fin >> n;
cout << "Spheres " << n << endl;
parse_sphere(n, vcs.obj_vec, fin);
}
else if(temp.compare("triangles:") == 0){
int n;
fin >> n;
cout << "Triangle " << n << endl;
parse_triangles(n, vcs.obj_vec, fin);
}
}
return vcs;
}
vector<LightSrc> parse_light_source(int n, istream &fin){
string temp;
vector<LightSrc> v;
for(int i=0; i<n; ++i){
vector<float> src(4,1);
float intensity;
fin >> temp;
fin >> src[0] >> src[1] >> src[2];
fin >> temp;
fin >> intensity;
v.emplace_back(src, intensity);
}
return v;
}
void parse_sphere(int n, vector<Object*> &obj_vector, istream &fin){
string temp;
for(int i=0; i<n; ++i){
Sphere *sphere = new Sphere();
while((fin >> temp, temp).compare("end") != 0){
if(temp.compare("c:") == 0){
float x, y, z;
fin >> x >> y >> z;
// cout << "center before\n"; sphere->t.print();
sphere->t *= Matrix(1,0,0,0, 0,1,0,0, 0,0,1,0, x,y,z,1);
// cout << "center\n"; sphere->t.print();
}
else if(temp.compare("r:") == 0){
float r;
fin >> r;
sphere->radius = r;
}
else if(temp.compare("rotate:") == 0){
float angle;
fin >> temp >> angle;
float s = sin(angle * M_PI/180);
float c = cos(angle * M_PI/180);
if(temp.compare("x") == 0) sphere->t *= Matrix(1,0,0,0, 0,c,s,0, 0,-s,c,0, 0,0,0,1);
if(temp.compare("y") == 0) sphere->t *= Matrix(c,0,-s,0, 0,1,0,0, s,0,c,0, 0,0,0,1);
if(temp.compare("z") == 0) sphere->t *= Matrix(c,s,0,0, -s,c,0,0, 0,0,1,0, 0,0,0,1);
// cout << "rotate " << temp << endl; sphere->t.print();
}
else if(temp.compare("shear:") == 0){
float c1, c2, c3, c4, c5, c6;
fin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
sphere->t *= Matrix(1,c1,c2,0, c3,1,c4,0, c5,c6,1,0, 0,0,0,1);
// cout << "shear\n"; sphere->t.print();
}
else if(temp.compare("scale:") == 0){
float x, y, z;
fin >> x >> y >> z;
sphere->t *= Matrix(x,0,0,0, 0,y,0,0, 0,0,z,0, 0,0,0,1);
// cout << "scale\n"; sphere->t.print();
}
else if(temp.compare("color:") == 0){
float r, g, b;
fin >> r >> g >> b;
sphere->set_color(r,g,b);
}
else if(temp.compare("K:") == 0){
float ka, kd, ks, s_pow;
fin >> ka >> kd >> ks >> s_pow;
sphere->k_ads[0] = ka;
sphere->k_ads[1] = kd;
sphere->k_ads[2] = ks;
fin >> sphere->kr >> sphere->kt >> sphere->eta;
sphere->s_pow = s_pow;
}
}
sphere->t.Calc_Inverse();
obj_vector.push_back(sphere);
}
}
void parse_triangles(int n, vector<Object*> &obj_vector, istream &fin){
string temp;
for (int i = 0; i < n; i++)
{
Triangle* t = new Triangle();
fin >> t->a[0] >> t->a[1] >> t->a[2];
fin >> t->b[0] >> t->b[1] >> t->b[2];
fin >> t->c[0] >> t->c[1] >> t->c[2];
int ca,cb,cc;
fin >> temp >> ca >> cb >> cc;
fin >> temp >> t->k_ads[0] >> t->k_ads[1] >> t->k_ads[2] >> t->s_pow;
fin >> t->kr >> t->kt >> t->eta;
fin >> temp;
t->Calc_Normal();
t->set_color(ca,cb,cc);
obj_vector.push_back(t);
}
}