aboutsummaryrefslogtreecommitdiffstats
path: root/src/vec3.cpp
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2020-12-21 01:26:14 +0100
committerladevieq <ladevie.quentin@gmail.com>2020-12-21 01:26:14 +0100
commit92d66138f53645f3400f633c9821a6fa36f589e0 (patch)
treea23bc2f45afa5858095ba5525a61aad530fdddd6 /src/vec3.cpp
parent25ac4fd14409a0d82bbaaaf4018674c3bdd46b03 (diff)
Add lambertian reflection to spheres
Diffstat (limited to 'src/vec3.cpp')
-rw-r--r--src/vec3.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/vec3.cpp b/src/vec3.cpp
index f4470dc..f5b6909 100644
--- a/src/vec3.cpp
+++ b/src/vec3.cpp
@@ -1,4 +1,5 @@
#include "vec3.hpp"
+#include "rt.hpp"
vec3::vec3(): x(0.0), y(0.0), z(0.0) {}
@@ -60,6 +61,14 @@ vec3 vec3::unit() const {
return *this / length();
}
+vec3 vec3::random() {
+ return vec3 { randd(), randd(), randd() };
+}
+
+vec3 vec3::random(double min, double max) {
+ return vec3 { randd(min, max), randd(min, max), randd(min, max) };
+}
+
vec3 lerp(vec3 u, vec3 v, double t) {
return (1.0 - t) * u + t * v;
}
@@ -95,3 +104,24 @@ vec3 operator/(vec3 u, double scale) {
std::ostream& operator<<(std::ostream& out, vec3 vec) {
return out << vec.x << ' ' << vec.y << ' ' << vec.z << std::endl;
}
+
+// TODO: Use something else to find random point onto a sphere
+vec3 random_in_unit_sphere() {
+ while (true) {
+ auto p = vec3::random(-1,1);
+ if (p.length_sq() >= 1) continue;
+ return p;
+ }
+}
+
+vec3 random_unit_vector() {
+ return random_in_unit_sphere().unit();
+}
+
+vec3 random_in_hemisphere(const vec3& normal) {
+ vec3 in_unit_sphere = random_in_unit_sphere();
+ if (in_unit_sphere.dot(normal) > 0.0) // In the same hemisphere as the normal
+ return in_unit_sphere;
+ else
+ return -in_unit_sphere;
+}