From f0a6a17e4fba3510c8ba8f6b114012873035a4a1 Mon Sep 17 00:00:00 2001
From: sanine-a <sanine.not@pm.me>
Date: Tue, 27 Oct 2020 23:23:22 -0500
Subject: fix minor bugs, add cube primitive binding, and implement basic
 first-person camera

---
 src/cglm_bindings.c         | 34 +++++++++++++++++++++++++++++++---
 src/cglm_bindings.h         | 20 ++++++++++++++++++++
 src/primitives/primitives.c | 19 ++++++++++++++++++-
 3 files changed, 69 insertions(+), 4 deletions(-)

(limited to 'src')

diff --git a/src/cglm_bindings.c b/src/cglm_bindings.c
index f8f6c8a..947c394 100644
--- a/src/cglm_bindings.c
+++ b/src/cglm_bindings.c
@@ -8,6 +8,7 @@ void honey_setup_cglm(lua_State* L)
         { "norm",      HONEY_FUNCTION, { .function = honey_cglm_vec3_norm } },
         { "add",       HONEY_FUNCTION, { .function = honey_cglm_vec3_add } },
         { "adds",      HONEY_FUNCTION, { .function = honey_cglm_vec3_adds } },
+        { "sub",       HONEY_FUNCTION, { .function = honey_cglm_vec3_sub } },
         { "mul",       HONEY_FUNCTION, { .function = honey_cglm_vec3_mul } },
         { "muls",      HONEY_FUNCTION, { .function = honey_cglm_vec3_muls } },
         { "normalize", HONEY_FUNCTION, { .function = honey_cglm_vec3_normalize } },
@@ -21,6 +22,7 @@ void honey_setup_cglm(lua_State* L)
         { "norm",      HONEY_FUNCTION, { .function = honey_cglm_vec4_norm } },
         { "add",       HONEY_FUNCTION, { .function = honey_cglm_vec4_add } },
         { "adds",      HONEY_FUNCTION, { .function = honey_cglm_vec4_adds } },
+        { "sub",       HONEY_FUNCTION, { .function = honey_cglm_vec4_sub } },
         { "mul",       HONEY_FUNCTION, { .function = honey_cglm_vec4_mul } },
         { "muls",      HONEY_FUNCTION, { .function = honey_cglm_vec4_muls } },
         { "normalize", HONEY_FUNCTION, { .function = honey_cglm_vec4_normalize } },
@@ -69,15 +71,15 @@ void honey_setup_cglm(lua_State* L)
         { "set_value",      HONEY_FUNCTION, { .function = honey_cglm_array_set_value } },
         { "get_value",      HONEY_FUNCTION, { .function = honey_cglm_array_get_value } },
         { "copy_array",     HONEY_FUNCTION, { .function = honey_cglm_array_copy } },
-        { "vec3",           HONEY_TABLE, { .table = { 10, vec3_elements } } },
-        { "vec4",           HONEY_TABLE, { .table = { 10, vec4_elements } } },
+        { "vec3",           HONEY_TABLE, { .table = { 11, vec3_elements } } },
+        { "vec4",           HONEY_TABLE, { .table = { 11, vec4_elements } } },
         { "mat3",           HONEY_TABLE, { .table = {  8, mat3_elements } } },
         { "mat4",           HONEY_TABLE, { .table = { 10, mat4_elements } } },
         { "affine",         HONEY_TABLE, { .table = { 3, affine_elements } } },
         { "camera",         HONEY_TABLE, { .table = { 3, camera_elements } } },
     };
 
-    honey_lua_create_table(L, cglm_elements, 9);
+    honey_lua_create_table(L, cglm_elements, 10);
 }
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
@@ -202,6 +204,19 @@ int honey_cglm_vec3_add(lua_State* L)
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
 
+int honey_cglm_vec3_sub(lua_State* L)
+{
+    float* a, *b, *dest;
+    honey_lua_parse_arguments(L, 3,
+                              HONEY_USERDATA, &a,
+                              HONEY_USERDATA, &b,
+                              HONEY_USERDATA, &dest);
+    glm_vec3_sub(a, b, dest);
+    return 0;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
 int honey_cglm_vec3_adds(lua_State* L)
 {
     float a, *v, *dest;
@@ -359,6 +374,19 @@ int honey_cglm_vec4_adds(lua_State* L)
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
 
+int honey_cglm_vec4_sub(lua_State* L)
+{
+    float* a, *b, *dest;
+    honey_lua_parse_arguments(L, 3,
+                              HONEY_USERDATA, &a,
+                              HONEY_USERDATA, &b,
+                              HONEY_USERDATA, &dest);
+    glm_vec4_sub(a, b, dest);
+    return 0;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
 int honey_cglm_vec4_mul(lua_State* L)
 {
     float *a, *b, *dest;
diff --git a/src/cglm_bindings.h b/src/cglm_bindings.h
index ba7714e..61554e3 100644
--- a/src/cglm_bindings.h
+++ b/src/cglm_bindings.h
@@ -102,6 +102,16 @@ int honey_cglm_vec3_norm(lua_State* L);
  */
 int honey_cglm_vec3_add(lua_State* L);
 
+/** @brief Subtract one vector from another.
+ *
+ * @param[in] a The first vector.
+ * @param[in] b The second vector.
+ * @param[out] dest Vector to fill with a - b
+ *
+ * @returns Nothing.
+ */
+int honey_cglm_vec3_sub(lua_State* L);
+
 /** @brief Add a scalar to a vector.
  *
  * @param[in] a The scalar.
@@ -215,6 +225,16 @@ int honey_cglm_vec4_add(lua_State* L);
  */
 int honey_cglm_vec4_adds(lua_State* L);
 
+/** @brief Subtract one vector from another.
+ *
+ * @param[in] a The first vector.
+ * @param[in] b The second vector.
+ * @param[out] dest vec4 to fill with a - b.
+ *
+ * @returns Nothing.
+ */
+int honey_cglm_vec4_sub(lua_State* L);
+
 /** @param Component-wise multiply two vectors together.
  *
  * @param[in] a The first vector.
diff --git a/src/primitives/primitives.c b/src/primitives/primitives.c
index 1bfbe14..8293d4d 100644
--- a/src/primitives/primitives.c
+++ b/src/primitives/primitives.c
@@ -15,13 +15,30 @@ static int honey_mesh_lua_plane(lua_State* L)
     return 1;
 }
 
+static int honey_mesh_lua_cube(lua_State* L)
+{
+    float width, height, depth;
+    honey_lua_parse_arguments(L, 3,
+                              HONEY_NUMBER, &width,
+                              HONEY_NUMBER, &height,
+                              HONEY_NUMBER, &depth);
+
+    honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh));
+    if (honey_mesh_new_textured_cube(mesh, width, height, depth) != HONEY_OK) {
+        lua_pushstring(L, "error encountered while building plane");
+        lua_error(L);
+    }
+    return 1;
+}
+
 void honey_setup_primitives(lua_State* L)
 {
     honey_lua_element primitive_elements[] = {
         { "plane", HONEY_FUNCTION, { .function = honey_mesh_lua_plane } },
+        { "cube", HONEY_FUNCTION, { .function = honey_mesh_lua_cube } },
     };
 
-    honey_lua_create_table(L, primitive_elements, 1);
+    honey_lua_create_table(L, primitive_elements, 2);
 }
 
 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-- 
cgit v1.2.1