-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
304 lines (269 loc) · 7.6 KB
/
Copy pathScene.cpp
File metadata and controls
304 lines (269 loc) · 7.6 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "Scene.h"
Color::Color(){
red = 0;
green = 0;
blue = 0;
}
Color::Color(float values[3]){
red = values[0];
green = values[1];
blue = values[2];
}
Material::Material(BRDF b){
conBRDF = b;
}
void Material::getBRDF(BRDF* brdf){
*brdf = conBRDF;
return;
}
Sampler::Sampler(int sizex, int sizey){
this->pixelSizex = sizex;
this->pixelSizey = sizey;
this->x = 0;
this->y = 0;
}
bool Sampler::getSample(Sample* sample){
if(x >= pixelSizex || y >= pixelSizey){
return false;
}
sample->x = (float)x/pixelSizex * 100.0f;
sample->y = (float)y/pixelSizey * 100.0f;
sample->xInReal = x;
sample->yInReal = y;
if(x == pixelSizex-1){
x = 0;
y++;
}else{
x++;
//std::cout << "sam.x: " << sample->x << "\n";
}
return true;
}
void Camera::GenerateRay(Sample& sample, Ray* ray){
float fovxy = pi*fov/180;
vec3 unscaled_w = eye - center;
vec3 w = glm::normalize(unscaled_w);
vec3 unscaled_u = glm::cross(up,w);
vec3 u = glm::normalize(unscaled_u);
vec3 v = glm::cross(w,u);
// std::cout << "W x: " << w[0] << " y: " << w[1] << " z: " << w[2] << "\n";
// std::cout << "U x: " << u[0] << " y: " << u[1] << " z: " << u[2] << "\n";
// std::cout << "V x: " << v[0] << " y: " << v[1] << " z: " << v[2] << "\n";
float alpha = tan(fovxy/2) * (sample.x - (100.0/2))/(100.0/2);
// std::cout << std::setprecision(15) << std::fixed << alpha << "\n" << "sample.x: " << sample.x << "\n";
float beta = tan(fovxy/2) * ((100.0/2)-sample.y)/(100.0/2);
ray->pos = eye;
ray->dir = glm::normalize(alpha * u + beta * v - w);
return;
}
Sphere::Sphere(vec3 c, float r){
center = c;
radius = r;
shape = ShapeType::Sphere;
}
bool Sphere::intersect(Ray& ray, float* thit, LocalGeo* geo){
//at^2 + bt + c = 0
float a = glm::dot(ray.dir, ray.dir);
float b = 2 * glm::dot(ray.dir, (ray.pos - center));
float c = glm::dot(ray.pos - center, ray.pos - center) - (radius * radius);
float mid_res = (b * b - 4 * a * c );
if(mid_res > 0){
//two results, take the smaller positive one;
float res1 = (-b + sqrt(mid_res))/(2*a);
float res2 = (-b - sqrt(mid_res))/(2*a);
if(res1 > 0 && res2 > 0){
if(res1 > res2){
*thit = res2;
}else{
*thit = res1;
}
}else if(res1 > 0 || res2 > 0){
if(res1 > 0){
*thit = res1;
}else{
*thit = res2;
}
}else{
//both result are 0 or negative
return false;
}
geo -> pos = ray.pos + (*thit) * ray.dir;
geo -> normal = glm::normalize(geo->pos - this->center);
//std::cout << "Sphere function intersect\n";
return true;
}
else if(mid_res == 0){
//two equal result
float res = (-b + sqrt(mid_res))/(2*a);
if(res <= 0){
return false;
}
*thit = res;
geo -> pos = ray.pos + (*thit) * ray.dir;
geo -> normal = glm::normalize(geo->pos - this->center);
//std::cout << "Sphere function intersect\n";
return true;
}
else{
//no intersection
return false;
}
}
bool Sphere::intersectP(Ray& ray){
float a = glm::dot(ray.dir, ray.dir);
float b = 2 * glm::dot(ray.dir, (ray.pos - center));
float c = glm::dot(ray.pos - center, ray.pos - center) - (radius * radius);
float mid_res = (b * b - 4 * a * c );
if(mid_res > 0){
float res1 = (-b + sqrt(mid_res))/(2*a);
float res2 = (-b - sqrt(mid_res))/(2*a);
if(res1 > 0 || res2 > 0){
return true;
}else{
return false;
}
}else if(mid_res == 0){
float res = (-b + sqrt(mid_res))/(2*a);
if(res > 0){
return true;
}
return false;
}else{
return false;
}
}
Triangle::Triangle(vec3 x, vec3 y, vec3 z){
a = x;
b = y;
c = z;
shape = ShapeType::Triangle;
}
bool Triangle::intersect(Ray& ray, float* thit, LocalGeo* geo){
vec3 normal = glm::normalize(glm::cross((c-a),(b-a)));
if(glm::dot(ray.dir,normal) == 0){
return false;
}
if(glm::dot(ray.dir, normal) < 0){
normal = vec3(0,0,0) - normal;
}
*thit = (glm::dot(a,normal) - glm::dot(ray.pos,normal))/glm::dot(ray.dir,normal);
if(*thit <= 0){
//std::cout << "less than zero" << "\n";
return false;
}
geo -> normal = normal;
geo -> pos = ray.pos + (*thit) * ray.dir;
//determine if the intersection is in the triangle
vec3 v1 = b-a;
vec3 v2 = c-a;
vec3 v3 = geo->pos - a;
float x1 = v1[0];
float x2 = v2[0];
float x3 = v3[0];
float y1 = v1[1];
float y2 = v2[1];
float y3 = v3[1];
float z1 = v1[2];
float z2 = v2[2];
float z3 = v3[2];
float beta;
float theta;
if((y2 * x1 - y1 * x2) == 0){
if((y2 * z1 - y1 * z2) == 0){
//use x and z
beta = (z2 * x3 - x2 * z3)/(z2 * x1 - x2 * z1);
}else{
//use y and z
beta = (z2 * y3 - y2 * z3)/(z2 * y1 - y2 * z1);
}
}else{
//use x and y
beta = (y2 * x3 - y3 * x2)/(y2 * x1 - y1 * x2);
}
if(beta == 0){
//no other ways work
return false;
}
if(x2 == 0){
if(y2 == 0){
//use z2
theta = (z3 - z1 * beta) / z2;
}else{
//use y2
theta = (y3 - y1 * beta) / y2;
}
}else{
//use x2
theta = (x3 - x1 * beta) / x2;
}
float alpha = 1 - beta - theta;
if(beta <= 1 && beta >= 0 && theta <= 1 && theta >= 0 && alpha <= 1 && alpha >= 0){
return true;
}else{
//std::cout << "Common: out of range theta: " << x2 << " beta: " << beta << " alpha: " << alpha << " \n";
return false;
}
}
bool Triangle::intersectP(Ray& ray){
vec3 normal = glm::normalize(glm::cross((c-a),(b-a)));
if(glm::dot(ray.dir,normal) == 0){
return false;
}
if(glm::dot(ray.dir, normal) < 0){
normal = vec3(0,0,0) - normal;
}
float thit = (glm::dot(a,normal) - glm::dot(ray.pos,normal))/glm::dot(ray.dir,normal);
if(thit <= 0){
return false;
}
vec3 intersect = ray.pos + (thit) * ray.dir;
//determine if the intersection is in the triangle
vec3 v1 = b-a;
vec3 v2 = c-a;
vec3 v3 = intersect - a;
float x1 = v1[0];
float x2 = v2[0];
float x3 = v3[0];
float y1 = v1[1];
float y2 = v2[1];
float y3 = v3[1];
float z1 = v1[2];
float z2 = v2[2];
float z3 = v3[2];
float beta;
float theta;
if((y2 * x1 - y1 * x2) == 0){
if((y2 * z1 - y1 * z2) == 0){
//use x and z
beta = (z2 * x3 - x2 * z3)/(z2 * x1 - x2 * z1);
}else{
//use y and z
beta = (z2 * y3 - y2 * z3)/(z2 * y1 - y2 * z1);
}
}else{
//use x and y
beta = (y2 * x3 - y3 * x2)/(y2 * x1 - y1 * x2);
}
if(beta == 0){
//no other ways work
return false;
}
if(x2 == 0){
if(y2 == 0){
//use z2
theta = (z3 - z1 * beta) / z2;
}else{
//use y2
theta = (y3 - y1 * beta) / y2;
}
}else{
//use x2
theta = (x3 - x1 * beta) / x2;
}
float alpha = 1 - beta - theta;
if(beta <= 1 && beta >= 0 && theta <= 1 && theta >= 0 && alpha <= 1 && alpha >= 0){
return true;
}else{
return false;
}
}