-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsphere.cpp
More file actions
80 lines (67 loc) · 1.97 KB
/
Copy pathsphere.cpp
File metadata and controls
80 lines (67 loc) · 1.97 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
#include "sphere.h"
Sphere::Sphere() {
radius = 0;
center = Vector(0, 0, 0);
}
Sphere::Sphere(Vector icenter, float iradius) {
center = icenter;
radius = iradius;
}
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);
// build localGeo
// currPos = e + thit *d
Vector currPos = Vector();
Vector prod = Vector(); prod.scalar_multiply(d, *thit);
currPos.add(e, prod);
// normal = 2(p - c)/R
Vector normal = Vector();
normal.subtract(currPos, c);
//normal.scalar_multiply(normal, 2.0f);
normal.scalar_divide(normal, 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, t1);
if(min_t < 0.004) { // shadow bias
return false;
}
return true;
}