-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.h
59 lines (50 loc) · 1.46 KB
/
sphere.h
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
/*------------------------------
* Sphere.h
* Description: defines a sphere primitive, and allows it to be rendered in the scene.
*
* By: Harrison Walters
-------------------------------*/
#pragma once
#include "object.h"
class Sphere : public Object
{
public:
Sphere() {
}
Sphere(Vector3 Center_in, float Radius_in, Vector3 colour_in, bool reflective_in)
{
Center = Center_in;
Radius = Radius_in;
colour = colour_in;
isLight = false;
reflective = reflective_in;
}
virtual bool Intersect(Vector3 Origin, Vector3 Direction,
float *t_out, Vector3 *normal_out)
{
Vector3 EO = Minus(Center, Origin);
float v = DotProduct(EO, Direction);
float RadiusSquare = Radius * Radius;
float EO_Square = DotProduct(EO, EO);
float discriminant = RadiusSquare - (EO_Square - v * v);
float t = -1;
if (discriminant > 0)
{
float d = sqrt(discriminant);
t = v - d;
}
if (t > 0) {
*t_out = t;
Vector3 IntersectionPoint = MultiplyScalar(Direction, t);
IntersectionPoint = Add(IntersectionPoint, Origin);
Vector3 SurfaceNormal = Minus(IntersectionPoint, Center);
(*normal_out) = Normalize(SurfaceNormal);
return true;
} else
{
return false;
}
}
Vector3 Center;
float Radius;
};