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