2021-11-30 21:29:18 -06:00
|
|
|
#include "transform.h"
|
|
|
|
#include <string.h>
|
2024-05-02 13:52:28 -05:00
|
|
|
#include <stdio.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2024-05-12 18:36:14 -05:00
|
|
|
transform *make_transform()
|
2024-05-02 13:52:28 -05:00
|
|
|
{
|
2024-05-12 18:36:14 -05:00
|
|
|
transform *t = calloc(sizeof(transform),1);
|
2024-07-23 14:30:41 -05:00
|
|
|
|
2024-05-12 18:36:14 -05:00
|
|
|
t->scale = (HMM_Vec3){1,1,1};
|
2024-05-13 14:07:00 -05:00
|
|
|
t->rotation = (HMM_Quat){0,0,0,1};
|
2024-07-23 14:30:41 -05:00
|
|
|
t->dirty = 1;
|
2024-05-02 13:52:28 -05:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2024-05-12 18:36:14 -05:00
|
|
|
void transform_free(transform *t) { free(t); }
|
2024-05-02 13:52:28 -05:00
|
|
|
|
2024-07-23 14:30:41 -05:00
|
|
|
void transform_apply(transform *t) { t->dirty = 1; }
|
|
|
|
|
2024-05-15 07:54:19 -05:00
|
|
|
void transform_move(transform *t, HMM_Vec3 v)
|
|
|
|
{
|
|
|
|
t->pos = HMM_AddV3(t->pos, v);
|
2024-07-23 14:30:41 -05:00
|
|
|
t->dirty = 1;
|
2024-05-15 07:54:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
HMM_Vec3 transform_direction(transform *t, HMM_Vec3 dir)
|
|
|
|
{
|
|
|
|
return HMM_QVRot(dir, t->rotation);
|
|
|
|
}
|
|
|
|
|
2024-05-12 18:36:14 -05:00
|
|
|
HMM_Vec3 trans_forward(const transform *const trans) { return HMM_QVRot(vFWD, trans->rotation); }
|
|
|
|
HMM_Vec3 trans_back(const transform *trans) { return HMM_QVRot(vBKWD, trans->rotation); }
|
|
|
|
HMM_Vec3 trans_up(const transform *trans) { return HMM_QVRot(vUP, trans->rotation); }
|
|
|
|
HMM_Vec3 trans_down(const transform *trans) { return HMM_QVRot(vDOWN, trans->rotation); }
|
|
|
|
HMM_Vec3 trans_right(const transform *trans) { return HMM_QVRot(vRIGHT, trans->rotation); }
|
|
|
|
HMM_Vec3 trans_left(const transform *trans) { return HMM_QVRot(vLEFT, trans->rotation); }
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-12-12 08:46:27 -06:00
|
|
|
HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos) { return HMM_MulM3V3(m, (HMM_Vec3){pos.x, pos.y, 1}).xy; }
|
2023-11-30 10:47:59 -06:00
|
|
|
|
|
|
|
HMM_Vec2 mat_t_dir(HMM_Mat3 m, HMM_Vec2 dir)
|
|
|
|
{
|
|
|
|
m.Columns[2] = (HMM_Vec3){0,0,1};
|
|
|
|
return HMM_MulM3V3(m, (HMM_Vec3){dir.x, dir.y, 1}).XY;
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:24:26 -06:00
|
|
|
HMM_Vec2 mat_up(HMM_Mat3 m) { return HMM_NormV2(m.Columns[1].XY); }
|
|
|
|
HMM_Vec2 mat_right(HMM_Mat3 m) { return HMM_NormV2(m.Columns[0].XY); }
|
|
|
|
float vec_angle(HMM_Vec2 a, HMM_Vec2 b) { return acos(HMM_DotV2(a,b)/(HMM_LenV2(a)*HMM_LenV2(b))); }
|
|
|
|
float vec_dirangle(HMM_Vec2 a, HMM_Vec2 b) { return atan2(b.x, b.y) - atan2(a.x, a.y); }
|
|
|
|
|
|
|
|
HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos) { return HMM_MulM4V4(m, (HMM_Vec4){pos.X, pos.Y, pos.Z, 1}).XYZ; }
|
2023-11-30 10:47:59 -06:00
|
|
|
|
|
|
|
HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir)
|
|
|
|
{
|
2023-12-21 17:21:01 -06:00
|
|
|
m.Columns[3] = (HMM_Vec4){0,0,0,1};
|
2023-11-30 10:47:59 -06:00
|
|
|
return mat3_t_pos(m, dir);
|
|
|
|
}
|
|
|
|
|
2024-07-23 14:30:41 -05:00
|
|
|
HMM_Mat4 transform2mat(transform *t) {
|
2024-07-23 17:21:27 -05:00
|
|
|
return HMM_M4TRS(t->pos, t->rotation, t->scale);
|
|
|
|
|
2024-07-23 14:30:41 -05:00
|
|
|
if (t->dirty) {
|
|
|
|
t->cache = HMM_M4TRS(t->pos, t->rotation, t->scale);
|
|
|
|
t->dirty = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t->cache;
|
2023-12-19 15:34:36 -06:00
|
|
|
}
|
2023-12-04 13:38:37 -06:00
|
|
|
|
2024-05-12 18:36:14 -05:00
|
|
|
HMM_Quat angle2rotation(float angle)
|
2024-04-23 15:58:08 -05:00
|
|
|
{
|
2024-05-17 12:39:04 -05:00
|
|
|
return HMM_QFromAxisAngle_LH(vBKWD, angle);
|
2024-04-26 16:04:31 -05:00
|
|
|
}
|
2023-12-04 13:38:37 -06:00
|
|
|
|
2024-05-12 18:36:14 -05:00
|
|
|
transform mat2transform(HMM_Mat4 m)
|
2023-12-04 13:38:37 -06:00
|
|
|
{
|
2024-05-12 18:36:14 -05:00
|
|
|
transform t;
|
2023-12-04 13:38:37 -06:00
|
|
|
t.pos = m.Columns[3].xyz;
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
t.scale.Elements[i] = HMM_LenV3(m.Columns[i].xyz);
|
|
|
|
// for (int i = 0; i < 2; i++)
|
|
|
|
// m.Columns[i].xyz = HMM_MulV3(m.Columns[i].xyz, t.scale.Elements[i]);
|
2024-05-14 15:22:24 -05:00
|
|
|
t.rotation = HMM_M4ToQ_LH(m);
|
2023-12-21 17:21:01 -06:00
|
|
|
return t;
|
2023-12-04 13:38:37 -06:00
|
|
|
}
|