-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVCS.cpp
224 lines (194 loc) · 5.99 KB
/
VCS.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "VCS.h"
VCS::VCS(){
u = vector<float>(4,1);
v = vector<float>(4,1);
n = vector<float>(4,1);
eye_vcs = vector<float>(4,1);
origin_vcs = vector<float>(4,1);
window = vector<float>(4,0);
bg_color = vector<float> (3,0);
}
void VCS::set_bg_color()
{
bg_color[0] = std::max(0.0f, std::min(255.0f, bg_color[0]));
bg_color[1] = std::max(0.0f, std::min(255.0f, bg_color[1]));
bg_color[2] = std::max(0.0f, std::min(255.0f, bg_color[2]));
}
void VCS::generate_Rays()
{
render_this = vector<vector<vector<int> > > (pixel_x, vector<vector<int> > (pixel_y, vector<int> (3)));
float del_x = (window[1] - window[0])/pixel_x;
float del_y = (window[2] - window[3])/pixel_y;
for (int i = 0; i < 3; i++)
M.t[0][i] = v[i];
for (int i = 0; i < 3; i++)
M.t[1][i] = u[i];
for (int i = 0; i < 3; i++)
M.t[2][i] = n[i];
// M is ready!
vector<float> p_e (4);
p_e[0] = window[0] - eye_vcs[0];
p_e[1] = window[2] - eye_vcs[1];
p_e[2] = - eye_vcs[2];
p_e[3] = 1;
Ray r_ij(M.Vec_Mul(p_e),M.Vec_Mul(eye_vcs));
r_ij.add_offset(origin_vcs);
r_ij.print();
vector<float> add_x (4,0);
add_x[0] = del_x;
add_x[3] = 1;
add_x = M.Vec_Mul(add_x);
vector<float> add_y (4,0);
add_y[1] = -del_y;
add_y[3] = 1;
add_y = M.Vec_Mul(add_y);
vector<float> sub_y (4,0);
sub_y[1] = del_y*(pixel_y);
sub_y[3] = 1;
// vector<float> add_x_avg (4,0);
// add_x_avg[0] = del_x/3.0;
// add_x_avg[3] = 1;
// add_x_avg = M.Vec_Mul(add_x_avg);
// vector<float> add_y_avg (4,0);
// add_y_avg[1] = -del_y/3.0;
// add_y_avg[3] = 1;
// add_y_avg = M.Vec_Mul(add_y_avg);
// vector<float> sub_y_avg (4,0);
// sub_y_avg[1] = del_y;
// sub_y_avg[3] = 1;
for (int i = 0; i < pixel_x; i++)
{
cout << i << endl;
r_ij.add_dirn(add_x);
for (int j = 0; j < pixel_y; j++)
{
// add (0,-j*dy,0,1) * M to d
r_ij.add_dirn(add_y);
// vector<float> rd = r_ij.get_d();
// for (int x = 0; x < 3; x++)
// rd[x] -= (add_x_avg[x] + add_y_avg[x]);
// // r1 : R(-1,-1)
// Ray r1 (rd, r_ij.get_p());
// vector<float> avg (3,0);
// for (int k = -1; k < 2; k++)
// {
// for (int l = -1; l < 2; l++)
// {
// auto rgb = recursive_ray_trace(r1,0);
// for (int x = 0; x < 3; x++)
// avg[x] += rgb.first[x]*rgb.second;
// r1.add_dirn(add_y_avg);
// }
// r1.add_dirn(sub_y_avg);
// r1.add_dirn(add_x_avg);
// }
// for (int k = 0; k < 3; k++)
// render_this[i][j][k] = min(float(avg[k]/9.0),(float)255.0);
// divide all by 9.
// cout << "i, j = " << i << " " << j << endl;
// r_ij.print(); cout << endl;
// RAY IN WCS:
// call rrt.
auto rgb = recursive_ray_trace(r_ij,0,1);
for (int k = 0; k < 3; k++)
render_this[i][j][k] = min(float(rgb[k]),(float)255.0);
}
r_ij.add_dirn(sub_y);
}
}
pair<Object *, pair<float, vector<float> > > VCS::intersect(Ray &r){
pair<float, vector<float> > t_min = make_pair(1000000000, vector<float>());
Object *first_obj = NULL;
for(auto ptr : obj_vec){
auto intersect = ptr->intersection(r);
if(intersect.first < eps) continue;
if(intersect.first < t_min.first){
first_obj = ptr;
t_min = intersect;
}
}
return make_pair(first_obj, t_min);
}
tuple<float,Ray,Ray> VCS::get_acc_illumination(Ray &r, pair<Object *, pair<float, vector<float> > > &input){
// auto final_color = //ambient
auto ans = input.first->k_ads[0] * Ia;
auto normal = input.first->normal(r, input.second);
// normal.print();
auto reflected = input.first->reflected(r, normal);
auto refracted = input.first->refracted(r,normal);
for (auto light : lights)
{
// Ray : p -> src
auto np = normal.get_p();
auto d = light.src;
for (int i = 0; i < 3; i++)
d[i] -= np[i];
Ray p_src(d, np);
auto l_in = intersect(p_src);
if (l_in.first != NULL)
continue;
// add Id, Is.
float kd_dot = dot(p_src,normal);
if (kd_dot >= 0.0)
{
ans += input.first->k_ads[1] * light.intensity * kd_dot;
ans += input.first->k_ads[2] * light.intensity * pow(max(dot(p_src, reflected), (float)0), input.first->s_pow);
}
}
// cout << " " << ans << endl;
return make_tuple(ans,reflected,refracted);
}
vector<float> VCS::recursive_ray_trace(Ray &r, int n, float contri){
if(n == limit) return vector<float> (3,0.0);
auto q = intersect(r);
if(q.first == NULL) return bg_color;
auto int_ray = get_acc_illumination(r, q);
vector<float> rthis (q.first->color);
mult_const(rthis,std::get<0>(int_ray));
// if (n == 0) cout << " " << intensity << endl;
float qkr = (q.first->kr);
float qkt = (q.first->kt);
if (contri*qkr > c_limit)
{
auto coh_Reflect = recursive_ray_trace(std::get<1>(int_ray), n+1,contri*qkr);
mult_const(coh_Reflect,qkr);
add_vecs(rthis,coh_Reflect);
// (std::get<1>(int_ray)).print();
}
// if (contri*qkt > c_limit)
// {
// auto coh_Refract = recursive_ray_trace(std::get<2>(int_ray), n+1, contri*qkt);
// mult_const(coh_Refract,qkt);
// add_vecs(rthis,coh_Refract);
// }
return rthis;
}
// void VCS::render()
// {
// ofstream fout("matrix.txt");
// fout << pixel_x << " " << pixel_y << "\n";
// for (int i = 0; i < pixel_x; i++)
// {
// for (int j = 0; j < pixel_y; j++)
// fout << render_this[i][j][0] << " " << render_this[i][j][1] << " " << render_this[i][j][2] << " ";
// fout << "\n";
// }
// }
void VCS::render()
{
float a1 = 1;
float a2 = 0.0;
ofstream fout("matrix.txt");
fout << (pixel_x-2) << " " << (pixel_y-2) << "\n";
for (int i = 1; i < pixel_x-1; i++)
{
for (int j = 1; j < pixel_y-1; j++){
int avg[3];
avg[0] = (a1)*render_this[i][j][0] + (a2 * 0.25)*(render_this[i+1][j+1][0] + render_this[i+1][j-1][0] + render_this[i-1][j+1][0] + render_this[i-1][j-1][0]);
avg[1] = (a1)*render_this[i][j][1] + (a2 * 0.25)*(render_this[i+1][j+1][1] + render_this[i+1][j-1][1] + render_this[i-1][j+1][1] + render_this[i-1][j-1][1]);
avg[2] = (a1)*render_this[i][j][2] + (a2 * 0.25)*(render_this[i+1][j+1][2] + render_this[i+1][j-1][2] + render_this[i-1][j+1][2] + render_this[i-1][j-1][2]);
fout << avg[0] << " " << avg[1] << " " << avg[2] << " ";
}
fout << "\n";
}
}