summaryrefslogtreecommitdiff
path: root/src/glm
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-08-23 22:16:24 -0500
committersanine <sanine.not@pm.me>2022-08-23 22:16:24 -0500
commit709e1b6e1ce86f8da4fc136747fcefbc6c6057bd (patch)
tree8f4da8c27526fb056a340a9d2948fdace8341ece /src/glm
parent3afbf2a13b2dada445fb667bf25600407fea480a (diff)
add basic 3d perspective
Diffstat (limited to 'src/glm')
-rw-r--r--src/glm/CMakeLists.txt2
-rw-r--r--src/glm/camera.c30
-rw-r--r--src/glm/glm.c26
-rw-r--r--src/glm/glm.h9
-rw-r--r--src/glm/mat4.c26
-rw-r--r--src/glm/transform.c29
-rw-r--r--src/glm/vec3.c50
7 files changed, 167 insertions, 5 deletions
diff --git a/src/glm/CMakeLists.txt b/src/glm/CMakeLists.txt
index 962dbbd..3f0dada 100644
--- a/src/glm/CMakeLists.txt
+++ b/src/glm/CMakeLists.txt
@@ -2,6 +2,8 @@ project(honey_engine)
target_sources(honey PUBLIC
${CMAKE_CURRENT_LIST_DIR}/glm.c
+ ${CMAKE_CURRENT_LIST_DIR}/vec3.c
${CMAKE_CURRENT_LIST_DIR}/mat4.c
${CMAKE_CURRENT_LIST_DIR}/transform.c
+ ${CMAKE_CURRENT_LIST_DIR}/camera.c
)
diff --git a/src/glm/camera.c b/src/glm/camera.c
new file mode 100644
index 0000000..7180b77
--- /dev/null
+++ b/src/glm/camera.c
@@ -0,0 +1,30 @@
+#include <lua.h>
+#include <honeysuckle.h>
+#include <cglm/cglm.h>
+#include "util/util.h"
+
+
+int perspective(lua_State *L);
+
+
+void setup_camera(lua_State *L, int glm_tbl)
+{
+ int tbl = hs_create_table(L,
+ hs_str_cfunc("perspective", perspective),
+ );
+
+ append_table(L, glm_tbl, tbl);
+ lua_pop(L, 1);
+}
+
+
+int perspective(lua_State *L)
+{
+ lua_Number fov, aspect, near, far;
+ void *dest_ptr;
+ hs_parse_args(L, hs_num(fov), hs_num(aspect), hs_num(near), hs_num(far), hs_user(dest_ptr));
+ mat4 *dest = dest_ptr;
+
+ glm_perspective(fov, aspect, near, far, *dest);
+ return 0;
+}
diff --git a/src/glm/glm.c b/src/glm/glm.c
index 786fcf0..525a029 100644
--- a/src/glm/glm.c
+++ b/src/glm/glm.c
@@ -5,9 +5,29 @@
void setup_glm(lua_State *L, int honey_index)
{
lua_createtable(L, 0, 1);
- int glm_index = lua_gettop(L);
+ int glm_tbl = lua_gettop(L);
+
+ setup_vec3(L, glm_tbl);
+ setup_mat4(L, glm_tbl);
+
+ setup_transform(L, glm_tbl);
+ setup_camera(L, glm_tbl);
- setup_mat4(L, glm_index);
- setup_transform(L, glm_index);
lua_setfield(L, honey_index, "glm");
}
+
+
+void array_get(lua_State *L, int max, float *array, int index)
+{
+ if (index < 0 || index >= max)
+ hs_throw_error(L, "index %d is out of range [0-%d]", index, max-1);
+ lua_pushnumber(L, array[index]);
+}
+
+
+void array_set(lua_State *L, int max, float *array, int index, float value)
+{
+ if (index < 0 || index >= max)
+ hs_throw_error(L, "index %d is out of range [0-%d]", index, max-1);
+ array[index] = value;
+}
diff --git a/src/glm/glm.h b/src/glm/glm.h
index 2bf803b..0667179 100644
--- a/src/glm/glm.h
+++ b/src/glm/glm.h
@@ -4,7 +4,12 @@
#include <lua.h>
void setup_glm(lua_State *L, int honey_index);
-void setup_mat4(lua_State *L, int glm_index);
-void setup_transform(lua_State *L, int glm_index);
+void setup_vec3(lua_State *L, int glm_tbl);
+void setup_mat4(lua_State *L, int glm_tbl);
+void setup_transform(lua_State *L, int glm_tbl);
+void setup_camera(lua_State *L, int glm_tbl);
+
+void array_get(lua_State *L, int max, float *array, int index);
+void array_set(lua_State *L, int max, float *array, int index, float value);
#endif
diff --git a/src/glm/mat4.c b/src/glm/mat4.c
index f98cdfb..02ef047 100644
--- a/src/glm/mat4.c
+++ b/src/glm/mat4.c
@@ -2,14 +2,19 @@
#include <honeysuckle.h>
#include <cglm/cglm.h>
#include "util/util.h"
+#include "glm.h"
int mat4_create(lua_State *L);
+int mat4_set(lua_State *L);
+int mat4_get(lua_State *L);
int mat4_identity(lua_State *L);
void setup_mat4(lua_State *L, int glm_tbl)
{
int tbl = hs_create_table(L,
hs_str_cfunc("mat4", mat4_create),
+ hs_str_cfunc("mat4_set", mat4_set),
+ hs_str_cfunc("mat4_get", mat4_get),
hs_str_cfunc("mat4_identity", mat4_identity),
);
@@ -25,6 +30,27 @@ int mat4_create(lua_State *L)
}
+int mat4_set(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ lua_Number value;
+ hs_parse_args(L, hs_user(ptr), hs_int(index), hs_num(value));
+ array_set(L, 16, ptr, index, value);
+ return 0;
+}
+
+
+int mat4_get(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ hs_parse_args(L, hs_user(ptr), hs_int(index));
+ array_get(L, 16, ptr, index);
+ return 1;
+}
+
+
int mat4_identity(lua_State *L)
{
void *ptr;
diff --git a/src/glm/transform.c b/src/glm/transform.c
index 4dce595..daf75a5 100644
--- a/src/glm/transform.c
+++ b/src/glm/transform.c
@@ -4,12 +4,16 @@
#include "util/util.h"
+int translate(lua_State *L);
+int rotate(lua_State *L);
int rotate_z(lua_State *L);
void setup_transform(lua_State *L, int glm_tbl)
{
int tbl = hs_create_table(L,
+ hs_str_cfunc("translate", translate),
+ hs_str_cfunc("rotate", rotate),
hs_str_cfunc("rotate_z", rotate_z),
);
@@ -18,6 +22,31 @@ void setup_transform(lua_State *L, int glm_tbl)
}
+int translate(lua_State *L)
+{
+ void *m_ptr, *v_ptr;
+ hs_parse_args(L, hs_user(m_ptr), hs_user(v_ptr));
+ mat4 *m = m_ptr;
+ vec3 *v = v_ptr;
+
+ glm_translate(*m, *v);
+ return 0;
+}
+
+
+int rotate(lua_State *L)
+{
+ void *m_ptr, *axis_ptr;
+ lua_Number angle;
+ hs_parse_args(L, hs_user(m_ptr), hs_num(angle), hs_user(axis_ptr));
+ mat4 *m = m_ptr;
+ vec3 *axis = axis_ptr;
+
+ glm_rotate(*m, angle, *axis);
+ return 0;
+}
+
+
int rotate_z(lua_State *L)
{
void *src_ptr, *dest_ptr;
diff --git a/src/glm/vec3.c b/src/glm/vec3.c
new file mode 100644
index 0000000..483f5bd
--- /dev/null
+++ b/src/glm/vec3.c
@@ -0,0 +1,50 @@
+#include <lua.h>
+#include <honeysuckle.h>
+#include <cglm/cglm.h>
+#include "util/util.h"
+#include "glm.h"
+
+int vec3_create(lua_State *L);
+int vec3_set(lua_State *L);
+int vec3_get(lua_State *L);
+
+void setup_vec3(lua_State *L, int glm_tbl)
+{
+ int tbl = hs_create_table(L,
+ hs_str_cfunc("vec3", vec3_create),
+ hs_str_cfunc("vec3_set", vec3_set),
+ hs_str_cfunc("vec3_get", vec3_get),
+ );
+
+ append_table(L, glm_tbl, tbl);
+ lua_pop(L, 1);
+}
+
+
+int vec3_create(lua_State *L)
+{
+ lua_newuserdata(L, 3*sizeof(float));
+ return 1;
+}
+
+
+int vec3_set(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ lua_Number value;
+ hs_parse_args(L, hs_user(ptr), hs_int(index), hs_num(value));
+ array_set(L, 3, ptr, index, value);
+ return 0;
+}
+
+
+int vec3_get(lua_State *L)
+{
+ void *ptr;
+ lua_Integer index;
+ lua_Number value;
+ hs_parse_args(L, hs_user(ptr), hs_int(index));
+ array_get(L, 3, ptr, index);
+ return 1;
+}