-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sphere.cpp
65 lines (51 loc) · 1.49 KB
/
Sphere.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
/*
* File: Sphere.cpp
* Author: caos
*
* Created on June 18, 2015, 12:22 PM
*/
#include "Sphere.h"
bool Sphere::hit(Ray& r, float& t, Surface** who, Vector& normal, Point& intersect)
{
*who = this;
float D = delta(r);
if(D<0)
return false;
else
{
D=sqrt(D);
Vector d = r.getDirection();
Vector diff = Vector::add(r.getOrigin(), Vector::multiply(center, -1));
float sq_d = Vector::dot_product(d, d);
float prod = Vector::dot_product(d, diff);
float t1 = (-prod + D)/sq_d;
float t2 = (-prod - D)/sq_d;
Vector e = r.getOrigin();
if(abs(t1) >= abs(t2))
{
intersect = Vector::add(e, Vector::multiply(d, t2));
t = t2;
}
else if(abs(t1) < abs(t2))
{
intersect = Vector::add(e, Vector::multiply(d, t1));
t = t1;
}
normal = Vector::multiply(Vector::add(intersect, Vector::multiply(center, -1)), 1.f/radius);
return true;
}
}
bool Sphere::isHit(Ray& r)
{
return (delta(r) >= 0);
}
float Sphere::delta(Ray& r)
{
Vector d = r.getDirection();
Vector diff = Vector::add(r.getOrigin(), Vector::multiply(center, -1));
float sq_prod = Vector::dot_product(d, diff);
sq_prod *= sq_prod;
float sq_d = Vector::dot_product(d, d);
float sq_diff = Vector::dot_product(diff, diff) - radius*radius;
return (sq_prod - sq_diff*sq_d);
}