-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphere.cpp
108 lines (79 loc) · 2.11 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
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
99
100
101
102
103
104
105
106
107
#include "Sphere.h"
/**
Vector3d ctr;
double rad;
Material material;
SPHERE_T();
SPHERE_T(Vector, double, Material);
**/
using namespace std;
SPHERE_T::SPHERE_T() {
Vector temp(0.0,0.0,0.0);
Vector3d cen(temp);
Material mat();
COLOR_C black(0.0,0.0,0.0);
ctr = cen;
rad = 1.0;
material.set(&black, &black, &black, &black, 0.0, 1.0);
}
SPHERE_T::SPHERE_T(Vector *cen, double r, Material *mat){
Vector3d temp(*cen);
ctr = temp;
rad = r;
material.set(*mat);
}
void SPHERE_T::set(Vector *cen, double r, Material *mat){
Vector3d temp(*cen);
ctr = temp;
rad = r;
material.set(*mat);
}
// determines if the ray intersects the sphere or not
int SPHERE_T::spCheck(RAY ray, Collision *collide) {
double a, b, c;
double disc, t1, t2, t;
Vector tempNorm(3);
// for finding the sphere
a = ray.rayD * ray.rayD;
b = 2 * (ray.rayD * (ray.rayO - ctr));
/**
b = 2 * ( (ray.rayD.x * (ray.rayO.x - ctr.x)) +
(ray.rayD.y * (ray.rayO.y - ctr.y)) +
(ray.rayD.z * (ray.rayO.z - ctr.z)) );
**/
c = pow( (ray.rayO.x - ctr.x), 2) +
pow( (ray.rayO.y - ctr.y), 2) +
pow( (ray.rayO.z - ctr.z), 2) -
rad*rad;
/**
c = pow( (arbRay.rayO[0] - ctr[0]), 2) +
pow( (arbRay.rayO[1] - ctr[1]), 2) +
pow( (arbRay.rayO[2] - ctr[2]), 2) -
pow(rad, 2);
**/
// computing the discriminant
disc = ((b*b) - (4 * a * c));
if (disc <= 0) {
return (-1);
} else {
t1 = (-b - sqrt(disc)) / (2 * a);
t2 = (-b + sqrt(disc)) / (2 * a);
if (t1 <= 0 && t2 <= 0) {
return (-1);
} else if ((t1 > 0 && t2 <= 0) || (t1 > 0 && t2 > 0 && t1 < t2)) {
t = t1;
} else {
t = t2;
}
// finding intersection point
Vector temp(0.0,0.0,0.0);
Vector3d interP(temp);
interP = ray.rayO + (t * ray.rayD);
//finding surface normal
Vector3d surfaceNorm(temp);
surfaceNorm = (interP - ctr) / rad;
surfaceNorm = surfaceNorm.normalize();
collide->set(t, interP, surfaceNorm);
return(1);
}
}