From 6d96b0e7ea1d98e44647d0bbbba232701a7d557e Mon Sep 17 00:00:00 2001
From: sanine <sanine.not@pm.me>
Date: Tue, 4 Oct 2022 14:53:08 -0500
Subject: add first person demo

---
 src/glm/camera.c |  15 ++++++++
 src/glm/glm.h    |   5 +++
 src/glm/mat4.c   |  30 ++++++++--------
 src/glm/vec3.c   | 103 +++++++++++++++++++++++++++++++++++++++++++++++++------
 4 files changed, 129 insertions(+), 24 deletions(-)

(limited to 'src/glm')

diff --git a/src/glm/camera.c b/src/glm/camera.c
index 7180b77..11d309d 100644
--- a/src/glm/camera.c
+++ b/src/glm/camera.c
@@ -2,15 +2,18 @@
 #include <honeysuckle.h>
 #include <cglm/cglm.h>
 #include "util/util.h"
+#include "glm.h"
 
 
 int perspective(lua_State *L);
+int look(lua_State *L);
 
 
 void setup_camera(lua_State *L, int glm_tbl)
 {
 	int tbl = hs_create_table(L,
 		hs_str_cfunc("perspective", perspective),
+		hs_str_cfunc("look", look),
 	);
 
 	append_table(L, glm_tbl, tbl);
@@ -28,3 +31,15 @@ int perspective(lua_State *L)
 	glm_perspective(fov, aspect, near, far, *dest);
 	return 0;
 }
+
+
+int look(lua_State *L)
+{
+	float *eye = luaL_checkudata(L, 1, vec3_tname);
+	float *dir = luaL_checkudata(L, 2, vec3_tname);
+	float *up  = luaL_checkudata(L, 3, vec3_tname);
+	void *dest  = luaL_checkudata(L, 4, mat4_tname);
+
+	glm_look(eye, dir, up, dest);
+	return 0;
+}
diff --git a/src/glm/glm.h b/src/glm/glm.h
index 0667179..8bcf7cc 100644
--- a/src/glm/glm.h
+++ b/src/glm/glm.h
@@ -3,6 +3,11 @@
 
 #include <lua.h>
 
+
+extern const char *vec3_tname;
+extern const char *mat4_tname;
+
+
 void setup_glm(lua_State *L, int honey_index);
 void setup_vec3(lua_State *L, int glm_tbl);
 void setup_mat4(lua_State *L, int glm_tbl);
diff --git a/src/glm/mat4.c b/src/glm/mat4.c
index 02ef047..5197891 100644
--- a/src/glm/mat4.c
+++ b/src/glm/mat4.c
@@ -9,8 +9,13 @@ int mat4_set(lua_State *L);
 int mat4_get(lua_State *L);
 int mat4_identity(lua_State *L);
 
+const char *mat4_tname = "glm.mat4";
+
 void setup_mat4(lua_State *L, int glm_tbl)
 {
+	luaL_newmetatable(L, mat4_tname);
+	lua_pop(L, 1);
+
 	int tbl = hs_create_table(L,
 		hs_str_cfunc("mat4", mat4_create),
 		hs_str_cfunc("mat4_set", mat4_set),
@@ -26,37 +31,34 @@ void setup_mat4(lua_State *L, int glm_tbl)
 int mat4_create(lua_State *L)
 {
 	lua_newuserdata(L, 16*sizeof(float));
+	luaL_getmetatable(L, mat4_tname);
+	lua_setmetatable(L, -2);
 	return 1;
 }
 
 
 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);
+	float *matrix = luaL_checkudata(L, 1, mat4_tname);
+	int index = luaL_checkinteger(L, 2);
+	float value = luaL_checknumber(L, 3);
+	array_set(L, 16, matrix, 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);
+	float *matrix = luaL_checkudata(L, 1, mat4_tname);
+	int index = luaL_checkinteger(L, 2);
+	array_get(L, 16, matrix, index);
 	return 1;
 }
 
 
 int mat4_identity(lua_State *L)
 {
-	void *ptr;
-	hs_parse_args(L, hs_user(ptr));
-	mat4 *m = ptr;
-
-	glm_mat4_identity(*m);
+	void *m = luaL_checkudata(L, 1, mat4_tname);
+	glm_mat4_identity(m);
 	return 0;
 }
diff --git a/src/glm/vec3.c b/src/glm/vec3.c
index 483f5bd..f278471 100644
--- a/src/glm/vec3.c
+++ b/src/glm/vec3.c
@@ -8,12 +8,34 @@ int vec3_create(lua_State *L);
 int vec3_set(lua_State *L);
 int vec3_get(lua_State *L);
 
+int vec3_normalize(lua_State *L);
+
+int vec3_add(lua_State *L);
+int vec3_sub(lua_State *L);
+int vec3_scale(lua_State *L);
+int vec3_dot(lua_State *L);
+int vec3_cross(lua_State *L);
+
+const char *vec3_tname = "glm.vec3";
+
+
 void setup_vec3(lua_State *L, int glm_tbl)
 {
+	luaL_newmetatable(L, vec3_tname);
+	lua_pop(L, 1);
+
 	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),
+
+		hs_str_cfunc("vec3_normalize", vec3_normalize),
+
+		hs_str_cfunc("vec3_add", vec3_add),
+		hs_str_cfunc("vec3_sub", vec3_sub),
+		hs_str_cfunc("vec3_scale", vec3_scale),
+		hs_str_cfunc("vec3_dot", vec3_dot),
+		hs_str_cfunc("vec3_cross", vec3_cross),
 	);
 
 	append_table(L, glm_tbl, tbl);
@@ -24,27 +46,88 @@ void setup_vec3(lua_State *L, int glm_tbl)
 int vec3_create(lua_State *L)
 {
 	lua_newuserdata(L, 3*sizeof(float));
+	luaL_getmetatable(L, vec3_tname);
+	lua_setmetatable(L, -2);
 	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);
+	float *vec = luaL_checkudata(L, 1, vec3_tname);
+	int index = luaL_checkinteger(L, 2);
+	double value = luaL_checknumber(L, 3);
+	array_set(L, 3, vec, 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);
+	float *vec = luaL_checkudata(L, 1, vec3_tname);
+	int index = luaL_checkinteger(L, 2);
+	array_get(L, 3, vec, index);
+	return 1;
+}
+
+
+int vec3_normalize(lua_State *L)
+{
+	float *vec = luaL_checkudata(L, 1, vec3_tname);
+	glm_vec3_normalize(vec);
+	return 0;
+}
+
+
+int vec3_add(lua_State *L)
+{
+	float *a = luaL_checkudata(L, 1, vec3_tname);
+	float *b = luaL_checkudata(L, 2, vec3_tname);
+	float *dest = luaL_checkudata(L, 3, vec3_tname);
+
+	glm_vec3_add(a, b, dest);
+	return 0;
+}
+
+
+int vec3_sub(lua_State *L)
+{
+	float *a = luaL_checkudata(L, 1, vec3_tname);
+	float *b = luaL_checkudata(L, 2, vec3_tname);
+	float *dest = luaL_checkudata(L, 3, vec3_tname);
+
+	glm_vec3_sub(a, b, dest);
+	return 0;
+}
+
+int vec3_scale(lua_State *L)
+{
+	float *v = luaL_checkudata(L, 1, vec3_tname);
+	float s = luaL_checknumber(L, 2);
+	float *dest = luaL_checkudata(L, 3, vec3_tname);
+
+	glm_vec3_scale(v, s, dest);
+	return 0;
+}
+
+
+int vec3_dot(lua_State *L)
+{
+	float *a = luaL_checkudata(L, 1, vec3_tname);
+	float *b = luaL_checkudata(L, 2, vec3_tname);
+
+	float result = glm_vec3_dot(a, b);
+	lua_pushnumber(L, result);
 	return 1;
 }
+
+
+int vec3_cross(lua_State *L)
+{
+	float *a = luaL_checkudata(L, 1, vec3_tname);
+	float *b = luaL_checkudata(L, 2, vec3_tname);
+	float *dest = luaL_checkudata(L, 3, vec3_tname);
+
+	glm_vec3_cross(a, b, dest);
+	return 0;
+}
-- 
cgit v1.2.1