commit eabd47d
uint
·
2026-07-23 13:22:11 +0000 UTC
parent 489391e
shim HandmadeMath behind mgmath back to C89
6 files changed,
+272,
-21
+56,
-0
1@@ -0,0 +1,56 @@
2+#ifndef MGMATH_H
3+#define MGMATH_H
4+
5+#define MG_PI (3.14159265358979323846f)
6+#define MG_DEG_TO_RAD (MG_PI / 180.0f)
7+
8+#define MG_AngleRad(a) (a)
9+#define MG_AngleDeg(a) ((a) * MG_DEG_TO_RAD)
10+
11+typedef struct {
12+ float x;
13+ float y;
14+} MGVec2;
15+
16+typedef struct {
17+ float x;
18+ float y;
19+ float z;
20+} MGVec3;
21+
22+typedef struct {
23+ float x;
24+ float y;
25+ float z;
26+ float w;
27+} MGVec4;
28+
29+typedef struct {
30+ float Elements[4][4];
31+} MGMat4;
32+
33+float MG_SinF(float angle);
34+float MG_CosF(float angle);
35+float MG_TanF(float angle);
36+float MG_SqrtF(float value);
37+
38+MGVec2 MG_V2(float x, float y);
39+MGVec3 MG_V3(float x, float y, float z);
40+MGVec4 MG_V4(float x, float y, float z, float w);
41+
42+MGVec3 MG_AddV3(MGVec3 left, MGVec3 right);
43+MGVec3 MG_SubV3(MGVec3 left, MGVec3 right);
44+MGVec3 MG_MulV3F(MGVec3 left, float right);
45+float MG_DotV3(MGVec3 left, MGVec3 right);
46+MGVec3 MG_Cross(MGVec3 left, MGVec3 right);
47+MGVec3 MG_NormV3(MGVec3 vec);
48+
49+MGMat4 MG_M4(void);
50+MGMat4 MG_M4D(float diagonal);
51+MGMat4 MG_MulM4(MGMat4 left, MGMat4 right);
52+MGMat4 MG_Translate(MGVec3 translation);
53+MGMat4 MG_Perspective_RH_NO(float fov_y, float aspect, float near_z, float far_z);
54+MGMat4 MG_LookAt_RH(MGVec3 eye, MGVec3 center, MGVec3 up);
55+
56+#endif /* MGMATH_H */
57+
+2,
-1
1@@ -2,6 +2,7 @@
2 #define RENDERER_H
3
4 #include "types.h"
5+#include "mgmath.h"
6 #include "platform/platform.h"
7
8 typedef u32 MGMesh;
9@@ -42,7 +43,7 @@ void renderer_destroy(void);
10 MGMesh renderer_mesh_create(const MGMeshDesc* desc);
11
12 /* draws mesh using specified mvp mat */
13-void renderer_mesh_draw(MGMesh id, const float mvp[16]); /* TODO: use MGMat4 or wtv itll be called */
14+void renderer_mesh_draw(MGMesh id, const MGMat4* mvp);
15
16 /* destroy mesh */
17 void renderer_mesh_destroy(MGMesh id);
+15,
-16
1@@ -1,8 +1,7 @@
2 #ifndef TEST_SCENE_H
3 #define TEST_SCENE_H
4
5-#include <HandmadeMath.h>
6-
7+#include "mgmath.h"
8 #include "util.h"
9 #include "renderer/renderer.h"
10 #include "renderer/test_block.h"
11@@ -19,11 +18,11 @@ void test_scene_create(void)
12
13 void test_scene_draw(MGContext* ctx)
14 {
15- HMM_Mat4 proj;
16- HMM_Mat4 view;
17- HMM_Mat4 model;
18- HMM_Mat4 view_model;
19- HMM_Mat4 mvp;
20+ MGMat4 proj;
21+ MGMat4 view;
22+ MGMat4 model;
23+ MGMat4 view_model;
24+ MGMat4 mvp;
25 i32 width;
26 i32 height;
27 float aspect;
28@@ -35,18 +34,18 @@ void test_scene_draw(MGContext* ctx)
29
30 aspect = (float)width / (float)height;
31
32- proj = HMM_Perspective_RH_NO(HMM_AngleDeg(70.0f), aspect, 0.1f, 100.0f);
33- view = HMM_LookAt_RH(
34- HMM_V3(2.5f, 2.0f, 3.0f),
35- HMM_V3(0.5f, 0.5f, 0.5f),
36- HMM_V3(0.0f, 1.0f, 0.0f)
37+ proj = MG_Perspective_RH_NO(MG_AngleDeg(70.0f), aspect, 0.1f, 100.0f);
38+ view = MG_LookAt_RH(
39+ MG_V3(2.5f, 2.0f, 3.0f),
40+ MG_V3(0.5f, 0.5f, 0.5f),
41+ MG_V3(0.0f, 1.0f, 0.0f)
42 );
43
44- model = HMM_M4D(1.0f);
45- view_model = HMM_MulM4(view, model);
46- mvp = HMM_MulM4(proj, view_model);
47+ model = MG_M4D(1.0f);
48+ view_model = MG_MulM4(view, model);
49+ mvp = MG_MulM4(proj, view_model);
50
51- renderer_mesh_draw(block, &mvp.Elements[0][0]);
52+ renderer_mesh_draw(block, &mvp);
53 }
54
55 void test_scene_destroy(void)
+21,
-2
1@@ -2,10 +2,13 @@ project(
2 'Magnolia',
3 'c',
4 default_options: [
5- 'c_std=c99',
6+ 'c_std=c89',
7 'warning_level=3',
8 ],
9-)
10+ )
11+
12+cc = meson.get_compiler('c')
13+math_dep = cc.find_library('m', required: false)
14
15 sources = [
16 'source/main.c',
17@@ -21,6 +24,21 @@ vendor_inc = [
18 include_directories('vendor'),
19 ]
20
21+mgmath_lib = static_library(
22+ 'mgmath',
23+ 'source/mgmath_handmade.c',
24+ include_directories: include_dirs + vendor_inc,
25+ dependencies: math_dep,
26+ c_args: [ '-Wno-pedantic' ],
27+ override_options: [ 'c_std=c99' ],
28+)
29+
30+mgmath_dep = declare_dependency(
31+ include_directories: include_dirs,
32+ link_with: mgmath_lib,
33+ dependencies: math_dep,
34+)
35+
36 opengl_dep = dependency('opengl', required: false)
37 if not opengl_dep.found() # fallback (at least for alpine)
38 opengl_dep = dependency('gl')
39@@ -56,6 +74,7 @@ renderer_dep = declare_dependency(
40
41 deps = [
42 dependency('sdl2'),
43+ mgmath_dep,
44 renderer_dep,
45 ]
46
+176,
-0
1@@ -0,0 +1,176 @@
2+#include <HandmadeMath.h>
3+
4+#include "mgmath.h"
5+
6+static HMM_Vec3 mg_to_hmm_v3(MGVec3 vec);
7+static HMM_Mat4 mg_to_hmm_m4(MGMat4 mat);
8+static MGVec2 mg_from_hmm_v2(HMM_Vec2 vec);
9+static MGVec3 mg_from_hmm_v3(HMM_Vec3 vec);
10+static MGVec4 mg_from_hmm_v4(HMM_Vec4 vec);
11+static MGMat4 mg_from_hmm_m4(HMM_Mat4 mat);
12+
13+static HMM_Vec3 mg_to_hmm_v3(MGVec3 vec)
14+{
15+ return HMM_V3(vec.x, vec.y, vec.z);
16+}
17+
18+static HMM_Mat4 mg_to_hmm_m4(MGMat4 mat)
19+{
20+ HMM_Mat4 result;
21+ int col;
22+ int row;
23+
24+ for (col = 0; col < 4; col++) {
25+ for (row = 0; row < 4; row++)
26+ result.Elements[col][row] = mat.Elements[col][row];
27+ }
28+
29+ return result;
30+}
31+
32+static MGVec2 mg_from_hmm_v2(HMM_Vec2 vec)
33+{
34+ MGVec2 result;
35+
36+ result.x = vec.X;
37+ result.y = vec.Y;
38+
39+ return result;
40+}
41+
42+static MGVec3 mg_from_hmm_v3(HMM_Vec3 vec)
43+{
44+ MGVec3 result;
45+
46+ result.x = vec.X;
47+ result.y = vec.Y;
48+ result.z = vec.Z;
49+
50+ return result;
51+}
52+
53+static MGVec4 mg_from_hmm_v4(HMM_Vec4 vec)
54+{
55+ MGVec4 result;
56+
57+ result.x = vec.X;
58+ result.y = vec.Y;
59+ result.z = vec.Z;
60+ result.w = vec.W;
61+
62+ return result;
63+}
64+
65+static MGMat4 mg_from_hmm_m4(HMM_Mat4 mat)
66+{
67+ MGMat4 result;
68+ int col;
69+ int row;
70+
71+ for (col = 0; col < 4; col++) {
72+ for (row = 0; row < 4; row++)
73+ result.Elements[col][row] = mat.Elements[col][row];
74+ }
75+
76+ return result;
77+}
78+
79+float MG_SinF(float angle)
80+{
81+ return HMM_SinF(angle);
82+}
83+
84+float MG_CosF(float angle)
85+{
86+ return HMM_CosF(angle);
87+}
88+
89+float MG_TanF(float angle)
90+{
91+ return HMM_TanF(angle);
92+}
93+
94+float MG_SqrtF(float value)
95+{
96+ return HMM_SqrtF(value);
97+}
98+
99+MGVec2 MG_V2(float x, float y)
100+{
101+ return mg_from_hmm_v2(HMM_V2(x, y));
102+}
103+
104+MGVec3 MG_V3(float x, float y, float z)
105+{
106+ return mg_from_hmm_v3(HMM_V3(x, y, z));
107+}
108+
109+MGVec4 MG_V4(float x, float y, float z, float w)
110+{
111+ return mg_from_hmm_v4(HMM_V4(x, y, z, w));
112+}
113+
114+MGVec3 MG_AddV3(MGVec3 left, MGVec3 right)
115+{
116+ return mg_from_hmm_v3(HMM_AddV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
117+}
118+
119+MGVec3 MG_SubV3(MGVec3 left, MGVec3 right)
120+{
121+ return mg_from_hmm_v3(HMM_SubV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
122+}
123+
124+MGVec3 MG_MulV3F(MGVec3 left, float right)
125+{
126+ return mg_from_hmm_v3(HMM_MulV3F(mg_to_hmm_v3(left), right));
127+}
128+
129+float MG_DotV3(MGVec3 left, MGVec3 right)
130+{
131+ return HMM_DotV3(mg_to_hmm_v3(left), mg_to_hmm_v3(right));
132+}
133+
134+MGVec3 MG_Cross(MGVec3 left, MGVec3 right)
135+{
136+ return mg_from_hmm_v3(HMM_Cross(mg_to_hmm_v3(left), mg_to_hmm_v3(right)));
137+}
138+
139+MGVec3 MG_NormV3(MGVec3 vec)
140+{
141+ return mg_from_hmm_v3(HMM_NormV3(mg_to_hmm_v3(vec)));
142+}
143+
144+MGMat4 MG_M4(void)
145+{
146+ return mg_from_hmm_m4(HMM_M4());
147+}
148+
149+MGMat4 MG_M4D(float diagonal)
150+{
151+ return mg_from_hmm_m4(HMM_M4D(diagonal));
152+}
153+
154+MGMat4 MG_MulM4(MGMat4 left, MGMat4 right)
155+{
156+ return mg_from_hmm_m4(HMM_MulM4(mg_to_hmm_m4(left), mg_to_hmm_m4(right)));
157+}
158+
159+MGMat4 MG_Translate(MGVec3 translation)
160+{
161+ return mg_from_hmm_m4(HMM_Translate(mg_to_hmm_v3(translation)));
162+}
163+
164+MGMat4 MG_Perspective_RH_NO(float fov_y, float aspect, float near_z, float far_z)
165+{
166+ return mg_from_hmm_m4(HMM_Perspective_RH_NO(fov_y, aspect, near_z, far_z));
167+}
168+
169+MGMat4 MG_LookAt_RH(MGVec3 eye, MGVec3 center, MGVec3 up)
170+{
171+ return mg_from_hmm_m4(HMM_LookAt_RH(
172+ mg_to_hmm_v3(eye),
173+ mg_to_hmm_v3(center),
174+ mg_to_hmm_v3(up)
175+ ));
176+}
177+
+2,
-2
1@@ -251,7 +251,7 @@ MGMesh renderer_mesh_create(const MGMeshDesc* desc)
2 return id;
3 }
4
5-void renderer_mesh_draw(MGMesh id, const float mvp[16])
6+void renderer_mesh_draw(MGMesh id, const MGMat4* mvp)
7 {
8 RendererMesh* mesh;
9 sg_bindings bindings = {0};
10@@ -270,7 +270,7 @@ void renderer_mesh_draw(MGMesh id, const float mvp[16])
11 bindings.vertex_buffers[0] = mesh->vb;
12 bindings.index_buffer = mesh->ib;
13
14- uniforms.ptr = mvp;
15+ uniforms.ptr = &mvp->Elements[0][0];
16 uniforms.size = sizeof(float) * 16;
17
18 sg_apply_pipeline(ren.pipeline);