[3376ec9] | 1 | #pragma once |
---|
| 2 | |
---|
| 3 | #include <math.hfa> |
---|
| 4 | |
---|
| 5 | trait fromint(otype T) { |
---|
| 6 | void ?{}(T&, int); |
---|
| 7 | }; |
---|
| 8 | trait zeroinit(otype T) { |
---|
| 9 | void ?{}(T&, zero_t); |
---|
| 10 | }; |
---|
| 11 | trait zero_assign(otype T) { |
---|
| 12 | T ?=?(T&, zero_t); |
---|
| 13 | }; |
---|
| 14 | trait subtract(otype T) { |
---|
| 15 | T ?-?(T, T); |
---|
| 16 | }; |
---|
| 17 | trait negate(otype T) { |
---|
| 18 | T -?(T); |
---|
| 19 | }; |
---|
| 20 | trait add(otype T) { |
---|
| 21 | T ?+?(T, T); |
---|
| 22 | }; |
---|
| 23 | trait multiply(otype T) { |
---|
| 24 | T ?*?(T, T); |
---|
| 25 | }; |
---|
| 26 | trait divide(otype T) { |
---|
| 27 | T ?/?(T, T); |
---|
| 28 | }; |
---|
| 29 | trait lessthan(otype T) { |
---|
| 30 | int ?<?(T, T); |
---|
| 31 | }; |
---|
| 32 | trait equality(otype T) { |
---|
| 33 | int ?==?(T, T); |
---|
| 34 | }; |
---|
| 35 | trait sqrt(otype T) { |
---|
| 36 | T sqrt(T); |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | static inline { |
---|
| 40 | // int |
---|
| 41 | int ?=?(int& n, zero_t) { return n = 0.f; } |
---|
| 42 | /* float */ |
---|
| 43 | void ?{}(float& a, int b) { a = b; } |
---|
| 44 | float ?=?(float& n, zero_t) { return n = 0.f; } |
---|
| 45 | /* double */ |
---|
| 46 | void ?{}(double& a, int b) { a = b; } |
---|
| 47 | double ?=?(double& n, zero_t) { return n = 0L; } |
---|
| 48 | // long double |
---|
| 49 | void ?{}(long double& a, int b) { a = b; } |
---|
| 50 | long double ?=?(long double& n, zero_t) { return n = 0L; } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | trait dottable(otype V, otype T) { |
---|
| 54 | T dot(V, V); |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | static inline { |
---|
| 58 | |
---|
| 59 | forall(otype T | sqrt(T), otype V | dottable(V, T)) |
---|
| 60 | T length(V v) { |
---|
| 61 | return sqrt(dot(v, v)); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | forall(otype T, otype V | dottable(V, T)) |
---|
| 65 | T length_squared(V v) { |
---|
| 66 | return dot(v, v); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | forall(otype T, otype V | { T length(V); } | subtract(V)) |
---|
| 70 | T distance(V v1, V v2) { |
---|
| 71 | return length(v1 - v2); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | forall(otype T, otype V | { T length(V); V ?/?(V, T); }) |
---|
| 76 | V normalize(V v) { |
---|
| 77 | return v / length(v); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | // Project vector u onto vector v |
---|
| 81 | forall(otype T, otype V | dottable(V, T) | { V normalize(V); V ?*?(V, T); }) |
---|
| 82 | V project(V u, V v) { |
---|
| 83 | V v_norm = normalize(v); |
---|
| 84 | return v_norm * dot(u, v_norm); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | // Reflect incident vector v with respect to surface with normal n |
---|
| 88 | forall(otype T | fromint(T), otype V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); }) |
---|
| 89 | V reflect(V v, V n) { |
---|
| 90 | return v - (T){2} * project(v, n); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | // Refract incident vector v with respect to surface with normal n |
---|
| 94 | // eta is the ratio of indices of refraction between starting material and |
---|
| 95 | // entering material (i.e., from air to water, eta = 1/1.33) |
---|
| 96 | // v and n must already be normalized |
---|
| 97 | forall(otype T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T), |
---|
| 98 | otype V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); }) |
---|
| 99 | V refract(V v, V n, T eta) { |
---|
| 100 | T dotValue = dot(n, v); |
---|
| 101 | T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue); |
---|
| 102 | if (k < (T){0}) { |
---|
| 103 | return 0; |
---|
| 104 | } |
---|
| 105 | return eta * v - (eta * dotValue + sqrt(k)) * n; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | // Given a perturbed normal and a geometric normal, |
---|
| 109 | // flip the perturbed normal if the geometric normal is pointing away |
---|
| 110 | // from the observer. |
---|
| 111 | // n is the perturbed vector that we want to align |
---|
| 112 | // i is the incident vector |
---|
| 113 | // ng is the geometric normal of the surface |
---|
| 114 | /* forall(| add(T) | multiply(T) | lessthan(T) | fromint(T) | subtract(T)) */ |
---|
| 115 | forall(otype T | lessthan(T) | zeroinit(T), otype V | dottable(V, T) | negate(V)) |
---|
| 116 | V faceforward(V n, V i, V ng) { |
---|
| 117 | return dot(ng, i) < (T){0} ? n : -n; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | } // inline |
---|