-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprimitive.cpp
More file actions
344 lines (266 loc) · 9.65 KB
/
Copy pathprimitive.cpp
File metadata and controls
344 lines (266 loc) · 9.65 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "primitive.h"
Primitive::~Primitive() {
}
bool Primitive::intersect(Ray& ray, float* thit, Intersection* in) {
}
bool Primitive::intersectP(Ray& ray) {
cout << "test";
}
bool Primitive::intersectE(Ray& ray, float* thit, Intersection* in, Transformation& trans) {
}
void Primitive::print() {
}
/************************************************/
// Sphere ////////////////////////////////////////
/************************************************/
Sphere::Sphere() : Primitive() {
radius = 0;
center = Vector(0, 0, 0);
type = 0;
}
Sphere::Sphere(Vector icenter, float iradius) : Primitive() {
center = icenter;
radius = iradius;
}
Sphere::Sphere(Vector icenter, float iradius, Material* imat) : Primitive() {
center = icenter;
radius = iradius;
mat = imat;
}
bool Sphere::intersect(Ray &ray, float* thit, Intersection* in) {
Vector e = ray.start;
Vector c = center;
Vector d = ray.dir;
float r = radius;
Vector e_c = Vector();
e_c.subtract(e, c);
float d_dot_d = d.dot_product(d);
float discriminant = pow(d.dot_product(e_c), 2) - (d_dot_d * (e_c.dot_product(e_c) - pow(r,2)));
if (discriminant < 0) { // the ray doesn't intersect the polygon (imaginary number)
return false;
}
float det = sqrt(discriminant);
Vector minus_d = Vector();
minus_d.scalar_multiply(d, -1.0);
float t1 = (minus_d.dot_product(e_c) + det)/d_dot_d;
float t2 = (minus_d.dot_product(e_c) - det)/d_dot_d;
// get the smallest thit value
*thit = min(t1, t2);
if(*thit < 0.00004) { // shadow bias
return false;
}
if(*thit < ray.t_min || *thit > ray.t_max) {
return false;
}
// build localGeo
Vector currPos = e + d * (*thit);
// normal = 2(p - c) or (p-c)/r
Vector normal = (currPos - c)/r;
LocalGeo local = LocalGeo(currPos, normal);
*in = Intersection(local, this);
return true;
}
bool Sphere::intersectE(Ray &ray, float* thit, Intersection* in, Transformation& trans) {
Ray transRay = Ray();
// cout << "original ray direction = "; ray.dir.print(); cout << endl;
// cout << "original ray start = "; ray.start.print(); cout << endl;
transRay.dir = trans.transform_dir(ray.dir);
// cout << "\ntransRay dir = "; transRay.dir.print(); cout << endl;
transRay.start = trans.transform_pos(ray.start); //cout << endl;
// cout << "transRay.start = "; transRay.start.print();
// intersection with the new Transformed Ray
Vector e = transRay.start;
Vector c = center;
Vector d = transRay.dir;
float r = radius;
Vector e_c = Vector();
e_c.subtract(e, c);
float d_dot_d = d.dot_product(d);
float discriminant = pow(d.dot_product(e_c), 2) - (d_dot_d * (e_c.dot_product(e_c) - pow(r,2)));
// cout << " can find the Discriminant :" << discriminant << " \n";
if (discriminant < 0) { // the ray doesn't intersect the polygon (imaginary number)
// cout << "I am false?\n";
return false;
}
float det = sqrt(discriminant);
Vector minus_d = Vector();
minus_d.scalar_multiply(d, -1.0);
float t1 = (minus_d.dot_product(e_c) + det)/d_dot_d;
float t2 = (minus_d.dot_product(e_c) - det)/d_dot_d;
// cout << "Before Segfault";
// get the smallest thit value
*thit = min(t1, t2);
if(*thit < 0.00004) { // shadow bias
return false;
}
if(*thit < ray.t_min || *thit > ray.t_max) {
return false;
}
// Build Local Geo with the old ray
Vector orig_e = ray.start;
Vector orig_d = ray.dir;
Vector currPos = orig_e + orig_d * (*thit);
//THE NORMAL MUST CHANGE
// normal = 2(p - c) or (p-c)/r
Vector normal = trans.transform_normal((currPos - c)/r);
normal.normalize();
// Vector normal = (currPos - c)/r;
LocalGeo local = LocalGeo(currPos, normal);
*in = Intersection(local, this);
return true;
}
bool Sphere::intersectP(Ray &ray) {
Vector e = ray.start;
Vector c = center;
Vector d = ray.dir;
float r = radius;
Vector e_c = Vector();
e_c.subtract(e, c);
float d_dot_d = d.dot_product(d);
float discriminant = pow(d.dot_product(e_c), 2) - (d_dot_d * (e_c.dot_product(e_c) - pow(r,2)));
if (discriminant < 0) { // the ray doesn't intersect the polygon (imaginary number)
return false;
}
float min_t;
float det = sqrt(discriminant);
Vector minus_d = Vector();
minus_d.scalar_multiply(d, -1.0);
float t1 = (minus_d.dot_product(e_c) + det)/d_dot_d;
float t2 = (minus_d.dot_product(e_c) - det)/d_dot_d;
// get the smallest thit value
min_t = min(t1, t2);
if(min_t < 0.004) { // shadow bias
return false;
}
if(min_t < ray.t_min || min_t > ray.t_max) {
return false;
}
return true;
}
void Sphere::print() {
cout << "Sphere Center: "; center.print(); cout << " Radius: " << radius << " Material: "; mat->print(); cout << endl;
}
/**************************************************/
// Triangle ////////////////////////////////////////
/**************************************************/
Triangle::Triangle() : Primitive() {
}
Triangle::Triangle(Vector iv1, Vector iv2, Vector iv3) : Primitive() {
v1 = iv1;
v2 = iv2;
v3 = iv3;
type = 1;
}
Triangle::Triangle(Vector iv1, Vector iv2, Vector iv3, Material* imat) : Primitive() {
v1 = iv1;
v2 = iv2;
v3 = iv3;
type = 1;
mat = imat;
}
bool Triangle::intersect(Ray &ray, float* thit, Intersection* in) {
float xa = v1.x; float ya = v1.y; float za = v1.z;
float xb = v2.x; float yb = v2.y; float zb = v2.z;
float xc = v3.x; float yc = v3.y; float zc = v3.z;
float xd = ray.dir.x; float yd = ray.dir.y; float zd = ray.dir.z;
float xe = ray.start.x; float ye = ray.start.y; float ze = ray.start.z;
float a = xa - xb; float b = ya - yb; float c = za - zb;
float d = xa - xc; float e = ya - yc; float f = za - zc;
float g = xd; float h = yd; float i = zd;
float j = xa - xe; float k = ya - ye; float l = za - ze;
float M = a*(e*i - h*f) + b*(g*f - d*i) + c*(d*h - e*g);
float t = ((f*(a*k - j*b) + e*(j*c - a*l) + d*(b*l - k*c))/M) * -1;
if(t < ray.t_min || t > ray.t_max) { return false; }
float gamma = (i*(a*k - j*b) + h*(j*c - a*l) + g*(b*l - k*c))/M;
if(gamma < 0 || gamma > 1) { return false; }
float beta = (j*(e*i - h*f) + k*(g*f - d*i) + l*(d*h - e*g))/M;
if(beta < 0 || beta > (1-gamma)) { return false; }
*thit = t;
if(t < 0.00004) { // shadow bias
return false;
}
// currPos = e + thit *d
Vector currPos = Vector();
Vector prod = Vector(); prod.scalar_multiply(ray.dir, *thit);
currPos.add(ray.start, prod);
// normal = cross(v2 - v1, v3 - v1);
Vector normal = Vector();
Vector V = v2 - v1;
Vector W = v3 - v1;
normal.x = (V.y * W.z) - (V.z * W.y);
normal.y = (V.z * W.x) - (V.x * W.z);
normal.z = (V.x * W.y) - (V.y * W.x);
normal.normalize();
LocalGeo local = LocalGeo(currPos, normal);
*in = Intersection(local, this);
return true;
}
bool Triangle::intersectE(Ray &ray, float* thit, Intersection* in, Transformation &trans) { Ray transRay = Ray();
// cout << "original ray direction = "; ray.dir.print(); cout << endl;
// cout << "original ray start = "; ray.start.print(); cout << endl;
transRay.dir = trans.transform_dir(ray.dir);
// cout << "\ntransRay dir = "; transRay.dir.print(); cout << endl;
transRay.start = trans.transform_pos(ray.start); cout << endl;
// cout << "transRay.start = "; transRay.start.print();
float xa = v1.x; float ya = v1.y; float za = v1.z;
float xb = v2.x; float yb = v2.y; float zb = v2.z;
float xc = v3.x; float yc = v3.y; float zc = v3.z;
float xd = ray.dir.x; float yd = ray.dir.y; float zd = ray.dir.z;
float xe = ray.start.x; float ye = ray.start.y; float ze = ray.start.z;
float a = xa - xb; float b = ya - yb; float c = za - zb;
float d = xa - xc; float e = ya - yc; float f = za - zc;
float g = xd; float h = yd; float i = zd;
float j = xa - xe; float k = ya - ye; float l = za - ze;
float M = a*(e*i - h*f) + b*(g*f - d*i) + c*(d*h - e*g);
float t = ((f*(a*k - j*b) + e*(j*c - a*l) + d*(b*l - k*c))/M) * -1;
if(t < ray.t_min || t > ray.t_max) { return false; }
float gamma = (i*(a*k - j*b) + h*(j*c - a*l) + g*(b*l - k*c))/M;
if(gamma < 0 || gamma > 1) { return false; }
float beta = (j*(e*i - h*f) + k*(g*f - d*i) + l*(d*h - e*g))/M;
if(beta < 0 || beta > (1-gamma)) { return false; }
*thit = t;
if(t < 0.00004) { // shadow bias
return false;
}
// currPos = e + thit *d
Vector currPos = Vector();
Vector prod = Vector(); prod.scalar_multiply(ray.dir, *thit);
currPos.add(ray.start, prod);
// normal = cross(v2 - v1, v3 - v1);
Vector normal = Vector();
Vector V = v2 - v1;
Vector W = v3 - v1;
normal.x = (V.y * W.z) - (V.z * W.y);
normal.y = (V.z * W.x) - (V.x * W.z);
normal.z = (V.x * W.y) - (V.y * W.x);
Vector newNormal = trans.transform_normal(normal);
newNormal.normalize();
LocalGeo local = LocalGeo(currPos, newNormal);
*in = Intersection(local, this);
return true;
}
bool Triangle::intersectP(Ray &ray) {
float xa = v1.x; float ya = v1.y; float za = v1.z;
float xb = v2.x; float yb = v2.y; float zb = v2.z;
float xc = v3.x; float yc = v3.y; float zc = v3.z;
float xd = ray.dir.x; float yd = ray.dir.y; float zd = ray.dir.z;
float xe = ray.start.x; float ye = ray.start.y; float ze = ray.start.z;
float a = xa - xb; float b = ya - yb; float c = za - zb;
float d = xa - xc; float e = ya - yc; float f = za - zc;
float g = xd; float h = yd; float i = zd;
float j = xa - xe; float k = ya - ye; float l = za - ze;
float M = a*(e*i - h*f) + b*(g*f - d*i) + c*(d*h - e*g);
float t = ((f*(a*k - j*b) + e*(j*c - a*l) + d*(b*l - k*c))/M) * -1;
if(t < ray.t_min || t > ray.t_max) { return false; }
float gamma = (i*(a*k - j*b) + h*(j*c - a*l) + g*(b*l - k*c))/M;
if(gamma < 0 || gamma > 1) { return false; }
float beta = (j*(e*i - h*f) + k*(g*f - d*i) + l*(d*h - e*g))/M;
if(beta < 0 || beta > (1-gamma)) { return false; }
if(t < 0.00004) { // shadow bias
return false;
}
return true;
}
void Triangle::print() {
cout << "Triangle v1: "; v1.print(); cout << " v2: "; v2.print(); cout << " v3: "; v3.print(); cout << endl;
}