|
11 | 11 | #include "numericTraits.h"
|
12 | 12 | #include <random>
|
13 | 13 |
|
14 |
| -namespace rev { |
15 |
| - namespace math { |
16 |
| - class SNoise |
| 14 | +namespace rev::math { |
| 15 | + class SNoise |
| 16 | + { |
| 17 | + public: |
| 18 | + // Simplex noise |
| 19 | + //static void setSimplexSeed(int _seed); |
| 20 | + static float simplex(float _x, float _y); // 2D simplex noise |
| 21 | + private: |
| 22 | + static int fastFloor(double _x); |
| 23 | + |
| 24 | + static Vec2f grad2[12]; |
| 25 | + static unsigned char p[256]; |
| 26 | + }; |
| 27 | + |
| 28 | + class RandomGenerator |
| 29 | + { |
| 30 | + public: |
| 31 | + float scalar() |
17 | 32 | {
|
18 |
| - public: |
19 |
| - // Simplex noise |
20 |
| - //static void setSimplexSeed(int _seed); |
21 |
| - static float simplex(float _x, float _y); // 2D simplex noise |
22 |
| - private: |
23 |
| - static int fastFloor(double _x); |
24 |
| - |
25 |
| - static Vec2f grad2[12]; |
26 |
| - static unsigned char p[256]; |
27 |
| - }; |
28 |
| - |
29 |
| - class RandomGenerator |
| 33 | + return distrib(engine); |
| 34 | + } |
| 35 | + |
| 36 | + math::Vec3f unit_vector() |
30 | 37 | {
|
31 |
| - public: |
32 |
| - float scalar() |
33 |
| - { |
34 |
| - return distrib(engine); |
35 |
| - } |
36 |
| - |
37 |
| - math::Vec3f unit_vector() |
38 |
| - { |
39 |
| - auto theta = math::TwoPi*scalar(); |
40 |
| - auto cosPhi = 2*scalar()-1; |
41 |
| - auto sinPhi = sqrt(1-cosPhi*cosPhi); |
42 |
| - return math::Vec3f( |
43 |
| - cos(theta)*sinPhi, |
44 |
| - sin(theta)*sinPhi, |
45 |
| - cosPhi); |
46 |
| - } |
47 |
| - private: |
48 |
| - std::default_random_engine engine; |
49 |
| - std::uniform_real_distribution<float> distrib; |
50 |
| - }; |
51 |
| - } |
52 |
| -} // namespace rev |
| 38 | + auto theta = math::TwoPi*scalar(); |
| 39 | + auto cosPhi = 2*scalar()-1; |
| 40 | + auto sinPhi = sqrt(1-cosPhi*cosPhi); |
| 41 | + return math::Vec3f( |
| 42 | + cos(theta)*sinPhi, |
| 43 | + sin(theta)*sinPhi, |
| 44 | + cosPhi); |
| 45 | + } |
| 46 | + private: |
| 47 | + std::default_random_engine engine; |
| 48 | + std::uniform_real_distribution<float> distrib; |
| 49 | + }; |
| 50 | + |
| 51 | + inline float RadicalInverse_VdC(uint32_t bits) |
| 52 | + { |
| 53 | + bits = (bits << 16u) | (bits >> 16u); |
| 54 | + bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); |
| 55 | + bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); |
| 56 | + bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); |
| 57 | + bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); |
| 58 | + return float(bits) / 0x100000000; |
| 59 | + } |
| 60 | + |
| 61 | + //------------------------------------------------------------------------------ |
| 62 | + inline Vec2f Hammersley(uint32_t i, uint32_t N) |
| 63 | + { |
| 64 | + return Vec2f(float(i) / N, RadicalInverse_VdC(i)); |
| 65 | + } |
| 66 | + |
| 67 | + inline Vec3f ImportanceSampleGGX(Vec2f Xi, float Roughness) |
| 68 | + { |
| 69 | + float a = Roughness * Roughness; |
| 70 | + float Phi = 2 * Pi * Xi.x(); |
| 71 | + float CosTheta = sqrt((1 - Xi.y()) / (1 + (a * a - 1) * Xi.y())); |
| 72 | + float SinTheta = sqrt(1 - CosTheta * CosTheta); |
| 73 | + Vec3f H; |
| 74 | + H.x() = SinTheta * cos(Phi); |
| 75 | + H.y() = SinTheta * sin(Phi); |
| 76 | + H.z() = CosTheta; |
| 77 | + |
| 78 | + return H; |
| 79 | + } |
| 80 | + |
| 81 | + inline Vec3f ImportanceSampleGGX_r1(Vec2f Xi) |
| 82 | + { |
| 83 | + float Phi = 2 * Pi * Xi.x(); |
| 84 | + float CosTheta = sqrt(1 - Xi.y()); |
| 85 | + float SinTheta = sqrt(1 - CosTheta * CosTheta); |
| 86 | + Vec3f H; |
| 87 | + H.x() = SinTheta * cos(Phi); |
| 88 | + H.y() = SinTheta * sin(Phi); |
| 89 | + H.z() = CosTheta; |
| 90 | + |
| 91 | + return H; |
| 92 | + } |
| 93 | + |
| 94 | + inline float SmithGGXCorrelatedG2(float ndv, float ndl, float alpha) |
| 95 | + { |
| 96 | + float a2 = alpha * alpha; |
| 97 | + float GV = ndv * sqrt(a2 + (1 - a2) * ndl * ndl); |
| 98 | + float GL = ndl * sqrt(a2 + (1 - a2) * ndv * ndv); |
| 99 | + return 2 * ndv * ndl / (GL + GV); |
| 100 | + } |
| 101 | + |
| 102 | + inline float SmithGGXCorrelatedG2_over_ndv(float ndv, float ndl, float alpha) |
| 103 | + { |
| 104 | + float a2 = alpha * alpha; |
| 105 | + float GV = ndv * sqrt(a2 + (1 - a2) * ndl * ndl); |
| 106 | + float GL = ndl * sqrt(a2 + (1 - a2) * ndv * ndv); |
| 107 | + return 2 * ndl / (GL + GV); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + inline float SmithGGXCorrelatedG2_a1(float ndv, float ndl) |
| 112 | + { |
| 113 | + return 2 * ndv * ndl / (ndl + ndv); |
| 114 | + } |
| 115 | + |
| 116 | + inline float SmithGGXCorrelatedG2_overNdv_ndv0(float alpha) |
| 117 | + { |
| 118 | + return 2 / alpha; |
| 119 | + } |
| 120 | + |
| 121 | +} // namespace rev::math |
0 commit comments