[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; }
|
---|
[ae3db00] | 42 | // unsigned int
|
---|
| 43 | int ?=?(unsigned int& n, zero_t) { return n = 0.f; }
|
---|
[3376ec9] | 44 | /* float */
|
---|
| 45 | void ?{}(float& a, int b) { a = b; }
|
---|
| 46 | float ?=?(float& n, zero_t) { return n = 0.f; }
|
---|
| 47 | /* double */
|
---|
| 48 | void ?{}(double& a, int b) { a = b; }
|
---|
| 49 | double ?=?(double& n, zero_t) { return n = 0L; }
|
---|
| 50 | // long double
|
---|
| 51 | void ?{}(long double& a, int b) { a = b; }
|
---|
| 52 | long double ?=?(long double& n, zero_t) { return n = 0L; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | trait dottable(otype V, otype T) {
|
---|
| 56 | T dot(V, V);
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | static inline {
|
---|
| 60 |
|
---|
| 61 | forall(otype T | sqrt(T), otype V | dottable(V, T))
|
---|
| 62 | T length(V v) {
|
---|
| 63 | return sqrt(dot(v, v));
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | forall(otype T, otype V | dottable(V, T))
|
---|
| 67 | T length_squared(V v) {
|
---|
| 68 | return dot(v, v);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | forall(otype T, otype V | { T length(V); } | subtract(V))
|
---|
| 72 | T distance(V v1, V v2) {
|
---|
| 73 | return length(v1 - v2);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | forall(otype T, otype V | { T length(V); V ?/?(V, T); })
|
---|
| 77 | V normalize(V v) {
|
---|
| 78 | return v / length(v);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | // Project vector u onto vector v
|
---|
| 82 | forall(otype T, otype V | dottable(V, T) | { V normalize(V); V ?*?(V, T); })
|
---|
| 83 | V project(V u, V v) {
|
---|
| 84 | V v_norm = normalize(v);
|
---|
| 85 | return v_norm * dot(u, v_norm);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // Reflect incident vector v with respect to surface with normal n
|
---|
| 89 | forall(otype T | fromint(T), otype V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); })
|
---|
| 90 | V reflect(V v, V n) {
|
---|
| 91 | return v - (T){2} * project(v, n);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | // Refract incident vector v with respect to surface with normal n
|
---|
| 95 | // eta is the ratio of indices of refraction between starting material and
|
---|
| 96 | // entering material (i.e., from air to water, eta = 1/1.33)
|
---|
| 97 | // v and n must already be normalized
|
---|
| 98 | forall(otype T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T),
|
---|
| 99 | otype V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); })
|
---|
| 100 | V refract(V v, V n, T eta) {
|
---|
| 101 | T dotValue = dot(n, v);
|
---|
| 102 | T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue);
|
---|
| 103 | if (k < (T){0}) {
|
---|
| 104 | return 0;
|
---|
| 105 | }
|
---|
| 106 | return eta * v - (eta * dotValue + sqrt(k)) * n;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | // Given a perturbed normal and a geometric normal,
|
---|
| 110 | // flip the perturbed normal if the geometric normal is pointing away
|
---|
| 111 | // from the observer.
|
---|
| 112 | // n is the perturbed vector that we want to align
|
---|
| 113 | // i is the incident vector
|
---|
| 114 | // ng is the geometric normal of the surface
|
---|
| 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
|
---|