This repository was archived by the owner on Jun 17, 2019. It is now read-only.
forked from avin1cius/Path-Tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphere.cpp
More file actions
47 lines (38 loc) · 1.32 KB
/
sphere.cpp
File metadata and controls
47 lines (38 loc) · 1.32 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
#include "sphere.h"
Sphere::Sphere( void )
{}
Sphere::Sphere( const glm::vec3 ¢er,
float radius ) :
center_{ center },
radius_{ radius }
{}
bool Sphere::intersect( const Ray &ray,
IntersectionRecord &intersection_record ) const
{
/* Ray-sphere intersection test adapted from the very efficient algorithm presented in the article:
*
* "Intersection of a Ray with a Sphere".
* Jeff Hultquist.
* Graphics Gems.
* Academic Press.
* 1990.
*/
float t1;
float t2;
glm::vec3 eo = center_ - ray.origin_;
float v = glm::dot( eo, ray.direction_ );
float disc = ( radius_ * radius_ ) - ( glm::dot( eo, eo ) - ( v * v ) );
if ( disc < 0.0f )
return false; // no intersection
else
{
float d = sqrt( disc );
t1 = v - d; // first intersection point
t2 = v + d; // second intersection point
}
// Set the intersection record
intersection_record.t_ = ( t1 > 0.00001f ) ? t1 : t2;
intersection_record.position_ = ray.origin_ + intersection_record.t_ * ray.direction_;
intersection_record.normal_ = glm::normalize( intersection_record.position_ - center_ );
return true;
}