-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject3DSphere.cpp
38 lines (31 loc) · 897 Bytes
/
Object3DSphere.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
#include "Object3DSphere.hpp"
Object3DSphere::Object3DSphere()
{
}
Object3DSphere::Object3DSphere(Vector3D center, double radius, Material* material)
{
this->center = center;
this->radius = radius;
this->material = material;
}
double Object3DSphere::object3DIntersects(const Ray& ray) const
{
Ray rayToCenter = Ray(center, ray.origin - center, false);
double a = 1.0;
double b = 2.0 * ray.direction.dot(rayToCenter.direction);
double c = rayToCenter.direction.dot(rayToCenter.direction) - (radius * radius);
double discriminant = b * b - 4.0 * a * c;
if(discriminant >= 0)
{
double dist = (-b + sqrt(discriminant)) / (2.0 * a);
if(dist > 0)
{
return dist;
}
}
return -1.0;
}
Vector3D Object3DSphere::object3DNormal(Vector3D surfacePoint)
{
return (surfacePoint - this->center).normalize();
}