-
Notifications
You must be signed in to change notification settings - Fork 7
/
sphere.h
54 lines (41 loc) · 1.36 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
#ifndef _SPHERE_H_
#define _SPHERE_H_
#include <cmath>
#include "vec.h"
#include "ray.h"
#include "material.h"
#include "constant.h"
#include "intersection.h"
namespace edubpt {
struct Sphere {
double radius;
Vec position;
Color emission;
Color color;
ReflectionType reflection_type;
Sphere(const double radius, const Vec &position, const Color &emission, const Color &color, const ReflectionType reflection_type) :
radius(radius), position(position), emission(emission), color(color), reflection_type(reflection_type) {}
// 入力のrayに対する交差点までの距離を返す。交差しなかったら0を返す。
// rayとの交差判定を行う。交差したらtrue,さもなくばfalseを返す。
bool intersect(const Ray &ray, Hitpoint *hitpoint) const {
const Vec p_o = position - ray.org;
const double b = dot(p_o, ray.dir);
const double D4 = b * b - dot(p_o, p_o) + radius * radius;
if (D4 < 0.0)
return false;
const double sqrt_D4 = sqrt(D4);
const double t1 = b - sqrt_D4, t2 = b + sqrt_D4;
if (t1 < kEPS && t2 < kEPS)
return false;
if (t1 > kEPS) {
hitpoint->distance = t1;
} else {
hitpoint->distance = t2;
}
hitpoint->position = ray.org + hitpoint->distance * ray.dir;
hitpoint->normal = (hitpoint->position - position) / radius; // 正規化して法線を得る
return true;
}
};
};
#endif