-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsphere.cpp
45 lines (37 loc) · 1.04 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
#include "ray.h"
#include "sphere.h"
#include "renderer.h"
#include "intersection.h"
namespace gui {
bool Sphere::intersect(
const Ray& ray,
float tMin,
float tMax,
Intersection& intersection) const {
++Renderer::nIntersects;
Vector3 co = ray.origin - center;
float a = ray.direction.lengthSquared();
float b = ray.direction.dot(co);
float c = co.lengthSquared() - radius * radius;
float delta = b * b - a * c;
if(delta < 0) return false;
delta = sqrt(delta);
float t = (-b - delta) / a;
if(t > tMin && t < tMax) {
intersection.t = t;
intersection.position = ray.origin + ray.direction * t;
intersection.normal = (intersection.position - center) / radius;
intersection.material = material;
return true;
}
t = (-b + delta) / a;
if(t > tMin && t < tMax) {
intersection.t = t;
intersection.position = ray.origin + ray.direction * t;
intersection.normal = (intersection.position - center) / radius;
intersection.material = material;
return true;
}
return false;
}
}