From ae4e17fc743ca0344af818ab767db7311ea7829c Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 23 Aug 2022 13:28:01 -0500 Subject: add basic transforms --- CMakeLists.txt | 11 +++++++---- demo/gl-window | 0 demo/honey.lua | 12 +++++++++++- src/gl/shader.c | 19 ++++++++++++++++++- src/glm/CMakeLists.txt | 7 +++++++ src/glm/glm.c | 13 +++++++++++++ src/glm/glm.h | 10 ++++++++++ src/glm/mat4.c | 36 ++++++++++++++++++++++++++++++++++++ src/glm/transform.c | 31 +++++++++++++++++++++++++++++++ src/main.c | 2 ++ 10 files changed, 135 insertions(+), 6 deletions(-) delete mode 100644 demo/gl-window create mode 100644 src/glm/CMakeLists.txt create mode 100644 src/glm/glm.c create mode 100644 src/glm/glm.h create mode 100644 src/glm/mat4.c create mode 100644 src/glm/transform.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 17bf14d..ea8fca3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,17 +16,19 @@ include_directories( ${CMAKE_SOURCE_DIR}/src ${LIB_ROOT}/honeysuckle/src ${LIB_ROOT}/glfw-3.3.8/include + ${LIB_ROOT}/cglm/include ) +# disable byte-alignment in cglm +add_definitions(-DCGLM_ALL_UNALIGNED) + # link to third-party included libraries link_directories(${LIB_ROOT}/assimp/lib) link_directories(${LIB_ROOT}/honeysuckle) link_directories(${LIB_ROOT}/glfw-3.3.8/src) -# add_library(glad ${SRC_ROOT}/gl/glad/glad.c) -# add_library(stb_image src/stb_image/stb_image.c) add_subdirectory(${LIB_ROOT}/lua-5.1.5) -add_subdirectory(${LIB_ROOT}/assimp) +#add_subdirectory(${LIB_ROOT}/assimp) add_subdirectory(${LIB_ROOT}/honeysuckle) add_subdirectory(${LIB_ROOT}/cglm) add_subdirectory(${LIB_ROOT}/glfw-3.3.8) @@ -35,7 +37,7 @@ add_subdirectory(${LIB_ROOT}/glfw-3.3.8) set(HONEY_SOURCE ${SRC_ROOT}/main.c) add_executable(honey ${HONEY_SOURCE}) -set(LIBRARIES lua5.1 honeysuckle assimp glfw) +set(LIBRARIES lua5.1 honeysuckle glfw) if (WIN32) set(LIBRARIES ${LIBRARIES} opengl32) else() @@ -61,3 +63,4 @@ add_subdirectory(${SRC_ROOT}/logging) add_subdirectory(${SRC_ROOT}/image) add_subdirectory(${SRC_ROOT}/util) add_subdirectory(${SRC_ROOT}/test) +add_subdirectory(${SRC_ROOT}/glm) diff --git a/demo/gl-window b/demo/gl-window deleted file mode 100644 index e69de29..0000000 diff --git a/demo/honey.lua b/demo/honey.lua index 3388b52..822877f 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -30,12 +30,14 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; layout (location = 2) in vec2 aTexCoord; +uniform mat4 transform; + out vec3 ourColor; out vec2 TexCoord; void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = transform * vec4(aPos, 1.0); ourColor = aColor; TexCoord = aTexCoord; } @@ -144,6 +146,8 @@ gl.Uniform1i(gl.GetUniformLocation(shader, 'ourTexture'), 0) --====== main loop ======-- +local transform = honey.glm.mat4() + while not window.shouldClose(w) do gl.ClearColor(0.2, 0.3, 0.3, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT) @@ -152,6 +156,12 @@ while not window.shouldClose(w) do gl.BindTexture(gl.TEXTURE_2D, texture) gl.UseProgram(shader) + honey.glm.mat4_identity(transform) + local time = window.getTime() + honey.glm.rotate_z(transform, time, transform) + local transformLocation = gl.GetUniformLocation(shader, 'transform') + gl.UniformMatrix4fv(transformLocation, false, transform) + gl.BindVertexArray(vertexArray) gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0) diff --git a/src/gl/shader.c b/src/gl/shader.c index b4d03e6..3732aff 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -18,6 +18,8 @@ int gl_uniform_get_location(lua_State *L); int gl_uniform_1i(lua_State *L); int gl_uniform_4f(lua_State *L); +int gl_uniform_matrix_4fv(lua_State *L); + void setup_shader(lua_State *L, int gl_index) { @@ -37,6 +39,8 @@ void setup_shader(lua_State *L, int gl_index) hs_str_cfunc("Uniform1i", gl_uniform_1i), hs_str_cfunc("Uniform4f", gl_uniform_4f), + hs_str_cfunc("UniformMatrix4fv", gl_uniform_matrix_4fv), + /******** enums ********/ /* shader types */ hs_str_int("VERTEX_SHADER", GL_VERTEX_SHADER), @@ -63,7 +67,7 @@ int gl_shader_set_source(lua_State *L) lua_Integer shader; char *code; hs_parse_args(L, hs_int(shader), hs_str(code)); - glShaderSource(shader, 1, &code, NULL); + glShaderSource(shader, 1, (const GLchar * const*)&code, NULL); return 0; } @@ -161,3 +165,16 @@ int gl_uniform_4f(lua_State *L) glUniform4f(location, v0, v1, v2, v3); return 0; } + + +int gl_uniform_matrix_4fv(lua_State *L) +{ + lua_Integer location; + bool transpose; + void *ptr; + hs_parse_args(L, hs_int(location), hs_bool(transpose), hs_user(ptr)); + + float *value = ptr; + glUniformMatrix4fv(location, 1, transpose, value); + return 0; +} diff --git a/src/glm/CMakeLists.txt b/src/glm/CMakeLists.txt new file mode 100644 index 0000000..962dbbd --- /dev/null +++ b/src/glm/CMakeLists.txt @@ -0,0 +1,7 @@ +project(honey_engine) + +target_sources(honey PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/glm.c + ${CMAKE_CURRENT_LIST_DIR}/mat4.c + ${CMAKE_CURRENT_LIST_DIR}/transform.c +) diff --git a/src/glm/glm.c b/src/glm/glm.c new file mode 100644 index 0000000..786fcf0 --- /dev/null +++ b/src/glm/glm.c @@ -0,0 +1,13 @@ +#include +#include + + +void setup_glm(lua_State *L, int honey_index) +{ + lua_createtable(L, 0, 1); + int glm_index = lua_gettop(L); + + setup_mat4(L, glm_index); + setup_transform(L, glm_index); + lua_setfield(L, honey_index, "glm"); +} diff --git a/src/glm/glm.h b/src/glm/glm.h new file mode 100644 index 0000000..2bf803b --- /dev/null +++ b/src/glm/glm.h @@ -0,0 +1,10 @@ +#ifndef HONEY_GLM_H +#define HONEY_GLM_H + +#include + +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); + +#endif diff --git a/src/glm/mat4.c b/src/glm/mat4.c new file mode 100644 index 0000000..f98cdfb --- /dev/null +++ b/src/glm/mat4.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include "util/util.h" + +int mat4_create(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_identity", mat4_identity), + ); + + append_table(L, glm_tbl, tbl); + lua_pop(L, 1); +} + + +int mat4_create(lua_State *L) +{ + lua_newuserdata(L, 16*sizeof(float)); + return 1; +} + + +int mat4_identity(lua_State *L) +{ + void *ptr; + hs_parse_args(L, hs_user(ptr)); + mat4 *m = ptr; + + glm_mat4_identity(*m); + return 0; +} diff --git a/src/glm/transform.c b/src/glm/transform.c new file mode 100644 index 0000000..4dce595 --- /dev/null +++ b/src/glm/transform.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include "util/util.h" + + +int rotate_z(lua_State *L); + + +void setup_transform(lua_State *L, int glm_tbl) +{ + int tbl = hs_create_table(L, + hs_str_cfunc("rotate_z", rotate_z), + ); + + append_table(L, glm_tbl, tbl); + lua_pop(L, 1); +} + + +int rotate_z(lua_State *L) +{ + void *src_ptr, *dest_ptr; + lua_Number angle; + hs_parse_args(L, hs_user(src_ptr), hs_num(angle), hs_user(dest_ptr)); + mat4 *source = src_ptr; + mat4 *dest = dest_ptr; + + glm_rotate_z(*source, angle, *dest); + return 0; +} diff --git a/src/main.c b/src/main.c index 66b1f11..5f5d399 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include "gl/gl.h" #include "image/image.h" +#include "glm/glm.h" int main(int argc, char **argv) @@ -16,6 +17,7 @@ int main(int argc, char **argv) setup_gl(L, honey_index); setup_window(L, honey_index); setup_image(L, honey_index); + setup_glm(L, honey_index); lua_setglobal(L, "honey"); int err = luaL_loadfile(L, "honey.lua"); -- cgit v1.2.1