-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathraven_util.odin
More file actions
421 lines (356 loc) · 12.6 KB
/
raven_util.odin
File metadata and controls
421 lines (356 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Gameplay utilities.
// WARNING: all of this will likely be moved to a separate utils package.
package raven
import "core:math"
import "core:math/linalg"
import "base:intrinsics"
import "base/ufmt"
import "core:simd/x86"
// TODO: random vector utilities etc
// TODO: 1d/2d/3d hashing
// TODO: non ugly colors? paletted? more shades?
WHITE :: Vec4{1, 1, 1, 1}
BLACK :: Vec4{0, 0, 0, 1}
TRANSPARENT :: Vec4{1, 1, 1, 0}
GRAY :: Vec4{0.5, 0.5, 0.5, 1}
DARK_GRAY :: Vec4{0.25, 0.25, 0.25, 1}
LIGHT_GRAY :: Vec4{0.75, 0.75, 0.75, 1}
RED :: Vec4{1, 0, 0, 1}
DARK_RED :: Vec4{0.5, 0, 0, 1}
LIGHT_RED :: Vec4{1, 0.5, 0.5, 1}
GREEN :: Vec4{0, 1, 0, 1}
DARK_GREEN :: Vec4{0, 0.5, 0, 1}
LIGHT_GREEN :: Vec4{0.5, 1, 0.5, 1}
BLUE :: Vec4{0, 0, 1, 1}
DARK_BLUE :: Vec4{0, 0, 0.5, 1}
LIGHT_BLUE :: Vec4{0.5, 0.5, 1, 1}
YELLOW :: Vec4{1, 1, 0, 1}
LIGHT_YELLOW :: Vec4{1, 1, 0.5, 1}
CYAN :: Vec4{0, 1, 1, 1}
DARK_CYAN :: Vec4{0, 0.5, 0.5, 1}
LIGHT_CYAN :: Vec4{0.5, 1, 1, 1}
PINK :: Vec4{1, 0, 1, 1}
DARK_PINK :: Vec4{0.5, 0, 0.5, 1}
LIGHT_PINK :: Vec4{1, 0.5, 1, 1}
ORANGE :: Vec4{1, 0.5, 0, 1}
LIGHT_ORANGE :: Vec4{1, 0.75, 0.5, 1}
PURPLE :: Vec4{0.5, 0, 1, 1}
DARK_PURPLE :: Vec4{0.25, 0, 0.5, 1}
LIGHT_PURPLE :: Vec4{0.75, 0.5, 1, 1}
quat_angle_axis :: linalg.quaternion_angle_axis_f32
eprintf :: ufmt.eprintf
eprintfln :: ufmt.eprintfln
tprintf :: ufmt.tprintf
@(require_results)
deg :: #force_inline proc "contextless" (degrees: f32) -> (radians: f32) {
return degrees * math.RAD_PER_DEG
}
@(require_results)
lerp :: proc "contextless" (a, b: $T, t: f32) -> T where !intrinsics.type_is_quaternion(T) {
return a * (1 - t) + b * t
}
// Exponential lerp. Multiply rate by delta to get frame rate independent interpolation
@(require_results)
lexp :: proc "contextless" (a, b: $T, rate: f32) -> T {
return lerp(b, a, approx_nexp(rate))
}
@(require_results)
nlerp :: proc "contextless" (a, b: $T, t: f32) -> T {
return linalg.normalize0(lerp(a, b, t))
}
@(require_results)
nlexp :: proc "contextless" (a, b: $T, rate: f32) -> T {
return nlerp(b, a, approx_nexp(rate))
}
@(require_results)
fade :: #force_inline proc "contextless" (alpha: f32) -> Vec4 {
return {1, 1, 1, alpha}
}
@(require_results)
gray :: #force_inline proc "contextless" (val: f32) -> Vec4 {
return {val, val, val, 1}
}
@(require_results)
addz :: #force_inline proc "contextless" (v: Vec2, z: f32 = 0.0) -> Vec3 {
return {v.x, v.y, z}
}
@(require_results)
nsin :: proc "contextless" (x: f32) -> f32 {
return 0.5 + 0.5 * math.sin_f32(x * math.PI * 2)
}
@(require_results)
vcast :: proc "contextless" ($T: typeid, v: [$N]$E) -> (result: [N]T)
where intrinsics.type_is_integer(E) || intrinsics.type_is_float(E)
{
for elem, i in v {
result[i] = cast(T)elem
}
return result
}
@(require_results)
int_cast :: proc($Dst: typeid, v: $Src) -> Dst where intrinsics.type_is_integer(Dst), intrinsics.type_is_integer(Src) {
assert(v == Src(Dst(v)), "Safe integer cast failed")
return cast(Dst)v
}
// Counter-clockwise. Negate to do clockwise.
@(require_results)
rot90 :: #force_inline proc "contextless" (v: [2]$T) -> [2]T {
return {-v.y, v.x}
}
// Returns value in 0..1 range.
// Same as remap(t, a, b, 0, 1)
@(require_results)
unlerp :: proc "contextless" (a, b: f32, x: f32) -> f32 {
return (x - a) / (b - a)
}
// Linearly transform x from range a0..a1 to b0..b1
@(require_results)
remap :: proc "contextless" (x, a0, a1, b0, b1: f32) -> f32 {
return ((x - a0) / (a1 - a0)) * (b1 - b0) + b0
}
@(require_results)
remap_clamped :: #force_inline proc "contextless" (x, a0, a1, b0, b1: f32) -> f32 {
return remap(clamp(x, a0, a1), a0, a1, b0, b1)
}
@(require_results)
smoothstep :: proc "contextless" (edge0, edge1, x: f32) -> f32 {
t := clamp((x - edge0) / (edge1 - edge0), 0.0, 1)
return t * t * (3.0 - 2.0 * t)
}
@(require_results)
luminance :: proc "contextless" (rgb: Vec3) -> f32 {
return linalg.dot(rgb, Vec3{0.2126, 0.7152, 0.0722})
}
floor :: proc {
floor_f32,
floor_vec,
}
@(require_results)
floor_vec :: proc (x: [$N]f32) -> [N]f32 where N <= 4 {
return transmute([N]f32)intrinsics.simd_floor(transmute(#simd[N]f32)x)
}
@(require_results)
floor_f32 :: proc (x: f32) -> f32 {
return transmute(f32)intrinsics.simd_floor(transmute(#simd[1]f32)x)
}
// RGB only!
@(require_results)
hex_color :: proc "contextless" (hex: u32) -> Vec4 {
bytes := transmute([4]u8)hex
return {
f32(bytes[2]) / 255.0,
f32(bytes[1]) / 255.0,
f32(bytes[0]) / 255.0,
1.0,
}
}
// Oklab lerp - Better color gradients than regular lerp()
@(require_results)
oklerp :: proc "contextless" (a, b: Vec4, t: f32) -> (result: Vec4) {
// https://bottosson.github.io/posts/oklab
// https://www.shadertoy.com/view/ttcyRS
CONE_TO_LMS :: Mat3{0.4121656120, 0.2118591070, 0.0883097947, 0.5362752080, 0.6807189584, 0.2818474174, 0.0514575653, 0.1074065790, 0.6302613616}
LMS_TO_CONE :: Mat3{4.0767245293, -1.2681437731, -0.0041119885, -3.3072168827, 2.6093323231, -0.7034763098, 0.2307590544, -0.3411344290, 1.7068625689}
// rgb to cone (arg of pow can't be negative)
lms_a := linalg.pow(CONE_TO_LMS * a.rgb, 1 / 3.0)
lms_b := linalg.pow(CONE_TO_LMS * b.rgb, 1 / 3.0)
lms := lerp(lms_a, lms_b, t)
// gain in the middle (no oaklab anymore, but looks better?)
// lms *= 1+0.2*h*(1-h);
// cone to rgb
result.rgb = LMS_TO_CONE * (lms * lms * lms)
result.a = lerp(a.a, b.a, t)
return result
}
// 0 -> Red, 0.5 -> Blue, 1 -> Green
@(require_results)
heatmap_color :: proc(val: f32) -> (result: Vec4) {
result.g = smoothstep(0.5, 0.8, val)
if (val > 0.5) {
result.b = smoothstep(1, 0.5, val)
} else {
result.b = smoothstep(0.0, 0.5, val)
}
result.r = smoothstep(1, 0.0, val)
result.a = 1
return result
}
// ZXY order for first-person view.
@(require_results)
euler_rot :: proc(angles: Vec3) -> Quat {
return linalg.quaternion_from_euler_angle_y_f32(angles.y) *
linalg.quaternion_from_euler_angle_x_f32(angles.x) *
linalg.quaternion_from_euler_angle_z_f32(angles.z)
}
// Spring Integration
// Source: http://allenchou.net/2015/04/game-math-precise-control-over-numeric-springing/
// damp: zeta, smoothness halflife
// freq: omega, the oscillation frequency
spring :: proc "contextless" (x, v: ^$T, x_target: T, damp: f32, freq: f32, delta: f32)
where intrinsics.type_is_float(T) || (intrinsics.type_is_array(T) && intrinsics.type_is_float(intrinsics.type_elem_type(T)))
{
x_temp := x^
v_temp := v^
f := 1.0 + 2.0 * delta * damp * freq
oo := freq * freq
hoo := delta * oo
hhoo := delta * hoo
det_inv := 1.0 / (f + hhoo)
det_x := f * x_temp + delta * v_temp + hhoo * x_target
det_v := v_temp + hoo * (x_target - x_temp)
x^ = det_x * det_inv
v^ = det_v * det_inv
}
// Utility for springs where X and V are packed in an array.
spring2 :: proc "contextless" (xv: ^[2]$T, x_target: T, damp: f32, freq: f32, delta: f32)
where intrinsics.type_is_float(T) || (intrinsics.type_is_array(T) && intrinsics.type_is_float(intrinsics.type_elem_type(T)))
{
spring(&xv[0], &xv[1], x_target = x_target, damp = damp, freq = freq, delta = delta)
}
// https://gist.github.com/jakubtomsu/d25210b55037858c3ed35fe00182f92a
@(require_results)
approx_nexp :: proc "contextless" (x: f32) -> (result: f32) {
A :: 1.1566406
C :: 0.53652346
denom := 1.0 + x * (A + C * x * x)
return rcp(denom)
}
@(require_results)
rcp :: proc "contextless" (denom: f32) -> (result: f32) {
// TODO: make this compile on WASM
// when ODIN_ARCH == .amd64 {
// return intrinsics.simd_extract(x86._mm_rcp_ss(cast(x86.__m128)denom), 0)
// } else {
return 1.0 / denom
// }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: Rect
//
@(require_results) rect_make :: proc(min: Vec2, full_size: Vec2) -> Rect {
return {
min = min,
max = min + full_size,
}
}
@(require_results) rect_make_centered :: proc(pos: Vec2, half_size: Vec2) -> Rect {
return {pos - half_size, pos + half_size}
}
@(require_results) rect_center :: proc(r: Rect) -> Vec2 {
return (r.min + r.max) * 0.5
}
@(require_results) rect_anchor :: proc(r: Rect, anchor: Vec2) -> Vec2 {
return {lerp(r.min.x, r.max.x, anchor.x), lerp(r.min.y, r.max.y, anchor.y)}
}
@(require_results) rect_full_size :: #force_inline proc(r: Rect) -> Vec2 {
return r.max - r.min
}
@(require_results) rect_expand :: proc(r: Rect, a: Vec2) -> Rect {
return {r.min - a, r.max + a}
}
@(require_results) rect_scale :: proc(r: Rect, a: Vec2) -> Rect {
size := rect_full_size(r) * 0.5
center := rect_center(r)
return {center - size * a, center + size * a}
}
@(require_results) rect_contains_point :: proc(r: Rect, p: Vec2) -> bool {
return p.x > r.min.x && p.y > r.min.y && p.x < r.max.x && p.y < r.max.y
}
@(require_results) rect_clamp_point :: proc(r: Rect, p: Vec2) -> Vec2 {
return {clamp(p.x, r.min.x, r.max.x), clamp(p.y, r.min.y, r.max.y)}
}
@(require_results) rect_cut_left :: proc(r: ^Rect, a: f32) -> Rect {
minx := r.min.x
r.min.x = min(r.max.x, r.min.x + a)
return {{minx, r.min.y}, {r.min.x, r.max.y}}
}
@(require_results) rect_cut_right :: proc(r: ^Rect, a: f32) -> Rect {
maxx := r.max.x
r.max.x = max(r.min.x, r.max.x - a)
return {{r.max.x, r.min.y}, {maxx, r.max.y}}
}
@(require_results) rect_cut_top :: proc(r: ^Rect, a: f32) -> Rect {
miny := r.min.y
r.min.y = min(r.max.y, r.min.y + a)
return {{r.min.x, miny}, {r.max.x, r.min.y}}
}
@(require_results) rect_cut_bottom :: proc(r: ^Rect, a: f32) -> Rect {
maxy := r.max.y
r.max.y = max(r.min.y, r.max.y - a)
return {{r.min.x, r.max.y}, {r.max.x, maxy}}
}
@(require_results) rect_split_left :: proc(r: ^Rect, t: f32) -> Rect {
return rect_cut_left(r, (r.max.x - r.min.x) * t)
}
@(require_results) rect_split_right :: proc(r: ^Rect, t: f32) -> Rect {
return rect_cut_right(r, (r.max.x - r.min.x) * t)
}
@(require_results) rect_split_top :: proc(r: ^Rect, t: f32) -> Rect {
return rect_cut_top(r, (r.max.y - r.min.y) * t)
}
@(require_results) rect_split_bottom :: proc(r: ^Rect, t: f32) -> Rect {
return rect_cut_bottom(r, (r.max.y - r.min.y) * t)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MARK: Camera
//
@(require_results)
make_3d_perspective_camera :: proc(pos: Vec3, rot: Quat, fov: f32 = math.PI * 0.5) -> Camera {
return {
pos = pos,
rot = rot,
projection = perspective_projection(
get_screen_size(),
fov = clamp(fov, 0.00001, math.PI * 0.99),
),
}
}
@(require_results)
make_3d_orthographic_camera :: proc(pos: Vec3, rot: Quat, fov: f32 = 10) -> Camera {
screen := get_screen_size()
aspect := screen.x / screen.y
return {
pos = pos,
rot = rot,
projection = orthographic_projection(
top = fov,
bottom = -fov,
left = -fov * aspect,
right = fov * aspect,
near = 1000.0, // Reverse Z!
far = 0.01,
),
}
}
@(require_results)
make_2d_camera :: proc(center: Vec3 = 0, fov: Vec2 = 1.0, angle: f32 = 0) -> Camera {
screen := get_screen_size()
return {
pos = center,
rot = linalg.quaternion_angle_axis_f32(angle, {0, 0, 1}),
projection = orthographic_projection(
left = -fov.x * screen.x * 0.5,
right = fov.x * screen.x * 0.5,
top = fov.y * screen.y * 0.5,
bottom = -fov.y * screen.y * 0.5,
near = 1,
far = 0,
),
}
}
@(require_results)
make_screen_camera :: proc(offset: Vec3 = 0) -> Camera {
screen := get_screen_size()
return {
pos = offset + {0, 0, -1},
rot = 1,
projection = orthographic_projection(
left = 0,
right = screen.x,
top = 0,
bottom = screen.y,
near = 2,
far = 0,
),
}
}