2016-02-07 21:15:58 +00:00
|
|
|
/*
|
2019-02-24 22:54:57 +00:00
|
|
|
* 4coder base types
|
2016-02-07 21:15:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2016-05-27 02:37:45 +00:00
|
|
|
#define C_MATH 1
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32
|
|
|
|
ceil32(f32 v){
|
|
|
|
return(((v)>0)?( (v == (i32)(v))?((i32)(v)):((i32)((v)+1.f)) ):( ((i32)(v)) ));
|
|
|
|
}
|
|
|
|
|
|
|
|
static i32
|
|
|
|
floor32(f32 v){
|
|
|
|
return(((v)<0)?( (v == (i32)(v))?((i32)(v)):((i32)((v)-1.f)) ):( ((i32)(v)) ));
|
|
|
|
}
|
|
|
|
|
|
|
|
static i32
|
|
|
|
round32(f32 v){
|
|
|
|
return(floor32(v + 0.5f));
|
|
|
|
}
|
|
|
|
|
|
|
|
static i32
|
|
|
|
trun32(f32 v){
|
|
|
|
return((i32)(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
static i32
|
|
|
|
div_ceil(i32 n, i32 d){
|
|
|
|
return( ((n) % (d) != 0) + ((n) / (d)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static i32
|
|
|
|
l_round_up_i32(i32 x, i32 b){
|
|
|
|
i32 t = x + b - 1;
|
|
|
|
return(t - (t%b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32
|
|
|
|
l_round_up_u32(u32 x, u32 b){
|
|
|
|
i32 t = x + b - 1;
|
|
|
|
return(t - (t%b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 round_up_pot_u32(u32 x){
|
|
|
|
--x;
|
|
|
|
x |= x >> 1;
|
|
|
|
x |= x >> 2;
|
|
|
|
x |= x >> 4;
|
|
|
|
x |= x >> 8;
|
|
|
|
x |= x >> 16;
|
|
|
|
++x;
|
|
|
|
return(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
// scalars
|
2016-02-07 21:15:58 +00:00
|
|
|
|
2016-08-31 01:44:02 +00:00
|
|
|
#define DEG_TO_RAD (0.0174533f)
|
2016-02-07 21:15:58 +00:00
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
ABS(f32 x){
|
|
|
|
if (x < 0) x = -x;
|
2016-08-31 01:44:02 +00:00
|
|
|
return(x);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 01:44:02 +00:00
|
|
|
#if C_MATH
|
|
|
|
#include <math.h>
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
MOD(f32 x, i32 m){
|
2017-03-17 17:45:41 +00:00
|
|
|
f32 whole;
|
|
|
|
f32 frac = modff(x, &whole);
|
|
|
|
f32 r = ((i32)(whole) % m) + frac;
|
2016-08-31 01:44:02 +00:00
|
|
|
return(r);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
SQRT(f32 x){
|
2017-03-17 17:45:41 +00:00
|
|
|
f32 r = sqrtf(x);
|
2016-08-31 01:44:02 +00:00
|
|
|
return(r);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
SIN(f32 x_degrees){
|
2016-08-31 01:44:02 +00:00
|
|
|
f32 r = sinf(x_degrees * DEG_TO_RAD);
|
|
|
|
return(r);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
COS(f32 x_degrees){
|
2016-08-31 01:44:02 +00:00
|
|
|
f32 r = cosf(x_degrees * DEG_TO_RAD);
|
|
|
|
return(r);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
2016-05-25 02:12:49 +00:00
|
|
|
#endif
|
2016-02-07 21:15:58 +00:00
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
// vectors
|
2016-02-07 21:15:58 +00:00
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2017-01-23 06:19:43 +00:00
|
|
|
V2(f32 x, f32 y){
|
2019-02-05 09:13:38 +00:00
|
|
|
Vec2 result = {};
|
2016-02-07 21:15:58 +00:00
|
|
|
result.x = x;
|
|
|
|
result.y = y;
|
2019-02-05 09:13:38 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2017-01-23 06:19:43 +00:00
|
|
|
V3(f32 x, f32 y, f32 z){
|
2019-02-05 09:13:38 +00:00
|
|
|
Vec3 result = {};
|
2016-02-07 21:15:58 +00:00
|
|
|
result.x = x;
|
|
|
|
result.y = y;
|
|
|
|
result.z = z;
|
2019-02-05 09:13:38 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2017-01-23 06:19:43 +00:00
|
|
|
V4(f32 x, f32 y, f32 z, f32 w){
|
2019-02-05 09:13:38 +00:00
|
|
|
Vec4 result = {};
|
2016-02-07 21:15:58 +00:00
|
|
|
result.x = x;
|
|
|
|
result.y = y;
|
|
|
|
result.z = z;
|
|
|
|
result.w = w;
|
2019-02-05 09:13:38 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
|
|
|
V2(Vec2_i32 pi){
|
|
|
|
return(V2((f32)pi.x, (f32)pi.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3
|
|
|
|
V3(Vec3_i32 pi){
|
|
|
|
return(V3((f32)pi.x, (f32)pi.y, (f32)pi.z));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4
|
|
|
|
V4(Vec4_i32 pi){
|
|
|
|
return(V4((f32)pi.x, (f32)pi.y, (f32)pi.z, (f32)pi.w));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2
|
|
|
|
V2(Vec2 pi){
|
|
|
|
return(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3
|
|
|
|
V3(Vec3 pi){
|
|
|
|
return(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4
|
|
|
|
V4(Vec4 pi){
|
|
|
|
return(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2_i32
|
2019-02-10 07:48:07 +00:00
|
|
|
V2i32(i32 x, i32 y){
|
2019-02-05 09:13:38 +00:00
|
|
|
Vec2_i32 result = {};
|
|
|
|
result.x = x;
|
|
|
|
result.y = y;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3_i32
|
2019-02-10 07:48:07 +00:00
|
|
|
V3i32(i32 x, i32 y, i32 z){
|
2019-02-05 09:13:38 +00:00
|
|
|
Vec3_i32 result = {};
|
|
|
|
result.x = x;
|
|
|
|
result.y = y;
|
|
|
|
result.z = z;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4_i32
|
2019-02-10 07:48:07 +00:00
|
|
|
V4i32(i32 x, i32 y, i32 z, i32 w){
|
2019-02-05 09:13:38 +00:00
|
|
|
Vec4_i32 result = {};
|
|
|
|
result.x = x;
|
|
|
|
result.y = y;
|
|
|
|
result.z = z;
|
|
|
|
result.w = w;
|
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2_i32
|
|
|
|
V2i32(Vec2_f32 pi){
|
|
|
|
return(V2i32((i32)pi.x, (i32)pi.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32
|
|
|
|
V3i32(Vec3_f32 pi){
|
|
|
|
return(V3i32((i32)pi.x, (i32)pi.y, (i32)pi.z));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32
|
|
|
|
V4i32(Vec4_f32 pi){
|
|
|
|
return(V4i32((i32)pi.x, (i32)pi.y, (i32)pi.z, (i32)pi.w));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2_i32
|
|
|
|
V2i32(Vec2_i32 pi){
|
|
|
|
return(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32
|
|
|
|
V3i32(Vec3_i32 pi){
|
|
|
|
return(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32
|
|
|
|
V4i32(Vec4_i32 pi){
|
|
|
|
return(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2
|
2016-02-07 21:15:58 +00:00
|
|
|
operator+(Vec2 a, Vec2 b){
|
|
|
|
Vec2 result;
|
|
|
|
result.x = a.x + b.x;
|
|
|
|
result.y = a.y + b.y;
|
2019-02-05 09:13:38 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2016-02-07 21:15:58 +00:00
|
|
|
operator+(Vec3 a, Vec3 b){
|
|
|
|
Vec3 result;
|
|
|
|
result.x = a.x + b.x;
|
|
|
|
result.y = a.y + b.y;
|
|
|
|
result.z = a.z + b.z;
|
2019-02-05 09:13:38 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2016-02-07 21:15:58 +00:00
|
|
|
operator+(Vec4 a, Vec4 b){
|
|
|
|
Vec4 result;
|
|
|
|
result.x = a.x + b.x;
|
|
|
|
result.y = a.y + b.y;
|
|
|
|
result.z = a.z + b.z;
|
|
|
|
result.w = a.w + b.w;
|
2019-02-05 09:13:38 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2016-02-07 21:15:58 +00:00
|
|
|
operator-(Vec2 a, Vec2 b){
|
|
|
|
Vec2 result;
|
|
|
|
result.x = a.x - b.x;
|
|
|
|
result.y = a.y - b.y;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2016-02-07 21:15:58 +00:00
|
|
|
operator-(Vec3 a, Vec3 b){
|
|
|
|
Vec3 result;
|
|
|
|
result.x = a.x - b.x;
|
|
|
|
result.y = a.y - b.y;
|
|
|
|
result.z = a.z - b.z;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2016-02-07 21:15:58 +00:00
|
|
|
operator-(Vec4 a, Vec4 b){
|
|
|
|
Vec4 result;
|
|
|
|
result.x = a.x - b.x;
|
|
|
|
result.y = a.y - b.y;
|
|
|
|
result.z = a.z - b.z;
|
|
|
|
result.w = a.w - b.w;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*(Vec2 a, f32 k){
|
2016-02-07 21:15:58 +00:00
|
|
|
Vec2 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*(Vec3 a, f32 k){
|
2016-02-07 21:15:58 +00:00
|
|
|
Vec3 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*(Vec4 a, f32 k){
|
2016-02-07 21:15:58 +00:00
|
|
|
Vec4 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
|
|
|
result.w = a.w * k;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*(f32 k, Vec2 a){
|
2016-02-07 21:15:58 +00:00
|
|
|
Vec2 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*(f32 k, Vec3 a){
|
2016-02-07 21:15:58 +00:00
|
|
|
Vec3 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
2018-09-17 18:47:06 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*(f32 k, Vec4 a){
|
2016-02-07 21:15:58 +00:00
|
|
|
Vec4 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
|
|
|
result.w = a.w * k;
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
|
|
|
}
|
2016-02-07 21:15:58 +00:00
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2&
|
2016-02-07 21:15:58 +00:00
|
|
|
operator+=(Vec2 &a, Vec2 b){
|
|
|
|
a = (a + b);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3&
|
2016-02-07 21:15:58 +00:00
|
|
|
operator+=(Vec3 &a, Vec3 b){
|
|
|
|
a = (a + b);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4&
|
2016-02-07 21:15:58 +00:00
|
|
|
operator+=(Vec4 &a, Vec4 b){
|
|
|
|
a = (a + b);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2&
|
2016-02-07 21:15:58 +00:00
|
|
|
operator-=(Vec2 &a, Vec2 b){
|
|
|
|
a = (a - b);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3&
|
2016-02-07 21:15:58 +00:00
|
|
|
operator-=(Vec3 &a, Vec3 b){
|
|
|
|
a = (a - b);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4&
|
2016-02-07 21:15:58 +00:00
|
|
|
operator-=(Vec4 &a, Vec4 b){
|
|
|
|
a = (a - b);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2&
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*=(Vec2 &a, f32 k){
|
2018-09-17 18:47:06 +00:00
|
|
|
a = (a*k);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3&
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*=(Vec3 &a, f32 k){
|
2018-09-17 18:47:06 +00:00
|
|
|
a = (a*k);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4&
|
2017-01-23 06:19:43 +00:00
|
|
|
operator*=(Vec4 &a, f32 k){
|
2018-09-17 18:47:06 +00:00
|
|
|
a = (a*k);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator==(Vec2 a, Vec2 b){
|
|
|
|
return(a.x == b.x && a.y == b.y);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator!=(Vec2 a, Vec2 b){
|
|
|
|
return(!(a.x == b.x && a.y == b.y));
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator==(Vec3 a, Vec3 b){
|
|
|
|
return(a.x == b.x && a.y == b.y && a.z == b.z);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator!=(Vec3 a, Vec3 b){
|
|
|
|
return(!(a.x == b.x && a.y == b.y && a.z == b.z));
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator==(Vec4 a, Vec4 b){
|
|
|
|
return(a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator!=(Vec4 a, Vec4 b){
|
|
|
|
return(!(a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w));
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
static Vec2_i32
|
|
|
|
operator+(Vec2_i32 a, Vec2_i32 b){
|
|
|
|
Vec2_i32 result;
|
|
|
|
result.x = a.x + b.x;
|
|
|
|
result.y = a.y + b.y;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32
|
|
|
|
operator+(Vec3_i32 a, Vec3_i32 b){
|
|
|
|
Vec3_i32 result;
|
|
|
|
result.x = a.x + b.x;
|
|
|
|
result.y = a.y + b.y;
|
|
|
|
result.z = a.z + b.z;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32
|
|
|
|
operator+(Vec4_i32 a, Vec4_i32 b){
|
|
|
|
Vec4_i32 result;
|
|
|
|
result.x = a.x + b.x;
|
|
|
|
result.y = a.y + b.y;
|
|
|
|
result.z = a.z + b.z;
|
|
|
|
result.w = a.w + b.w;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2_i32
|
|
|
|
operator-(Vec2_i32 a, Vec2_i32 b){
|
|
|
|
Vec2_i32 result;
|
|
|
|
result.x = a.x - b.x;
|
|
|
|
result.y = a.y - b.y;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32
|
|
|
|
operator-(Vec3_i32 a, Vec3_i32 b){
|
|
|
|
Vec3_i32 result;
|
|
|
|
result.x = a.x - b.x;
|
|
|
|
result.y = a.y - b.y;
|
|
|
|
result.z = a.z - b.z;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32
|
|
|
|
operator-(Vec4_i32 a, Vec4_i32 b){
|
|
|
|
Vec4_i32 result;
|
|
|
|
result.x = a.x - b.x;
|
|
|
|
result.y = a.y - b.y;
|
|
|
|
result.z = a.z - b.z;
|
|
|
|
result.w = a.w - b.w;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2_i32
|
|
|
|
operator*(Vec2_i32 a, i32 k){
|
|
|
|
Vec2_i32 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32
|
|
|
|
operator*(Vec3_i32 a, i32 k){
|
|
|
|
Vec3_i32 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32
|
|
|
|
operator*(Vec4_i32 a, i32 k){
|
|
|
|
Vec4_i32 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
|
|
|
result.w = a.w * k;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2_i32
|
|
|
|
operator*(i32 k, Vec2_i32 a){
|
|
|
|
Vec2_i32 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32
|
|
|
|
operator*(i32 k, Vec3_i32 a){
|
|
|
|
Vec3_i32 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32
|
|
|
|
operator*(i32 k, Vec4_i32 a){
|
|
|
|
Vec4_i32 result;
|
|
|
|
result.x = a.x * k;
|
|
|
|
result.y = a.y * k;
|
|
|
|
result.z = a.z * k;
|
|
|
|
result.w = a.w * k;
|
|
|
|
return(result);}
|
|
|
|
|
|
|
|
static Vec2_i32&
|
|
|
|
operator+=(Vec2_i32 &a, Vec2_i32 b){
|
|
|
|
a = (a + b);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32&
|
|
|
|
operator+=(Vec3_i32 &a, Vec3_i32 b){
|
|
|
|
a = (a + b);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32&
|
|
|
|
operator+=(Vec4_i32 &a, Vec4_i32 b){
|
|
|
|
a = (a + b);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2_i32&
|
|
|
|
operator-=(Vec2_i32 &a, Vec2_i32 b){
|
|
|
|
a = (a - b);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32&
|
|
|
|
operator-=(Vec3_i32 &a, Vec3_i32 b){
|
|
|
|
a = (a - b);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32&
|
|
|
|
operator-=(Vec4_i32 &a, Vec4_i32 b){
|
|
|
|
a = (a - b);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec2_i32&
|
|
|
|
operator*=(Vec2_i32 &a, i32 k){
|
|
|
|
a = (a*k);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec3_i32&
|
|
|
|
operator*=(Vec3_i32 &a, i32 k){
|
|
|
|
a = (a*k);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vec4_i32&
|
|
|
|
operator*=(Vec4_i32 &a, i32 k){
|
|
|
|
a = (a*k);
|
|
|
|
return(a);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator==(Vec2_i32 a, Vec2_i32 b){
|
|
|
|
return(a.x == b.x && a.y == b.y);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator!=(Vec2_i32 a, Vec2_i32 b){
|
|
|
|
return(!(a.x == b.x && a.y == b.y));
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator==(Vec3_i32 a, Vec3_i32 b){
|
|
|
|
return(a.x == b.x && a.y == b.y && a.z == b.z);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator!=(Vec3_i32 a, Vec3_i32 b){
|
|
|
|
return(!(a.x == b.x && a.y == b.y && a.z == b.z));
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator==(Vec4_i32 a, Vec4_i32 b){
|
|
|
|
return(a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static b32
|
2019-02-05 09:13:38 +00:00
|
|
|
operator!=(Vec4_i32 a, Vec4_i32 b){
|
|
|
|
return(!(a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w));
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
dot(Vec2 a, Vec2 b){
|
2017-01-23 06:19:43 +00:00
|
|
|
f32 result;
|
2016-02-07 21:15:58 +00:00
|
|
|
result = a.x*b.x + a.y*b.y;
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
dot(Vec3 a, Vec3 b){
|
2017-01-23 06:19:43 +00:00
|
|
|
f32 result;
|
2016-02-07 21:15:58 +00:00
|
|
|
result = a.x*b.x + a.y*b.y + a.z*b.z;
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-02-07 21:15:58 +00:00
|
|
|
dot(Vec4 a, Vec4 b){
|
2017-01-23 06:19:43 +00:00
|
|
|
f32 result;
|
2016-02-07 21:15:58 +00:00
|
|
|
result = a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2016-02-07 21:15:58 +00:00
|
|
|
cross(Vec3 a, Vec3 b){
|
|
|
|
Vec3 result;
|
|
|
|
result.x = a.y*b.z - b.y*a.z;
|
|
|
|
result.y = a.z*b.x - b.z*a.x;
|
|
|
|
result.z = a.x*b.y - b.x*a.y;
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2016-02-07 21:15:58 +00:00
|
|
|
hadamard(Vec2 a, Vec2 b){
|
2018-09-17 18:47:06 +00:00
|
|
|
a.x *= b.x;
|
|
|
|
a.y *= b.y;
|
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2016-02-07 21:15:58 +00:00
|
|
|
hadamard(Vec3 a, Vec3 b){
|
2018-09-17 18:47:06 +00:00
|
|
|
a.x *= b.x;
|
|
|
|
a.y *= b.y;
|
|
|
|
a.z *= b.z;
|
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2016-02-07 21:15:58 +00:00
|
|
|
hadamard(Vec4 a, Vec4 b){
|
2018-09-17 18:47:06 +00:00
|
|
|
a.x *= b.x;
|
|
|
|
a.y *= b.y;
|
|
|
|
a.z *= b.z;
|
|
|
|
a.w *= b.w;
|
|
|
|
return(a);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2016-02-07 21:15:58 +00:00
|
|
|
perp(Vec2 v){
|
2018-09-17 18:47:06 +00:00
|
|
|
return(V2(-v.y, v.x));
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2017-01-23 06:19:43 +00:00
|
|
|
polar_to_cartesian(f32 theta_degrees, f32 length){
|
2018-09-17 18:47:06 +00:00
|
|
|
return(V2(COS(theta_degrees), SIN(theta_degrees))*length);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2017-01-23 06:19:43 +00:00
|
|
|
rotate(Vec2 v, f32 theta_degrees){
|
2018-09-17 18:47:06 +00:00
|
|
|
f32 c = COS(theta_degrees);
|
|
|
|
f32 s = SIN(theta_degrees);
|
|
|
|
return(V2(v.x*c - v.y*s,
|
|
|
|
v.x*s + v.y*c));
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
////////////////////////////////
|
2016-02-07 21:15:58 +00:00
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-03-31 04:05:47 +00:00
|
|
|
lerp(f32 a, f32 t, f32 b){
|
|
|
|
return(a + (b-a)*t);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32
|
2016-03-31 04:05:47 +00:00
|
|
|
lerp(i32 a, f32 t, i32 b){
|
2018-09-17 18:47:06 +00:00
|
|
|
return((i32)(lerp((f32)a, t, (f32)b)));
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec2
|
2016-03-31 04:05:47 +00:00
|
|
|
lerp(Vec2 a, f32 t, Vec2 b){
|
|
|
|
return(a + (b-a)*t);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2016-03-31 04:05:47 +00:00
|
|
|
lerp(Vec3 a, f32 t, Vec3 b){
|
|
|
|
return(a + (b-a)*t);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2016-03-31 04:05:47 +00:00
|
|
|
lerp(Vec4 a, f32 t, Vec4 b){
|
|
|
|
return(a + (b-a)*t);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2016-03-31 04:05:47 +00:00
|
|
|
unlerp(f32 a, f32 x, f32 b){
|
|
|
|
f32 r = x;
|
2019-02-05 09:13:38 +00:00
|
|
|
if (b != a){
|
2017-01-07 02:59:55 +00:00
|
|
|
r = (x - a) / (b - a);
|
|
|
|
}
|
2016-03-31 04:05:47 +00:00
|
|
|
return(r);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
////////////////////////////////
|
2017-03-13 23:48:11 +00:00
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
// color
|
2016-02-07 21:15:58 +00:00
|
|
|
|
|
|
|
// TODO(allen): Convert colors to Vec4
|
2019-02-24 22:54:57 +00:00
|
|
|
static u32
|
2017-01-23 06:19:43 +00:00
|
|
|
color_blend(u32 a, f32 t, u32 b){
|
2016-02-07 21:15:58 +00:00
|
|
|
union{
|
|
|
|
u8 byte[4];
|
|
|
|
u32 comp;
|
|
|
|
} A, B, R;
|
2017-01-07 02:59:55 +00:00
|
|
|
|
2016-02-07 21:15:58 +00:00
|
|
|
A.comp = a;
|
|
|
|
B.comp = b;
|
|
|
|
|
|
|
|
R.byte[0] = (u8)lerp(A.byte[0], t, B.byte[0]);
|
|
|
|
R.byte[1] = (u8)lerp(A.byte[1], t, B.byte[1]);
|
|
|
|
R.byte[2] = (u8)lerp(A.byte[2], t, B.byte[2]);
|
|
|
|
R.byte[3] = (u8)lerp(A.byte[3], t, B.byte[3]);
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
return(R.comp);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec3
|
2016-02-07 21:15:58 +00:00
|
|
|
unpack_color3(u32 color){
|
|
|
|
Vec3 result;
|
|
|
|
result.r = ((color >> 16) & 0xFF) / 255.f;
|
|
|
|
result.g = ((color >> 8) & 0xFF) / 255.f;
|
|
|
|
result.b = ((color >> 0) & 0xFF) / 255.f;
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2016-02-07 21:15:58 +00:00
|
|
|
unpack_color4(u32 color){
|
|
|
|
Vec4 result;
|
|
|
|
result.a = ((color >> 24) & 0xFF) / 255.f;
|
|
|
|
result.r = ((color >> 16) & 0xFF) / 255.f;
|
|
|
|
result.g = ((color >> 8) & 0xFF) / 255.f;
|
|
|
|
result.b = ((color >> 0) & 0xFF) / 255.f;
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static u32
|
2016-02-07 21:15:58 +00:00
|
|
|
pack_color4(Vec4 color){
|
|
|
|
u32 result =
|
2018-09-17 18:47:06 +00:00
|
|
|
((u8)(color.a*255) << 24) |
|
|
|
|
((u8)(color.r*255) << 16) |
|
|
|
|
((u8)(color.g*255) << 8) |
|
|
|
|
((u8)(color.b*255) << 0);
|
2019-02-25 23:42:13 +00:00
|
|
|
return(result);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2016-02-07 21:15:58 +00:00
|
|
|
rgba_to_hsla(Vec4 rgba){
|
|
|
|
Vec4 hsla = {};
|
2017-01-23 06:19:43 +00:00
|
|
|
f32 max, min, delta;
|
2016-02-07 21:15:58 +00:00
|
|
|
i32 maxc;
|
|
|
|
hsla.a = rgba.a;
|
|
|
|
max = rgba.r; min = rgba.r;
|
|
|
|
maxc = 0;
|
|
|
|
if (rgba.r < rgba.g){
|
|
|
|
max = rgba.g;
|
|
|
|
maxc = 1;
|
|
|
|
}
|
|
|
|
if (rgba.b > max){
|
|
|
|
max = rgba.b;
|
|
|
|
maxc = 2;
|
|
|
|
}
|
|
|
|
if (rgba.r > rgba.g){
|
|
|
|
min = rgba.g;
|
|
|
|
}
|
|
|
|
if (rgba.b < min){
|
|
|
|
min = rgba.b;
|
|
|
|
}
|
|
|
|
delta = max - min;
|
|
|
|
|
2018-09-17 18:47:06 +00:00
|
|
|
hsla.z = (max + min)*.5f;
|
2016-02-07 21:15:58 +00:00
|
|
|
if (delta == 0){
|
|
|
|
hsla.x = 0.f;
|
|
|
|
hsla.y = 0.f;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch (maxc){
|
2017-01-07 02:59:55 +00:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
hsla.x = (rgba.g - rgba.b) / delta;
|
2018-09-17 18:47:06 +00:00
|
|
|
hsla.x += (rgba.g < rgba.b)*6.f;
|
2017-01-07 02:59:55 +00:00
|
|
|
}break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
hsla.x = (rgba.b - rgba.r) / delta;
|
|
|
|
hsla.x += 2.f;
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
hsla.x = (rgba.r - rgba.g) / delta;
|
|
|
|
hsla.x += 4.f;
|
|
|
|
}break;
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
2018-09-17 18:47:06 +00:00
|
|
|
hsla.x *= (1/6.f); //*60 / 360
|
2016-02-07 21:15:58 +00:00
|
|
|
hsla.y = delta / (1.f - ABS(2.f*hsla.z - 1.f));
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
return(hsla);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static Vec4
|
2016-02-07 21:15:58 +00:00
|
|
|
hsla_to_rgba(Vec4 hsla){
|
2019-02-10 07:48:07 +00:00
|
|
|
if (hsla.h >= 1.f){
|
|
|
|
hsla.h = 0.f;
|
|
|
|
}
|
2018-09-17 18:47:06 +00:00
|
|
|
f32 C = (1.f - ABS(2*hsla.z - 1.f))*hsla.y;
|
|
|
|
f32 X = C*(1.f-ABS(MOD(hsla.x*6.f, 2)-1.f));
|
2017-01-23 06:19:43 +00:00
|
|
|
f32 m = hsla.z - C*.5f;
|
2018-09-17 18:47:06 +00:00
|
|
|
i32 H = floor32(hsla.x*6.f);
|
2019-02-10 07:48:07 +00:00
|
|
|
Vec4 rgba = {};
|
2016-02-07 21:15:58 +00:00
|
|
|
rgba.a = hsla.a;
|
|
|
|
switch (H){
|
2017-01-23 06:19:43 +00:00
|
|
|
case 0: rgba.r = C; rgba.g = X; rgba.b = 0; break;
|
|
|
|
case 1: rgba.r = X; rgba.g = C; rgba.b = 0; break;
|
|
|
|
case 2: rgba.r = 0; rgba.g = C; rgba.b = X; break;
|
|
|
|
case 3: rgba.r = 0; rgba.g = X; rgba.b = C; break;
|
|
|
|
case 4: rgba.r = X; rgba.g = 0; rgba.b = C; break;
|
|
|
|
case 5: rgba.r = C; rgba.g = 0; rgba.b = X; break;
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
rgba.r += m;
|
|
|
|
rgba.g += m;
|
|
|
|
rgba.b += m;
|
2017-01-23 06:19:43 +00:00
|
|
|
return(rgba);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
////////////////////////////////
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2019-04-01 06:14:31 +00:00
|
|
|
static i32
|
|
|
|
get_width(Range range){
|
|
|
|
i32 result = range.end - range.start;
|
|
|
|
if (result < 0){
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Range
|
|
|
|
make_range(i32 p1, i32 p2){
|
|
|
|
Range range;
|
|
|
|
if (p1 < p2){
|
|
|
|
range.min = p1;
|
|
|
|
range.max = p2;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
range.min = p2;
|
|
|
|
range.max = p1;
|
|
|
|
}
|
|
|
|
return(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Range
|
|
|
|
rectify(Range range) {
|
|
|
|
return(make_range(range.min, range.max));
|
|
|
|
}
|
|
|
|
|
|
|
|
static b32
|
|
|
|
interval_overlap(i32 a0, i32 a1, i32 b0, i32 b1){
|
2019-04-05 02:03:36 +00:00
|
|
|
return(a0 < b1 && b0 < a1);
|
2019-04-01 06:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static b32
|
|
|
|
interval_overlap(Range a, Range b){
|
|
|
|
return(interval_overlap(a.first, a.one_past_last, b.first, b.one_past_last));
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:03:36 +00:00
|
|
|
static i32
|
|
|
|
interval_overlap(f32 a0, f32 a1, f32 b0, f32 b1){
|
|
|
|
return(a0 < b1 && b0 < a1);
|
|
|
|
}
|
|
|
|
|
2019-04-01 06:14:31 +00:00
|
|
|
static b32
|
|
|
|
interval_contains(i32 a0, i32 a1, i32 b){
|
|
|
|
return((a0 <= b) && (b < a1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static b32
|
|
|
|
interval_contains(Range range, i32 b){
|
|
|
|
return(interval_contains(range.start, range.one_past_last, b));
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:03:36 +00:00
|
|
|
static Range
|
|
|
|
clip_range_to_width(Range range, i32 max_width) {
|
|
|
|
i32 top = range.first + max_width;
|
|
|
|
range.end = clamp_top(range.end, top);
|
|
|
|
return(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
static b32
|
|
|
|
interval_is_valid(Range range){
|
|
|
|
return(range.start <= range.one_past_last);
|
|
|
|
}
|
|
|
|
|
2019-04-01 06:14:31 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32_Rect
|
2019-02-10 07:48:07 +00:00
|
|
|
i32R(i32 l, i32 t, i32 r, i32 b){
|
2018-11-20 08:18:54 +00:00
|
|
|
i32_Rect rect = {};
|
2018-10-09 23:51:45 +00:00
|
|
|
rect.x0 = l;
|
|
|
|
rect.y0 = t;
|
|
|
|
rect.x1 = r;
|
|
|
|
rect.y1 = b;
|
2017-01-23 06:19:43 +00:00
|
|
|
return(rect);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32_Rect
|
2019-02-24 07:22:16 +00:00
|
|
|
i32R_xy_wh(i32 x, i32 y, i32 w, i32 h){
|
|
|
|
return(i32R(x, y, x + w, y + h));
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32_Rect
|
2018-08-07 09:08:01 +00:00
|
|
|
i32R(f32_Rect r){
|
2019-02-10 07:48:07 +00:00
|
|
|
i32_Rect rect = {};
|
|
|
|
rect.x0 = (i32)r.x0;
|
|
|
|
rect.y0 = (i32)r.y0;
|
|
|
|
rect.x1 = (i32)r.x1;
|
|
|
|
rect.y1 = (i32)r.y1;
|
2018-08-07 09:08:01 +00:00
|
|
|
return(rect);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32_Rect
|
2019-02-10 07:48:07 +00:00
|
|
|
f32R(f32 l, f32 t, f32 r, f32 b){
|
|
|
|
f32_Rect rect = {};
|
|
|
|
rect.x0 = l;
|
|
|
|
rect.y0 = t;
|
|
|
|
rect.x1 = r;
|
|
|
|
rect.y1 = b;
|
2017-01-23 06:19:43 +00:00
|
|
|
return(rect);
|
|
|
|
}
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
static f32_Rect
|
|
|
|
f32R(Vec2 p0, Vec2 p1){
|
|
|
|
f32_Rect rect = {};
|
|
|
|
rect.p0 = p0;
|
|
|
|
rect.p1 = p1;
|
|
|
|
return(rect);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32_Rect
|
2017-01-23 06:19:43 +00:00
|
|
|
f32R(i32_Rect r){
|
2019-02-10 07:48:07 +00:00
|
|
|
f32_Rect rect = {};
|
|
|
|
rect.x0 = (f32)r.x0;
|
|
|
|
rect.y0 = (f32)r.y0;
|
|
|
|
rect.x1 = (f32)r.x1;
|
|
|
|
rect.y1 = (f32)r.y1;
|
2017-01-23 06:19:43 +00:00
|
|
|
return(rect);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32
|
2017-01-23 06:19:43 +00:00
|
|
|
rect_equal(i32_Rect r1, i32_Rect r2){
|
2019-02-10 07:48:07 +00:00
|
|
|
return(r1.x0 == r2.x0 && r1.y0 == r2.y0 && r1.x1 == r2.x1 && r1.y1 == r2.y1);
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
static b32
|
2019-04-05 02:03:36 +00:00
|
|
|
rect_contains_point(b32 BLAH, f32 x0, f32 y0, f32 x1, f32 y1, f32 x, f32 y){
|
|
|
|
return(x0 <= x && x < x1 && y0 <= y && y < y1);
|
2019-02-25 23:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static b32
|
2019-04-05 02:03:36 +00:00
|
|
|
rect_contains_point(b32 BLAH, i32 x0, i32 y0, i32 x1, i32 y1, i32 x, i32 y){
|
|
|
|
return(x0 <= x && x < x1 && y0 <= y && y < y1);
|
2019-02-25 23:42:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static b32
|
2019-04-05 02:03:36 +00:00
|
|
|
rect_contains_point(Rect_f32 rect, Vec2_f32 p){
|
|
|
|
return(rect_contains_point(false, rect.x0, rect.y0, rect.x1, rect.y1, p.x, p.y));
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:42:13 +00:00
|
|
|
static b32
|
2019-04-05 02:03:36 +00:00
|
|
|
rect_contains_point(Rect_i32 rect, Vec2_i32 p){
|
|
|
|
return(rect_contains_point(false, rect.x0, rect.y0, rect.x1, rect.y1, p.x, p.y));
|
2019-02-25 23:42:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32_Rect
|
2017-01-29 00:03:23 +00:00
|
|
|
get_inner_rect(i32_Rect outer, i32 margin){
|
2019-02-05 09:13:38 +00:00
|
|
|
i32_Rect r = {};
|
2017-01-23 06:19:43 +00:00
|
|
|
r.x0 = outer.x0 + margin;
|
|
|
|
r.y0 = outer.y0 + margin;
|
|
|
|
r.x1 = outer.x1 - margin;
|
|
|
|
r.y1 = outer.y1 - margin;
|
2017-01-29 00:03:23 +00:00
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32_Rect
|
2017-01-29 00:03:23 +00:00
|
|
|
get_inner_rect(f32_Rect outer, f32 margin){
|
2019-02-05 09:13:38 +00:00
|
|
|
f32_Rect r = {};
|
2017-01-29 00:03:23 +00:00
|
|
|
r.x0 = outer.x0 + margin;
|
|
|
|
r.y0 = outer.y0 + margin;
|
|
|
|
r.x1 = outer.x1 - margin;
|
|
|
|
r.y1 = outer.y1 - margin;
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32
|
2019-02-10 07:48:07 +00:00
|
|
|
rect_height(i32_Rect rect){
|
|
|
|
return(rect.y1 - rect.y0);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32
|
2019-02-10 07:48:07 +00:00
|
|
|
rect_width(i32_Rect rect){
|
|
|
|
return(rect.x1 - rect.x0);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32
|
2017-01-29 00:03:23 +00:00
|
|
|
fits_inside(i32_Rect rect, i32_Rect outer){
|
|
|
|
return(rect.x0 >= outer.x0 && rect.x1 <= outer.x1 && rect.y0 >= outer.y0 && rect.y1 <= outer.y1);
|
2016-02-07 21:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static i32
|
2019-02-10 07:48:07 +00:00
|
|
|
rect_overlap(f32_Rect a, f32_Rect b){
|
|
|
|
return(interval_overlap(a.x0, a.x1, b.x0, b.x1) &&
|
|
|
|
interval_overlap(a.y0, a.y1, b.y0, b.y1));
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2019-02-10 07:48:07 +00:00
|
|
|
rect_height(f32_Rect rect){
|
|
|
|
return(rect.y1 - rect.y0);
|
|
|
|
}
|
|
|
|
|
2019-02-24 22:54:57 +00:00
|
|
|
static f32
|
2019-02-10 07:48:07 +00:00
|
|
|
rect_width(f32_Rect rect){
|
|
|
|
return(rect.x1 - rect.x0);
|
2018-07-17 03:21:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 06:40:24 +00:00
|
|
|
static Vec2
|
|
|
|
rect_dim(f32_Rect rect){
|
|
|
|
return(V2(rect.x1 - rect.x0, rect.y1 - rect.y0));
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:03:36 +00:00
|
|
|
static Vec2
|
|
|
|
rect_center(f32_Rect rect){
|
|
|
|
return(V2(0.5f*(rect.x0 + rect.x1), 0.5f*(rect.y0 + rect.y1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define center_of rect_center
|
|
|
|
|
2019-03-15 08:38:28 +00:00
|
|
|
static i32_Rect
|
|
|
|
intersection_of(i32_Rect a, i32_Rect b)
|
|
|
|
{
|
|
|
|
i32_Rect result;
|
|
|
|
result.x0 = Max(a.x0, b.x0);
|
|
|
|
result.y0 = Max(a.y0, b.y0);
|
|
|
|
result.x1 = Min(a.x1, b.x1);
|
|
|
|
result.y1 = Min(a.y1, b.y1);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static i32_Rect
|
|
|
|
union_of(i32_Rect a, i32_Rect b)
|
|
|
|
{
|
|
|
|
i32_Rect result;
|
|
|
|
result.x0 = Max(a.x0, b.x0);
|
|
|
|
result.y0 = Max(a.y0, b.y0);
|
|
|
|
result.x1 = Min(a.x1, b.x1);
|
|
|
|
result.y1 = Min(a.y1, b.y1);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static f32_Rect
|
|
|
|
intersection_of(f32_Rect a, f32_Rect b)
|
|
|
|
{
|
|
|
|
f32_Rect result;
|
|
|
|
result.x0 = Max(a.x0, b.x0);
|
|
|
|
result.y0 = Max(a.y0, b.y0);
|
|
|
|
result.x1 = Min(a.x1, b.x1);
|
|
|
|
result.y1 = Min(a.y1, b.y1);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static f32_Rect
|
|
|
|
union_of(f32_Rect a, f32_Rect b)
|
|
|
|
{
|
|
|
|
f32_Rect result;
|
|
|
|
result.x0 = Max(a.x0, b.x0);
|
|
|
|
result.y0 = Max(a.y0, b.y0);
|
|
|
|
result.x1 = Min(a.x1, b.x1);
|
|
|
|
result.y1 = Min(a.y1, b.y1);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2016-02-07 21:15:58 +00:00
|
|
|
// BOTTOM
|
|
|
|
|