small3dlib 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)
 13{
 14	return HMM_V3(vec.x, vec.y, vec.z);
 15}
 16
 17static HMM_Mat4 mg_to_hmm_m4(MGMat4 mat)
 18{
 19	HMM_Mat4 result;
 20	int col;
 21	int row;
 22
 23	for (col = 0; col < 4; col++) {
 24		for (row = 0; row < 4; row++)
 25			result.Elements[col][row] = mat.Elements[col][row];
 26	}
 27
 28	return result;
 29}
 30
 31static MGVec2 mg_from_hmm_v2(HMM_Vec2 vec)
 32{
 33	MGVec2 result;
 34
 35	result.x = vec.X;
 36	result.y = vec.Y;
 37
 38	return result;
 39}
 40
 41static MGVec3 mg_from_hmm_v3(HMM_Vec3 vec)
 42{
 43	MGVec3 result;
 44
 45	result.x = vec.X;
 46	result.y = vec.Y;
 47	result.z = vec.Z;
 48
 49	return result;
 50}
 51
 52static MGVec4 mg_from_hmm_v4(HMM_Vec4 vec)
 53{
 54	MGVec4 result;
 55
 56	result.x = vec.X;
 57	result.y = vec.Y;
 58	result.z = vec.Z;
 59	result.w = vec.W;
 60
 61	return result;
 62}
 63
 64static MGMat4 mg_from_hmm_m4(HMM_Mat4 mat)
 65{
 66	MGMat4 result;
 67	int col;
 68	int row;
 69
 70	for (col = 0; col < 4; col++) {
 71		for (row = 0; row < 4; row++)
 72			result.Elements[col][row] = mat.Elements[col][row];
 73	}
 74
 75	return result;
 76}
 77
 78float MG_SinF(float angle)
 79{
 80	return HMM_SinF(angle);
 81}
 82
 83float MG_CosF(float angle)
 84{
 85	return HMM_CosF(angle);
 86}
 87
 88float MG_TanF(float angle)
 89{
 90	return HMM_TanF(angle);
 91}
 92
 93float MG_SqrtF(float value)
 94{
 95	return HMM_SqrtF(value);
 96}
 97
 98MGVec2 MG_V2(float x, float y)
 99{
100	return mg_from_hmm_v2(HMM_V2(x, y));
101}
102
103MGVec3 MG_V3(float x, float y, float z)
104{
105	return mg_from_hmm_v3(HMM_V3(x, y, z));
106}
107
108MGVec4 MG_V4(float x, float y, float z, float w)
109{
110	return mg_from_hmm_v4(HMM_V4(x, y, z, w));
111}
112
113MGVec3 MG_AddV3(MGVec3 left, MGVec3 right)
114{
115	return mg_from_hmm_v3(HMM_AddV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
116}
117
118MGVec3 MG_SubV3(MGVec3 left, MGVec3 right)
119{
120	return mg_from_hmm_v3(HMM_SubV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
121}
122
123MGVec3 MG_MulV3F(MGVec3 left, float right)
124{
125	return mg_from_hmm_v3(HMM_MulV3F(mg_to_hmm_v3(left), right));
126}
127
128float MG_DotV3(MGVec3 left, MGVec3 right)
129{
130	return HMM_DotV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right));
131}
132
133MGVec3 MG_Cross(MGVec3 left, MGVec3 right)
134{
135	return mg_from_hmm_v3(HMM_Cross(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
136}
137
138MGVec3 MG_NormV3(MGVec3 vec)
139{
140	return mg_from_hmm_v3(HMM_NormV3(mg_to_hmm_v3(vec)));
141}
142
143MGMat4 MG_M4(void)
144{
145	return mg_from_hmm_m4(HMM_M4());
146}
147
148MGMat4 MG_M4D(float diagonal)
149{
150	return mg_from_hmm_m4(HMM_M4D(diagonal));
151}
152
153MGMat4 MG_MulM4(MGMat4 left, MGMat4 right)
154{
155	return mg_from_hmm_m4(HMM_MulM4(mg_to_hmm_m4(left), mg_to_hmm_m4(right)));
156}
157
158MGMat4 MG_Translate(MGVec3 translation)
159{
160	return mg_from_hmm_m4(HMM_Translate(mg_to_hmm_v3(translation)));
161}
162
163MGMat4 MG_Perspective_RH_NO(float fov_y, float aspect, float near_z, float far_z)
164{
165	return mg_from_hmm_m4(HMM_Perspective_RH_NO(fov_y, aspect, near_z, far_z));
166}
167
168MGMat4 MG_LookAt_RH(MGVec3 eye, MGVec3 center, MGVec3 up)
169{
170	return mg_from_hmm_m4(HMM_LookAt_RH(
171		mg_to_hmm_v3(eye),
172		mg_to_hmm_v3(center),
173		mg_to_hmm_v3(up)
174	));
175}
176