-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sphere.cc
72 lines (55 loc) · 1.82 KB
/
Sphere.cc
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
#include "Sphere.hh"
#include <iostream>
using std::cout;
Sphere::Sphere(){
material = NULL;
center = Point(0,0,0);
radius = 1.f;
}
Sphere::Sphere(Material* material, const Point& center, float radius){
this->material = material;
this->center = center;
this->radius = radius;
}
Point Sphere::GetCenter()const{ return center; }
float Sphere::GetRadius()const{ return radius; }
Point& Sphere::GetCenter(){ return center; }
float& Sphere::GetRadius(){ return radius; }
float Sphere::GetRadiusSquared()const{ return radius*radius; }
void Sphere::Preprocess(){
material->Preprocess();
}
Vector Sphere::Normal(const Point& point)const{
return (point-center).Normal();
}
void Sphere::Intersect(HitRecord& hit, const RenderContext& rc, const Ray& ray)const{
/*float a = dot(ray.d(), ray.d());
Vector v = ray.p()-p;/*Vector(ray.p().x(), ray.p().y(), ray.p().z());*/
/*float b = 2 * dot(ray.d(), v);
float c = dot(v, v) - r2();
float disc = b * b - 4 * a * c;
if (disc < 0.f){
hit.hit(std::numeric_limits<float>::infinity(), this, material);
return;
}
float distSqrt = sqrtf(disc);
float q;
if (b < 0) q = (-b - distSqrt)/2.0f;
else q = (-b + distSqrt)/2.0f;
float t0 = q / a;
float t1 = c / q;
if (t0 > t1){
float temp = t0;
t0 = t1;
t1 = temp;
}
if (t1 < 0) hit.hit(std::numeric_limits<float>::infinity(), this, material);
if (t0 < 0) hit.hit(t1, this, material);
else hit.hit(t0, this, material);*/
Vector dist = ray.Origin() - center;
float b = Dot(dist, ray.Direction());
float c = Dot(dist, dist) - GetRadiusSquared();
float d = b*b - c;
float t = d > 0 ? -b - sqrt(d) : std::numeric_limits<float>::infinity();
hit.Hit(t, this, this->material);
}