revolver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

platform_math.h (4308B)


      1 //////////////////////////////////////////////////////////////////
      2 // platform_math.h
      3 
      4 
      5 //////////////////////////////////////////////////////////////////
      6 // Misc
      7 
      8 #include <math.h>
      9 
     10 #define KB(n)  (((u64)(n)) << 10)
     11 #define MB(n)  (((u64)(n)) << 20)
     12 #define GB(n)  (((u64)(n)) << 30)
     13 #define TB(n)  (((u64)(n)) << 40)
     14 
     15 #define rv_min(A, B) ((A) < (B) ? (A) : (B))
     16 #define rv_max(A, B) ((A) > (B) ? (A) : (B))
     17 
     18 #define rv_array_count(arr) sizeof(arr) / sizeof(arr[0])
     19 
     20 #define RV_EPSILON  1e-6
     21 #define RV_PI       3.1415926535897932
     22 
     23 #define RV_XAXIS    rv_v3(1, 0, 0)
     24 #define RV_YAXIS    rv_v3(0, 1, 0)
     25 #define RV_ZAXIS    rv_v3(0, 0, 1)
     26 
     27 #define rv_rad2deg(radians)\
     28     (float)((radians * 180.0f) / RV_PI)
     29 
     30 #define rv_deg2rad(degrees)\
     31     (float)((degrees * RV_PI) / 180.0f)
     32 
     33 // range
     34 
     35 RV_GLOBAL s64 rv_range_diff(rv_range range);
     36 
     37 //////////////////////////////////////////////////////////////////
     38 // vector math
     39 
     40 // vec2
     41 RV_GLOBAL rv_vec2 rv_vec2_neg(rv_vec2 v);
     42 RV_GLOBAL rv_vec2 rv_vec2_norm(rv_vec2 v);
     43 RV_GLOBAL rv_vec2 rv_vec2_scale(rv_vec2 v, f32 s);
     44 RV_GLOBAL rv_vec2 rv_vec2_add(rv_vec2 a, rv_vec2 b);
     45 RV_GLOBAL rv_vec2 rv_vec2_sub(rv_vec2 a, rv_vec2 b);
     46 RV_GLOBAL rv_vec2 rv_vec2_mul(rv_vec2 a, rv_vec2 b);
     47 RV_GLOBAL rv_vec2 rv_vec2_div(rv_vec2 a, rv_vec2 b);
     48 RV_GLOBAL rv_vec2 rv_vec2_project_onto(rv_vec2 a, rv_vec2 b);
     49 
     50 RV_GLOBAL f32 rv_vec2_len(rv_vec2 v);
     51 RV_GLOBAL f32 rv_vec2_dist(rv_vec2 a, rv_vec2 b);
     52 RV_GLOBAL f32 rv_vec2_cross(rv_vec2 a, rv_vec2 b);
     53 RV_GLOBAL f32 rv_vec2_angle(rv_vec2 a, rv_vec2 b);
     54 RV_GLOBAL f32 rv_vec2_dot(rv_vec2 a, rv_vec2 b);
     55 
     56 RV_GLOBAL bool32 rv_vec2_nan(rv_vec2 v);
     57 RV_GLOBAL bool32 rv_vec2_eq(rv_vec2 a, rv_vec2 b);
     58 
     59 // vec3
     60 RV_GLOBAL rv_vec3 rv_vec3_neg(rv_vec3 v);
     61 RV_GLOBAL rv_vec3 rv_vec3_scale(rv_vec3 v, f32 s);
     62 RV_GLOBAL rv_vec3 rv_vec3_norm(rv_vec3 v);
     63 RV_GLOBAL rv_vec3 rv_vec3_add(rv_vec3 a, rv_vec3 b);
     64 RV_GLOBAL rv_vec3 rv_vec3_sub(rv_vec3 a, rv_vec3 b);
     65 RV_GLOBAL rv_vec3 rv_vec3_mul(rv_vec3 a, rv_vec3 b);
     66 RV_GLOBAL rv_vec3 rv_vec3_div(rv_vec3 a, rv_vec3 b);
     67 RV_GLOBAL rv_vec3 rv_vec3_project_onto(rv_vec3 a, rv_vec3 b);
     68 RV_GLOBAL rv_vec3 rv_vec3_cross(rv_vec3 a, rv_vec3 b);
     69 
     70 RV_GLOBAL f32 rv_vec3_len(rv_vec3 v);
     71 RV_GLOBAL f32 rv_vec3_dist(rv_vec3 a, rv_vec3 b);
     72 RV_GLOBAL f32 rv_vec3_dot(rv_vec3 a, rv_vec3 b);
     73 
     74 RV_GLOBAL bool32 rv_vec3_nan(rv_vec3 v);
     75 RV_GLOBAL bool32 rv_vec3_eq(rv_vec3 a, rv_vec3 b);
     76 RV_GLOBAL bool32 rv_vec3_same_dir(rv_vec3 a, rv_vec3 b);
     77 
     78 // vec4
     79 RV_GLOBAL rv_vec4 rv_vec4_neg(rv_vec4 v);
     80 RV_GLOBAL rv_vec4 rv_vec4_scale(rv_vec4 v, f32 s);
     81 RV_GLOBAL rv_vec4 rv_vec4_norm(rv_vec4 v);
     82 RV_GLOBAL rv_vec4 rv_vec4_add(rv_vec4 a, rv_vec4 b);
     83 RV_GLOBAL rv_vec4 rv_vec4_sub(rv_vec4 a, rv_vec4 b);
     84 RV_GLOBAL rv_vec4 rv_vec4_mul(rv_vec4 a, rv_vec4 b);
     85 RV_GLOBAL rv_vec4 rv_vec4_div(rv_vec4 a, rv_vec4 b);
     86 RV_GLOBAL rv_vec4 rv_vec4_project_onto(rv_vec4 a, rv_vec4 b);
     87 RV_GLOBAL rv_vec4 rv_vec4_from_color(rv_color_t c);
     88 
     89 RV_GLOBAL f32 rv_vec4_len(rv_vec4 v);
     90 RV_GLOBAL f32 rv_vec4_dist(rv_vec4 a, rv_vec4 b);
     91 RV_GLOBAL f32 rv_vec4_dot(rv_vec4 a, rv_vec4 b);
     92 
     93 RV_GLOBAL bool32 rv_vec4_nan(rv_vec4 v);
     94 RV_GLOBAL bool32 rv_vec4_eq(rv_vec4 a, rv_vec4 b);
     95 
     96 
     97 //////////////////////////////////////////////////////////////////
     98 // matrix math
     99 
    100 RV_GLOBAL rv_mat4 rv_mat4_translatev(rv_vec3 v);
    101 RV_GLOBAL rv_mat4 rv_mat4_rotatev(f32 angle, rv_vec3 axis);
    102 RV_GLOBAL rv_mat4 rv_mat4_scalev(rv_vec3 v);
    103 RV_GLOBAL rv_mat4 rv_mat4_transpose(rv_mat4 m);
    104 RV_GLOBAL rv_mat4 rv_mat4_inverse(rv_mat4 m);
    105 RV_GLOBAL rv_mat4 rv_mat4_look_at(rv_vec3 position, rv_vec3 target, rv_vec3 up);
    106 
    107 RV_GLOBAL rv_mat4 rv_mat4_mul(rv_mat4 m0, rv_mat4 m1);
    108 RV_GLOBAL rv_mat4 rv_mat4_mul_list_(rv_mat4* m_list, s64 m_list_len);
    109 #define rv_mat4_mul_list(...) rv_mat4_mul_list_((rv_mat4[]){__VA_ARGS__}, rv_array_size((rv_mat4[]){__VA_ARGS__}))
    110 RV_GLOBAL rv_vec4 rv_mat4_mul_vec4(rv_mat4 m, rv_vec4 v);
    111 RV_GLOBAL rv_vec3 rv_mat4_mul_vec3(rv_mat4 m, rv_vec3 v);
    112 
    113 RV_GLOBAL rv_mat4 rv_mat4_ortho_norm(rv_mat4 m);
    114 RV_GLOBAL rv_mat4 rv_mat4_ortho(f32 l, f32 r, f32 b, f32 t, f32 n, f32 f);
    115 RV_GLOBAL rv_mat4 rv_mat4_perspective(f32 fov, f32 asp_ratio, f32 n, f32 f);
    116 
    117 RV_GLOBAL void rv_mat4_decompose(const rv_mat4* m, rv_vec3* translation, rv_vec3* rotation, rv_vec3* scale);
    118 RV_GLOBAL rv_mat4 rv_mat4_recompose(rv_vec3 translation, rv_vec3 rotation, rv_vec3 scale);