-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec_tools.h
91 lines (74 loc) · 1.88 KB
/
vec_tools.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Created by W9990 on 2023/11/21.
//
#ifndef RAYTRACING_VEC_TOOLS_H
#define RAYTRACING_VEC_TOOLS_H
#include "glm/glm.hpp"
#include <cmath>
#include "constants.h"
using point = glm::dvec3;
using vec = glm::dvec3;
using color = glm::dvec3;
using std::sqrt;
using std::fabs;
class vec_tools
{
public:
static vec random()
{
vec a(random_double(), random_double(), random_double());
return a;
}
static vec random(double min, double max)
{
vec a(random_double(min, max), random_double(min, max), random_double(min, max));
return a;
}
static bool near_zero(vec a) {
// Return true if the vector is close to zero in all dimensions.
auto s = 1e-8;
return (fabs(a.x) < s) && (fabs(a.y) < s) && (fabs(a.z) < s);
}
};
inline vec random_in_unit_sphere()
{
while(true)
{
auto p = vec_tools::random(-1, 1);
if (glm::length(p) < 1)
return p;
}
}
inline vec random_unit_vector()
{
auto p = glm::normalize(random_in_unit_sphere());
return p;
}
inline vec random_on_hemisphere(vec& normal) {
vec on_unit_sphere = random_unit_vector();
if (glm::dot(on_unit_sphere, normal) > 0.0)
return on_unit_sphere;
else
return -on_unit_sphere;
}
vec reflect(const vec& v, const vec& n)
{
return v - 2*glm::dot(v, n) * n;
}
inline vec refract(const vec& uv, const vec& n, double etai_over_etat)
{
auto cos_theta = fmin(glm::dot(-uv, n), 1.0);
vec r_out_prep = etai_over_etat * (uv + cos_theta * n);
vec r_out_parallel = -sqrt(fabs(1.0 - glm::length(r_out_prep) * glm::length(r_out_prep))) * n;
return r_out_prep + r_out_parallel;
}
inline vec random_in_unit_disk()
{
while(true)
{
auto p = vec(random_double(-1, 1), random_double(-1, 1), 0);
if (glm::length(p) < 1)
return p;
}
}
#endif //RAYTRACING_VEC_TOOLS_H