-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sphere.h
34 lines (26 loc) · 902 Bytes
/
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
#pragma once
#include "header/Vertex.h"
#include "header/Ray.h"
#include "header/Color.h"
class Sphere {
public:
Sphere() = default;
Sphere(const glm::vec3& c, float r, const Color& clr) :
center{ c }, radius{ r }, color{ clr } {}
float rayIntersection(const Ray& ray){
glm::vec3 dir = ray.endPoint.position - ray.startPoint.position;
dir = glm::normalize(dir); //l i lecture notes
float b = 2 * glm::dot(dir, ray.startPoint.position - center);
float c = glm::dot(ray.startPoint.position - center, ray.startPoint.position - center) - (radius * radius);
float underroot = (b / 2) * (b / 2) - c;
if (underroot < 0) //imaginary root
return std::numeric_limits<float>::max();
float d1 = -b / 2 + std::sqrt(underroot);
float d2 = -b / 2 - std::sqrt(underroot);
//take the smalle of d1, d2
return (d1 < d2) ? d1 : d2;
}
glm::vec3 center;
float radius;
Color color;
};