This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sphere.h
53 lines (35 loc) · 1.5 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
#ifndef Sphere_h
#define Sphere_h
#include "GLObject.h"
class Sphere : public Object {
public:
Vec3f center;
float radius;
float radius2;
Sphere(Vec3f c,
float r,
Vec3f surfaceColor,
Vec3f emissionColor = BLACK,
Material material = Material(0.3, 1.0, 0.9, 50),
float transparency = 0)
: center(c), radius(r), radius2(r* r), Object(surfaceColor, emissionColor, material, transparency) {}
bool intersect(Ray& ray, float& t1, float& t2) const {
// https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
// Solução geométrica
Vec3f distance = center - ray.origin;
float distanceProjection = distance.dot(ray.direction);
if (distanceProjection < 0) return false; // não se cruzam
float distanceLength2 = distance.length2();
float rdistance2 = distanceLength2 - distanceProjection * distanceProjection;
if (rdistance2 > radius2) return false; // não se cruzam
float interception = sqrt(radius2 - rdistance2); // Metade da interceptação
t1 = distanceProjection - interception;
t2 = distanceProjection + interception;
return true;
}
// Normalizar
Vec3f nhit(Vec3f& phit) const {
return (phit - center).normalize();
}
};
#endif