protocol uint/magnolia / source / mgmath_handmade.c
  1#include <HandmadeMath.h>
  2
  3#include "mgmath.h"
  4
  5static HMM_Vec3 mg_to_hmm_v3(MGVec3 vec);
  6static HMM_Mat4 mg_to_hmm_m4(MGMat4 mat);
  7static MGVec2   mg_from_hmm_v2(HMM_Vec2 vec);
  8static MGVec3   mg_from_hmm_v3(HMM_Vec3 vec);
  9static MGVec4   mg_from_hmm_v4(HMM_Vec4 vec);
 10static MGMat4   mg_from_hmm_m4(HMM_Mat4 mat);
 11
 12static HMM_Vec3 mg_to_hmm_v3(MGVec3 vec) { return HMM_V3(vec.x, vec.y, vec.z); }
 13
 14static HMM_Mat4 mg_to_hmm_m4(MGMat4 mat)
 15{
 16	HMM_Mat4 result;
 17	int      col;
 18	int      row;
 19
 20	for (col = 0; col < 4; col++) {
 21		for (row = 0; row < 4; row++)
 22			result.Elements[col][row] = mat.Elements[col][row];
 23	}
 24
 25	return result;
 26}
 27
 28static MGVec2 mg_from_hmm_v2(HMM_Vec2 vec)
 29{
 30	MGVec2 result;
 31
 32	result.x = vec.X;
 33	result.y = vec.Y;
 34
 35	return result;
 36}
 37
 38static MGVec3 mg_from_hmm_v3(HMM_Vec3 vec)
 39{
 40	MGVec3 result;
 41
 42	result.x = vec.X;
 43	result.y = vec.Y;
 44	result.z = vec.Z;
 45
 46	return result;
 47}
 48
 49static MGVec4 mg_from_hmm_v4(HMM_Vec4 vec)
 50{
 51	MGVec4 result;
 52
 53	result.x = vec.X;
 54	result.y = vec.Y;
 55	result.z = vec.Z;
 56	result.w = vec.W;
 57
 58	return result;
 59}
 60
 61static MGMat4 mg_from_hmm_m4(HMM_Mat4 mat)
 62{
 63	MGMat4 result;
 64	int    col;
 65	int    row;
 66
 67	for (col = 0; col < 4; col++) {
 68		for (row = 0; row < 4; row++)
 69			result.Elements[col][row] = mat.Elements[col][row];
 70	}
 71
 72	return result;
 73}
 74
 75float MG_SinF(float angle) { return HMM_SinF(angle); }
 76
 77float MG_CosF(float angle) { return HMM_CosF(angle); }
 78
 79float MG_TanF(float angle) { return HMM_TanF(angle); }
 80
 81float MG_SqrtF(float value) { return HMM_SqrtF(value); }
 82
 83MGVec2 MG_V2(float x, float y) { return mg_from_hmm_v2(HMM_V2(x, y)); }
 84
 85MGVec3 MG_V3(float x, float y, float z) { return mg_from_hmm_v3(HMM_V3(x, y, z)); }
 86
 87MGVec4 MG_V4(float x, float y, float z, float w) { return mg_from_hmm_v4(HMM_V4(x, y, z, w)); }
 88
 89MGVec3 MG_AddV3(MGVec3 left, MGVec3 right)
 90{
 91	return mg_from_hmm_v3(HMM_AddV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
 92}
 93
 94MGVec3 MG_SubV3(MGVec3 left, MGVec3 right)
 95{
 96	return mg_from_hmm_v3(HMM_SubV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
 97}
 98
 99MGVec3 MG_MulV3F(MGVec3 left, float right)
100{
101	return mg_from_hmm_v3(HMM_MulV3F(mg_to_hmm_v3(left), right));
102}
103
104float MG_DotV3(MGVec3 left, MGVec3 right)
105{
106	return HMM_DotV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right));
107}
108
109MGVec3 MG_Cross(MGVec3 left, MGVec3 right)
110{
111	return mg_from_hmm_v3(HMM_Cross(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
112}
113
114MGVec3 MG_NormV3(MGVec3 vec) { return mg_from_hmm_v3(HMM_NormV3(mg_to_hmm_v3(vec))); }
115
116MGMat4 MG_M4(void) { return mg_from_hmm_m4(HMM_M4()); }
117
118MGMat4 MG_M4D(float diagonal) { return mg_from_hmm_m4(HMM_M4D(diagonal)); }
119
120MGMat4 MG_MulM4(MGMat4 left, MGMat4 right)
121{
122	return mg_from_hmm_m4(HMM_MulM4(mg_to_hmm_m4(left), mg_to_hmm_m4(right)));
123}
124
125MGMat4 MG_Translate(MGVec3 translation)
126{
127	return mg_from_hmm_m4(HMM_Translate(mg_to_hmm_v3(translation)));
128}
129
130MGMat4 MG_Perspective_RH_NO(float fov_y, float aspect, float near_z, float far_z)
131{
132	return mg_from_hmm_m4(HMM_Perspective_RH_NO(fov_y, aspect, near_z, far_z));
133}
134
135MGMat4 MG_LookAt_RH(MGVec3 eye, MGVec3 center, MGVec3 up)
136{
137	return mg_from_hmm_m4(
138	    HMM_LookAt_RH(mg_to_hmm_v3(eye), mg_to_hmm_v3(center), mg_to_hmm_v3(up)));
139}