Files
Microtransactions64/src/engine/math_util.h

86 lines
3.4 KiB
C
Raw Normal View History

2020-06-02 12:44:34 -04:00
#ifndef MATH_UTIL_H
#define MATH_UTIL_H
#include <PR/ultratypes.h>
2020-01-03 10:38:57 -05:00
#include "types.h"
2019-08-25 00:46:40 -04:00
2019-11-03 14:36:27 -05:00
/*
* The sine and cosine tables overlap, but "#define gCosineTable (gSineTable +
* 0x400)" doesn't give expected codegen; gSineTable and gCosineTable need to
* be different symbols for code to match. Most likely the tables were placed
* adjacent to each other, and gSineTable cut short, such that reads overflow
* into gCosineTable.
*
* These kinds of out of bounds reads are undefined behavior, and break on
* e.g. GCC (which doesn't place the tables next to each other, and probably
* exploits array sizes for range analysis-based optimizations as well).
* Thus, for non-IDO compilers we use the standard-compliant version.
*/
2019-08-25 00:46:40 -04:00
extern f32 gSineTable[];
2019-12-01 21:52:53 -05:00
#ifdef AVOID_UB
2019-11-03 14:36:27 -05:00
#define gCosineTable (gSineTable + 0x400)
#else
2019-08-25 00:46:40 -04:00
extern f32 gCosineTable[];
2019-11-03 14:36:27 -05:00
#endif
2019-08-25 00:46:40 -04:00
#define sins(x) gSineTable[(u16) (x) >> 4]
#define coss(x) gCosineTable[(u16) (x) >> 4]
#define min(a, b) ((a) <= (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
2021-08-15 11:30:19 +01:00
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#define ABS(x) ((x) > 0.f ? (x) : -(x))
2019-08-25 00:46:40 -04:00
#define sqr(x) ((x) * (x))
#define RAYCAST_FIND_FLOOR (0x1)
#define RAYCAST_FIND_WALL (0x2)
#define RAYCAST_FIND_CEIL (0x4)
#define RAYCAST_FIND_WATER (0x8)
#define RAYCAST_FIND_ALL (0xFFFFFFFF)
2019-08-25 00:46:40 -04:00
void *vec3f_copy(Vec3f dest, Vec3f src);
void *vec3f_set(Vec3f dest, f32 x, f32 y, f32 z);
void *vec3f_add(Vec3f dest, Vec3f a);
void *vec3f_sum(Vec3f dest, Vec3f a, Vec3f b);
void *vec3s_copy(Vec3s dest, Vec3s src);
void *vec3s_set(Vec3s dest, s16 x, s16 y, s16 z);
void *vec3s_add(Vec3s dest, Vec3s a);
void *vec3s_sum(Vec3s dest, Vec3s a, Vec3s b);
void *vec3s_sub(Vec3s dest, Vec3s a);
void *vec3s_to_vec3f(Vec3f dest, Vec3s a);
void *vec3f_to_vec3s(Vec3s dest, Vec3f a);
void *find_vector_perpendicular_to_plane(Vec3f dest, Vec3f a, Vec3f b, Vec3f c);
void *vec3f_cross(Vec3f dest, Vec3f a, Vec3f b);
void *vec3f_normalize(Vec3f dest);
2020-06-02 12:44:34 -04:00
void mtxf_copy(Mat4 dest, Mat4 src);
void mtxf_identity(Mat4 mtx);
void mtxf_translate(Mat4 dest, Vec3f b);
void mtxf_lookat(Mat4 mtx, Vec3f from, Vec3f to, s16 roll);
void mtxf_rotate_zxy_and_translate(Mat4 dest, Vec3f translate, Vec3s rotate);
void mtxf_rotate_xyz_and_translate(Mat4 dest, Vec3f b, Vec3s c);
void mtxf_billboard(Mat4 dest, Mat4 mtx, Vec3f position, s16 angle);
void mtxf_align_terrain_normal(Mat4 dest, Vec3f upDir, Vec3f pos, s16 yaw);
void mtxf_align_terrain_triangle(Mat4 mtx, Vec3f pos, s16 yaw, f32 radius);
void mtxf_mul(Mat4 dest, Mat4 a, Mat4 b);
void mtxf_scale_vec3f(Mat4 dest, Mat4 mtx, Vec3f s);
void mtxf_mul_vec3s(Mat4 mtx, Vec3s b);
void mtxf_to_mtx(Mtx *dest, Mat4 src);
void mtxf_rotate_xy(Mtx *mtx, s16 angle);
void get_pos_from_transform_mtx(Vec3f dest, Mat4 objMtx, Mat4 camMtx);
2019-11-03 14:36:27 -05:00
void vec3f_get_dist_and_angle(Vec3f from, Vec3f to, f32 *dist, s16 *pitch, s16 *yaw);
2020-01-03 10:38:57 -05:00
void vec3f_set_dist_and_angle(Vec3f from, Vec3f to, f32 dist, s16 pitch, s16 yaw);
s32 approach_s32(s32 current, s32 target, s32 inc, s32 dec);
f32 approach_f32(f32 current, f32 target, f32 inc, f32 dec);
2020-06-02 12:44:34 -04:00
s16 atan2s(f32 y, f32 x);
2019-08-25 00:46:40 -04:00
f32 atan2f(f32 a, f32 b);
2020-06-02 12:44:34 -04:00
void spline_get_weights(Vec4f result, f32 t, UNUSED s32 c);
void anim_spline_init(Vec4s *keyFrames);
s32 anim_spline_poll(Vec3f result);
extern void find_surface_on_ray(Vec3f orig, Vec3f dir, struct Surface **hit_surface, Vec3f hit_pos, s32 flags);
2019-08-25 00:46:40 -04:00
2020-06-02 12:44:34 -04:00
#endif // MATH_UTIL_H