2023-12-27 20:14:29 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
#include <philox.h>
|
2023-12-27 20:14:29 +01:00
|
|
|
|
|
|
|
|
#include "raytracer.h"
|
2023-12-29 10:59:06 +01:00
|
|
|
#include "vec3.h"
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
class RandomGen {
|
|
|
|
|
public:
|
|
|
|
|
constexpr u64 GenU64() { return philox_.Next(); }
|
|
|
|
|
// constexpr u32 GenU32() { return static_cast<u32>(GenU64()); }
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
constexpr f64 GenUniform() { return philox_.NextF64(); }
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
constexpr f64 GenUniform(f64 min, f64 max) { return min + (max - min) * GenUniform(); }
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
constexpr Vec3 GenVec3() { return Vec3{GenUniform(), GenUniform(), GenUniform()}; }
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
constexpr Vec3 GenVec3(f64 min, f64 max) {
|
|
|
|
|
return Vec3{GenUniform(min, max), GenUniform(min, max), GenUniform(min, max)};
|
|
|
|
|
}
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
constexpr Vec3 GenInUnitBall() {
|
|
|
|
|
while (true) {
|
|
|
|
|
Vec3 v = GenVec3(-1.0, 1.0);
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
if (v.norm() < 1.0) {
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-27 20:14:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
constexpr Vec3 GenOnUnitSphere() { return GenInUnitBall().normed(); }
|
|
|
|
|
|
|
|
|
|
constexpr Vec3 GenOnHemisphere(const Vec3& normal) {
|
|
|
|
|
Vec3 v = GenOnUnitSphere();
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
if (dot(v, normal) < 0) {
|
|
|
|
|
v = -v;
|
|
|
|
|
}
|
2023-12-27 20:14:29 +01:00
|
|
|
|
2023-12-29 10:59:06 +01:00
|
|
|
return v;
|
|
|
|
|
}
|
2023-12-27 20:14:29 +01:00
|
|
|
|
|
|
|
|
private:
|
2023-12-29 10:59:06 +01:00
|
|
|
Philox philox_;
|
2023-12-27 20:14:29 +01:00
|
|
|
};
|