| 1 | #pragma once
|
|---|
| 2 | #include <math.hfa>
|
|---|
| 3 | #include <iostream.hfa>
|
|---|
| 4 |
|
|---|
| 5 | struct vec2 {
|
|---|
| 6 | float x, y;
|
|---|
| 7 | };
|
|---|
| 8 |
|
|---|
| 9 | // Constructors
|
|---|
| 10 |
|
|---|
| 11 | void ?{}( vec2 & v, float x, float y) {
|
|---|
| 12 | v.[x, y] = [x, y];
|
|---|
| 13 | }
|
|---|
| 14 | void ?{}(vec2& vec, zero_t) with (vec) {
|
|---|
| 15 | x = y = 0;
|
|---|
| 16 | }
|
|---|
| 17 | void ?{}(vec2& vec, float val) with (vec) {
|
|---|
| 18 | x = y = val;
|
|---|
| 19 | }
|
|---|
| 20 | void ?{}(vec2& vec, vec2& other) with (vec) {
|
|---|
| 21 | [x,y] = other.[x,y];
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | // Assignment
|
|---|
| 25 | void ?=?(vec2& vec, const vec2& other) with (vec) {
|
|---|
| 26 | [x,y] = other.[x,y];
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | // Primitive mathematical operations
|
|---|
| 30 |
|
|---|
| 31 | // Subtraction
|
|---|
| 32 | vec2 ?-?(const vec2& u, const vec2& v) {
|
|---|
| 33 | return [u.x - v.x, u.y - v.y];
|
|---|
| 34 | }
|
|---|
| 35 | vec2& ?-=?(vec2& u, const vec2& v) {
|
|---|
| 36 | u = u - v;
|
|---|
| 37 | return u;
|
|---|
| 38 | }
|
|---|
| 39 | vec2 -?(const vec2& v) with (v) {
|
|---|
| 40 | return [-x, -y];
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | // Addition
|
|---|
| 44 | vec2 ?+?(const vec2& u, const vec2& v) {
|
|---|
| 45 | return [u.x + v.x, u.y + v.y];
|
|---|
| 46 | }
|
|---|
| 47 | vec2& ?+=?(vec2& u, const vec2& v) {
|
|---|
| 48 | u = u + v;
|
|---|
| 49 | return u;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | // Scalar Multiplication
|
|---|
| 53 | vec2 ?*?(const vec2& v, float scalar) with (v) {
|
|---|
| 54 | return [x * scalar, y * scalar];
|
|---|
| 55 | }
|
|---|
| 56 | vec2& ?*=?(vec2& v, float scalar) with (v) {
|
|---|
| 57 | v = v * scalar;
|
|---|
| 58 | return v;
|
|---|
| 59 | }
|
|---|
| 60 | vec2 ?*?(float scalar, const vec2& v) {
|
|---|
| 61 | return v * scalar;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | // Scalar Division
|
|---|
| 66 | vec2 ?/?(const vec2& v, float scalar) with (v) {
|
|---|
| 67 | return [x / scalar, y / scalar];
|
|---|
| 68 | }
|
|---|
| 69 | vec2& ?/=?(vec2& v, float scalar) with (v) {
|
|---|
| 70 | v = v / scalar;
|
|---|
| 71 | return v;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | // Relational Operators
|
|---|
| 75 | bool ?==?(const vec2& u, const vec2& v) with (u) {
|
|---|
| 76 | return x == v.x && y == v.y;
|
|---|
| 77 | }
|
|---|
| 78 | bool ?!=?(const vec2& u, const vec2& v) {
|
|---|
| 79 | return !(u == v);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | // Printing the vector (ostream)
|
|---|
| 83 | forall( dtype ostype | ostream( ostype ) ) {
|
|---|
| 84 | ostype & ?|?( ostype & os, const vec2& v) with (v) {
|
|---|
| 85 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
|
|---|
| 86 | fmt( os, "<%g,%g>", x, y);
|
|---|
| 87 | return os;
|
|---|
| 88 | }
|
|---|
| 89 | void ?|?( ostype & os, const vec2& v ) {
|
|---|
| 90 | (ostype &)(os | v); ends( os );
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /* //---------------------- Geometric Functions ---------------------- */
|
|---|
| 95 | /* // These functions implement the Geometric Functions section of GLSL for 2D vectors*/
|
|---|
| 96 |
|
|---|
| 97 | static inline float dot(const vec2& u, const vec2& v) {
|
|---|
| 98 | return u.x * v.x + u.y * v.y;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | static inline float length(const vec2& v) {
|
|---|
| 102 | return sqrt(dot(v, v));
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | static inline float distance(const vec2& v1, const vec2& v2) {
|
|---|
| 106 | return length(v1 - v2);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | static inline vec2 normalize(const vec2& v) {
|
|---|
| 110 | return v / sqrt(dot(v, v));
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | // Project vector u onto vector v
|
|---|
| 114 | static inline vec2 project(const vec2& u, const vec2& v) {
|
|---|
| 115 | vec2 v_norm = normalize(v);
|
|---|
| 116 | return v_norm * dot(u, v_norm);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | // Reflect incident vector v with respect to surface with normal n
|
|---|
| 120 | static inline vec2 reflect(const vec2& v, const vec2& n) {
|
|---|
| 121 | return v - 2 * project(v, n);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | // Refract incident vector v with respect to surface with normal n
|
|---|
| 125 | // eta is the ratio of indices of refraction between starting material and
|
|---|
| 126 | // entering material (i.e., from air to water, eta = 1/1.33)
|
|---|
| 127 | static inline vec2 refract(const vec2& v, const vec2& n, float eta) {
|
|---|
| 128 | float dotValue = dot(n, v);
|
|---|
| 129 | float k = 1 - eta * eta * (1 - dotValue * dotValue);
|
|---|
| 130 | if (k < 0) {
|
|---|
| 131 | return 0;
|
|---|
| 132 | }
|
|---|
| 133 | return eta * v - (eta * dotValue + sqrt(k)) * n;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | // Used to render perturbed surfaces by ensuring that a perturbed normal
|
|---|
| 137 | // is pointing in the same direction as the geometric normal of the
|
|---|
| 138 | // surface.
|
|---|
| 139 | // n is the perturbed vector that we want to align
|
|---|
| 140 | // i is the incident vector
|
|---|
| 141 | // ng is the geometric normal of the surface
|
|---|
| 142 | static inline vec2 faceforward(const vec2& n, const vec2& i, float ng) {
|
|---|
| 143 | return dot(n, i) < 0 ? ng : -ng;
|
|---|
| 144 | }
|
|---|