36#define JPL_RAND_SEED std::random_device{}()
40 static constexpr float JPL_SPEED_OF_SOUND = 343.0f;
41 static constexpr float JPL_INV_SPEAD_OF_SOUND = 1.0f / JPL_SPEED_OF_SOUND;
46 namespace InternalUtils
48 template<std::
floating_po
int FloatType>
49 static constexpr auto four_pi = FloatType(4.0) * std::numbers::pi_v<FloatType>;
50 template<std::
floating_po
int FloatType>
51 static constexpr FloatType two_pi = FloatType(2.0) * std::numbers::pi_v<FloatType>;
53 template<std::
floating_po
int FloatType>
54 static inline FloatType RandFloat()
56 static constexpr auto bits = std::same_as<FloatType, double> ? 53u : 24u;
58 return std::generate_canonical<FloatType, bits>(
mt);
62 static inline Vec3 RandDirection()
64 return Normalized(Vec3{
89 static auto GeometryTerm(
const Vec3&
x,
const Vec3&
xNorm,
const Vec3&
y,
const Vec3&
yNorm) -> Internal::FloatOf<Vec3>
91 using FloatType = Internal::FloatOf<Vec3>;
94 const FloatType
dist2 = LengthSquared(
dir);
95 if (
dist2 < FloatType(1
e-6))
96 return FloatType(0.0);
109 static auto GeometryTerm(
const Vec3&
x,
const Vec3&
y) -> Internal::FloatOf<Vec3>
111 using FloatType = Internal::FloatOf<Vec3>;
113 const FloatType
dist2 = LengthSquared(
y -
x);
114 if (
dist2 < FloatType(1
e-6))
115 return FloatType(0.0);
122 static auto DistanceAttenuation(
const Vec3&
x,
const Vec3&
y) -> Internal::FloatOf<Vec3>
124 return GeometryTerm(
x,
y);
134 static Vec3 SpecularReflection(
const Vec3&
incident,
const Vec3&
normal)
141 static Vec3 SampleHemisphereCosine(
const Vec3&
normal)
143 using FloatType = Internal::FloatOf<Vec3>;
146 const FloatType
u1 = InternalUtils::RandFloat<FloatType>();
147 const FloatType
u2 = InternalUtils::RandFloat<FloatType>();
148 const FloatType
r = std::sqrt(
u1);
149 const FloatType
phi = InternalUtils::two_pi<FloatType> *
u2;
151 const FloatType
x =
r * std::cos(
phi);
152 const FloatType
z =
r * std::sin(
phi);
153 const FloatType
y = std::sqrt(std::max(FloatType(0.0), FloatType(1.0) -
u1));
160 return Normalized(Vec3(
173 using FloatType = Internal::FloatOf<Vec3>;
213 if (InternalUtils::RandFloat<FloatType>() <
diffusion)
221 std::numbers::inv_pi_v<FloatType>,
249 template<std::
floating_po
int FloatType>
255 static constexpr auto epsilon =
256 std::numeric_limits<FloatType>::epsilon() * FloatType(8.0);
262 return FloatType(std::fabs(
gamma - FloatType(1.0)) <=
epsilon);
267 const FloatType
oneMinusD = FloatType(1.0) - d;
274 const FloatType
rootArg =
a2 *
gamma2 + FloatType(2.0) * d - FloatType(1.0);
283 if (d < FloatType(0.5))
285 const FloatType
gammaMin = std::sqrt((FloatType(1.0) - FloatType(2.0) * d) /
a2);
287 return FloatType(0.0);
293 if (d >= FloatType(0.5))
295 const FloatType
denom1 = InternalUtils::four_pi<FloatType> * d *
sqrtTerm;
304 const FloatType
denom = InternalUtils::two_pi<FloatType> * d *
sqrtTerm;
318 using FloatType = Internal::FloatOf<Vec3>;
#define JPL_RAND_SEED
Definition Math.h:36
std::remove_cvref_t< decltype(GetX(std::declval< Vec3 >()))> FloatOf
Definition Vec3Math.h:36
void CreateOrthonormalBasis(const Vec3 &normal, Vec3 &tangent, Vec3 &bitangent) noexcept
Definition Vec3Math.h:84
JPL_INLINE float ComputePDF(const Vec3 &normal, const Vec3 &outDirection, float diffusion, bool bIsSpecular)
Definition Math.h:237
Definition AcousticMaterial.h:36
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
Vec3 OutDirection
Definition Math.h:191
float PDF
Definition Math.h:192
bool bIsSpecular
Definition Math.h:193