-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphere.cpp
46 lines (39 loc) · 1.1 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
/*----------------------------------------------------------
* COSC363 Ray Tracer
*
* The sphere class
* This is a subclass of SceneObject, and hence implements the
* methods intersect() and normal().
-------------------------------------------------------------*/
#include "Sphere.h"
#include <math.h>
#include <iostream>
/**
* Sphere's intersection method. The input is a ray.
*/
float Sphere::intersect(glm::vec3 p0, glm::vec3 dir)
{
glm::vec3 vdif = p0 - center; //Vector s (see Slide 28)
float b = glm::dot(dir, vdif);
float len = glm::length(vdif);
float c = len*len - radius*radius;
float delta = b*b - c;
if(delta < 0.001) return -1.0; //includes zero and negative values
float t1 = -b - sqrt(delta);
float t2 = -b + sqrt(delta);
if (t1 < 0)
{
return (t2 > 0) ? t2 : -1;
}
else return t1;
}
/**
* Returns the unit normal vector at a given point.
* Assumption: The input point p lies on the sphere.
*/
glm::vec3 Sphere::normal(glm::vec3 p)
{
glm::vec3 n = p - center;
n = glm::normalize(n);
return n;
}