| 1 | #pragma once
|
|---|
| 2 | #include <math.hfa>
|
|---|
| 3 | #include <iostream.hfa>
|
|---|
| 4 |
|
|---|
| 5 | //---------------------- Vector Types ----------------------
|
|---|
| 6 | // TODO: make generic, as per glm
|
|---|
| 7 |
|
|---|
| 8 | struct vec2 {
|
|---|
| 9 | float x, y;
|
|---|
| 10 | };
|
|---|
| 11 |
|
|---|
| 12 | void ?{}( vec2 & v, float x, float y) {
|
|---|
| 13 | v.[x, y] = [x, y];
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | forall( dtype ostype | ostream( ostype ) ) {
|
|---|
| 17 | ostype & ?|?( ostype & os, const vec2& v) with (v) {
|
|---|
| 18 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 19 | fmt( os, "<%g,%g>", x, y);
|
|---|
| 20 | return os;
|
|---|
| 21 | }
|
|---|
| 22 | void ?|?( ostype & os, const vec2& v ) {
|
|---|
| 23 | (ostype &)(os | v); ends( os );
|
|---|
| 24 | }
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | void ?{}(vec2& vec, zero_t) with (vec) {
|
|---|
| 28 | x = y = 0;
|
|---|
| 29 | }
|
|---|
| 30 | void ?{}(vec2& vec, vec2& other) with (vec) {
|
|---|
| 31 | [x,y] = other.[x,y];
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | vec2 ?-?(const vec2& u, const vec2& v) {
|
|---|
| 35 | return [u.x - v.x, u.y - v.y];
|
|---|
| 36 | }
|
|---|
| 37 | vec2 ?*?(const vec2& v, float scalar) with (v) {
|
|---|
| 38 | return [x * scalar, y * scalar];
|
|---|
| 39 | }
|
|---|
| 40 | vec2 ?*?(float scalar, const vec2& v) {
|
|---|
| 41 | return v * scalar;
|
|---|
| 42 | }
|
|---|
| 43 | vec2 ?/?(const vec2& v, float scalar) with (v) {
|
|---|
| 44 | return [x / scalar, y / scalar];
|
|---|
| 45 | }
|
|---|
| 46 | vec2 -?(const vec2& v) with (v) {
|
|---|
| 47 | return [-x, -y];
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /* //---------------------- Geometric Functions ---------------------- */
|
|---|
| 51 | /* // These functions implement the Geometric Functions section of GLSL */
|
|---|
| 52 |
|
|---|
| 53 | static inline float dot(const vec2& u, const vec2& v) {
|
|---|
| 54 | return u.x * v.x + u.y * v.y;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | static inline float length(const vec2& v) {
|
|---|
| 58 | return sqrt(dot(v, v));
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | // Returns the distance betwwen v1 and v2, i.e., length(p0 - p1).
|
|---|
| 62 | static inline float distance(const vec2& v1, const vec2& v2) {
|
|---|
| 63 | return length(v1 - v2);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | static inline vec2 normalize(const vec2& v) {
|
|---|
| 67 | // TODO(dkobets) -- show them inversesqrt
|
|---|
| 68 | // https://github.com/g-truc/glm/blob/269ae641283426f7f84116f2fe333472b9c914c9/glm/detail/func_exponential.inl
|
|---|
| 69 | /* return v * inversesqrt(dot(v, v)); */
|
|---|
| 70 | return v / sqrt(dot(v, v));
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | // project vector u onto vector v
|
|---|
| 74 | static inline vec2 project(const vec2& u, const vec2& v) {
|
|---|
| 75 | vec2 v_norm = normalize(v);
|
|---|
| 76 | return v_norm * dot(u, v_norm);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /* returns the reflection direction : v - 2.0 * project(v, n)
|
|---|
| 80 | * for incident vector v and surface normal n
|
|---|
| 81 | */
|
|---|
| 82 | static inline vec2 reflect(const vec2& v, const vec2& n) {
|
|---|
| 83 | return v - 2 * project(v, n);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | // incident vector v, surface normal n
|
|---|
| 87 | // eta = ratio of indices of refraction between starting material and
|
|---|
| 88 | // entering material (i.e., from air to water, eta = 1/1.33)
|
|---|
| 89 | static inline vec2 refract(const vec2& v, const vec2& n, float eta) {
|
|---|
| 90 | float dotValue = dot(n, v);
|
|---|
| 91 | float k = 1 - eta * eta * (1 - dotValue * dotValue);
|
|---|
| 92 | if (k < 0) {
|
|---|
| 93 | return 0;
|
|---|
| 94 | }
|
|---|
| 95 | return eta * v - (eta * dotValue + sqrt(k)) * n;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | // TODO: I don't quite understand the use-case for faceforward
|
|---|