diff options
author | sanine <sanine.not@pm.me> | 2022-10-04 14:53:08 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-10-04 14:53:08 -0500 |
commit | 6d96b0e7ea1d98e44647d0bbbba232701a7d557e (patch) | |
tree | 15fa9d310f1db5767b662fb0bb4cad8a560d3650 /src | |
parent | 0c55c0e1b48de55fea401ea1b2b3f2280ef0f93c (diff) |
add first person demo
Diffstat (limited to 'src')
-rw-r--r-- | src/gl/window.c | 48 | ||||
-rw-r--r-- | src/glm/camera.c | 15 | ||||
-rw-r--r-- | src/glm/glm.h | 5 | ||||
-rw-r--r-- | src/glm/mat4.c | 30 | ||||
-rw-r--r-- | src/glm/vec3.c | 103 |
5 files changed, 176 insertions, 25 deletions
diff --git a/src/gl/window.c b/src/gl/window.c index b088b86..5ec6fc1 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -24,11 +24,14 @@ int window_destroy(lua_State *L); int window_make_context_current(lua_State *L); int window_set_hint(lua_State *L); int window_should_close(lua_State *L); +int window_set_should_close(lua_State *L); int window_poll_events(lua_State *L); int window_swap_buffers(lua_State *L); int window_set_framebuffer_size_callback(lua_State *L); int window_get_time(lua_State *L); int window_get_key(lua_State *L); +int window_get_cursor_pos(lua_State *L); +int window_set_input_mode(lua_State *L); static const char *window_tname = "window"; @@ -55,15 +58,26 @@ void setup_window(lua_State *L, int honey_index) hs_str_cfunc("makeContextCurrent", window_make_context_current), hs_str_cfunc("setHint", window_set_hint), hs_str_cfunc("shouldClose", window_should_close), + hs_str_cfunc("setShouldClose", window_set_should_close), hs_str_cfunc("pollEvents", window_poll_events), hs_str_cfunc("swapBuffers", window_swap_buffers), hs_str_cfunc("setFramebufferSizeCallback", window_set_framebuffer_size_callback), hs_str_cfunc("getTime", window_get_time), hs_str_cfunc("getKey", window_get_key), + hs_str_cfunc("getCursorPos", window_get_cursor_pos), + hs_str_cfunc("setInputMode", window_set_input_mode), hs_str_tbl("hintType", hint_types), hs_str_tbl("profileType", profile_types), + /* input modes */ + hs_str_int("CURSOR", GLFW_CURSOR), + + /* cursor modes */ + hs_str_int("CURSOR_NORMAL", GLFW_CURSOR_NORMAL), + hs_str_int("CURSOR_HIDDEN", GLFW_CURSOR_HIDDEN), + hs_str_int("CURSOR_DISABLED", GLFW_CURSOR_DISABLED), + /* key states */ hs_str_int("PRESS", GLFW_PRESS), hs_str_int("RELEASE", GLFW_RELEASE), @@ -205,7 +219,7 @@ static void framebuffer_size_callback_(GLFWwindow *win, int width, int height) lua_pushlightuserdata(wdata->L, win); lua_pushinteger(wdata->L, width); lua_pushinteger(wdata->L, height); - hs_call(wdata->L, 3, 0); + lua_call(wdata->L, 3, 0); } } @@ -269,6 +283,16 @@ int window_should_close(lua_State *L) } +int window_set_should_close(lua_State *L) +{ + GLFWwindow **win = luaL_checkudata(L, 1, window_tname); + int value = lua_toboolean(L, 2); + + glfwSetWindowShouldClose(*win, value); + return 0; +} + + int window_poll_events(lua_State *L) { glfwPollEvents(); @@ -312,3 +336,25 @@ int window_get_key(lua_State *L) lua_pushinteger(L, glfwGetKey(*win, key)); return 1; } + + +int window_get_cursor_pos(lua_State *L) +{ + GLFWwindow **win = luaL_checkudata(L, 1, window_tname); + double x, y; + glfwGetCursorPos(*win, &x, &y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; +} + + +int window_set_input_mode(lua_State *L) +{ + GLFWwindow **win = luaL_checkudata(L, 1, window_tname); + int mode = luaL_checkinteger(L, 2); + int value = luaL_checkinteger(L, 3); + + glfwSetInputMode(*win, mode, value); + return 0; +} 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; +} |