-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSphere.h
More file actions
98 lines (76 loc) · 2.01 KB
/
Sphere.h
File metadata and controls
98 lines (76 loc) · 2.01 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef SPHERE_H
#define SPHERE_H
#include "Object3D.h"
#include <vecmath.h>
#include <cmath>
#include <iostream>
using namespace std;
///TODO:
///Implement functions and add more fields as necessary
class Sphere: public Object3D
{
public:
Sphere(){
//unit ball at the center
this->centerPoint = Vector3f(0.0f, 0.0f, 0.0f);
this->radius = 1.0f;
this->type = 'S';
}
Sphere( Vector3f center , float radius , Material* material ):Object3D(material){
this->centerPoint = center;
this->radius = radius;
this->type = 'S';
}
~Sphere(){}
virtual bool intersect( const Ray& r , Hit& h , float tmin){
Vector3f rayOrigin = r.getOrigin() - centerPoint;
//Using Equations
//Week 5 Slide 13
float a = r.getDirection().absSquared();
float b = 2.0f * Vector3f::dot(r.getDirection(), rayOrigin);
float c = Vector3f::dot(rayOrigin, rayOrigin) - (radius*radius);
float deter = (b*b) - (4*a*c);
if(deter<0) //Impossible Solution
{
return false;
}
deter = sqrt(deter);
float t1 = ((-1)*b + deter)/(2.0f*a);
float t2 = ((-1)*b - deter)/(2.0f*a);
if((t1 < tmin && t2 < tmin)) //|| deter < 0) //Bound Checking
{
return false;
}
if(t1 > h.getT() && t2 > h.getT())
{
return false;
}
if (t2 >= tmin && t1 >= tmin) //If both intersections are in front
{
//Picking the smaller one
float T = (t2<=t1 ? t2 : t1);
//cout << "T: " << T << endl;
//Gives us the normal of point of intersection
Vector3f normal = (r.pointAtParameter(T) - centerPoint).normalized();
//Updating the Hit Data Structure
h.set(T, material, normal);
return true;
}
else //Only one of the is in front
{
//Picking the one in front
float T = (t2 >= tmin ? t2 : t1);
//cout << "T: " << T << endl;
//Gives us the normal of point of intersection
Vector3f normal = (r.pointAtParameter(T) - centerPoint).normalized();
//Updating the Hit Data Structure
h.set(T, material, normal);
return true;
}
return false;
}
protected:
Vector3f centerPoint;
float radius;
};
#endif