master
ブランチ master
ShaderPlayground / include / util / vector.hh
4.1 KB、292行 生 Blame 履歴
1/*
2# 076 Free License
3
4Copyright (c) テクニカル諏訪子
5
6Permission is hereby granted to any person obtaining a copy of the software
7C++ Utils (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 2. **Notice Preservation**:
14 - This license and the above copyright notice must remain intact in all copies
15 of the source code.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24#ifndef VECTOR_HH
25#define VECTOR_HH
26
27#include <util/types.hh>
28#include <cmath>
29
30union Vector2 {
31 struct Position {
32 f32 x;
33 f32 y;
34 };
35 struct UV {
36 f32 u;
37 f32 v;
38 };
39
40 Position pos;
41 UV uv;
42};
43
44union Vector2f {
45 struct Position {
46 f32 x;
47 f32 y;
48 };
49 struct UV {
50 f32 u;
51 f32 v;
52 };
53
54 Position pos;
55 UV uv;
56};
57
58union Vector2d {
59 struct Position {
60 f64 x;
61 f64 y;
62 };
63 struct UV {
64 f64 u;
65 f64 v;
66 };
67
68 Position pos;
69 UV uv;
70};
71
72union Vector2i {
73 struct Position {
74 i32 x;
75 i32 y;
76 };
77 struct UV {
78 i32 u;
79 i32 v;
80 };
81
82 Position pos;
83 UV uv;
84};
85
86union Vector2u {
87 struct Position {
88 u32 x;
89 u32 y;
90 };
91 struct UV {
92 u32 u;
93 u32 v;
94 };
95
96 Position pos;
97 UV uv;
98};
99
100union Vector3 {
101 struct Position {
102 f32 x;
103 f32 y;
104 f32 z;
105 };
106 struct RGB {
107 f32 r;
108 f32 g;
109 f32 b;
110 };
111
112 Position pos;
113 RGB rgb;
114};
115
116union Vector3f {
117 struct Position {
118 f32 x;
119 f32 y;
120 f32 z;
121 };
122 struct RGB {
123 f32 r;
124 f32 g;
125 f32 b;
126 };
127
128 Position pos;
129 RGB rgb;
130};
131
132union Vector3d {
133 struct Position {
134 f64 x;
135 f64 y;
136 f64 z;
137 };
138 struct RGB {
139 f64 r;
140 f64 g;
141 f64 b;
142 };
143
144 Position pos;
145 RGB rgb;
146};
147
148union Vector3i {
149 struct Position {
150 i32 x;
151 i32 y;
152 i32 z;
153 };
154 struct RGB {
155 i32 r;
156 i32 g;
157 i32 b;
158 };
159
160 Position pos;
161 RGB rgb;
162};
163
164union Vector3u {
165 struct Position {
166 u32 x;
167 u32 y;
168 u32 z;
169 };
170 struct RGB {
171 u32 r;
172 u32 g;
173 u32 b;
174 };
175
176 Position pos;
177 RGB rgb;
178};
179
180union Vector4 {
181 struct Position {
182 f32 x;
183 f32 y;
184 f32 z;
185 f32 w;
186 };
187 struct RGBA {
188 f32 r;
189 f32 g;
190 f32 b;
191 f32 a;
192 };
193
194 Position pos;
195 RGBA rgba;
196};
197
198union Vector4f {
199 struct Position {
200 f32 x;
201 f32 y;
202 f32 z;
203 f32 w;
204 };
205 struct RGBA {
206 f32 r;
207 f32 g;
208 f32 b;
209 f32 a;
210 };
211
212 Position pos;
213 RGBA rgba;
214};
215
216union Vector4d {
217 struct Position {
218 f64 x;
219 f64 y;
220 f64 z;
221 f64 w;
222 };
223 struct RGBA {
224 f64 r;
225 f64 g;
226 f64 b;
227 f64 a;
228 };
229
230 Position pos;
231 RGBA rgba;
232};
233
234union Vector4i {
235 struct Position {
236 i32 x;
237 i32 y;
238 i32 z;
239 i32 w;
240 };
241 struct RGBA {
242 i32 r;
243 i32 g;
244 i32 b;
245 i32 a;
246 };
247
248 Position pos;
249 RGBA rgba;
250};
251
252union Vector4u {
253 struct Position {
254 u32 x;
255 u32 y;
256 u32 z;
257 u32 w;
258 };
259 struct RGBA {
260 u32 r;
261 u32 g;
262 u32 b;
263 u32 a;
264 };
265
266 Position pos;
267 RGBA rgba;
268};
269
270inline Vector3 vec3Sub(Vector3 a, Vector3 b) {
271 return { a.pos.x - b.pos.x, a.pos.y - b.pos.y, a.pos.z - b.pos.z };
272}
273
274inline f32 vec3Dot(Vector3 a, Vector3 b) {
275 return a.pos.x * b.pos.x + a.pos.y * b.pos.y + a.pos.z * b.pos.z;
276}
277
278inline Vector3 vec3Cross(Vector3 a, Vector3 b) {
279 return {
280 a.pos.y * b.pos.z - a.pos.z * b.pos.y,
281 a.pos.z * b.pos.x - a.pos.x * b.pos.z,
282 a.pos.x * b.pos.y - a.pos.y * b.pos.x,
283 };
284}
285
286inline Vector3 vec3Normalize(Vector3 v) {
287 f32 len = sqrtf(v.pos.x * v.pos.x + v.pos.y * v.pos.y + v.pos.z * v.pos.z);
288 if (len == 0.f) return { 0, 0, 0 };
289 return { v.pos.x / len, v.pos.y / len, v.pos.z / len };
290}
291
292#endif // VECTOR_HH