diff options
-rw-r--r-- | CMakeLists.txt | 21 | ||||
-rw-r--r-- | demo/honey.lua | 12 | ||||
-rw-r--r-- | src/gl/CMakeLists.txt | 12 | ||||
-rw-r--r-- | src/gl/shader.c | 27 | ||||
-rw-r--r-- | src/gl/window.c | 10 |
5 files changed, 60 insertions, 22 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index dc9a83c..3796a43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.2) -project(honey_engine_demo) +project(honey_engine) if (UNIX) set(CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") @@ -32,22 +32,9 @@ add_subdirectory(${LIB_ROOT}/cglm) add_subdirectory(${LIB_ROOT}/glfw-3.3.8) -set(HONEY_LIB_FILES - ${SRC_ROOT}/logging/logging.c - ${SRC_ROOT}/gl/glad/glad.c - ${SRC_ROOT}/gl/gl.c - ${SRC_ROOT}/gl/shader.c - ${SRC_ROOT}/gl/drawing.c - ${SRC_ROOT}/gl/data.c - ${SRC_ROOT}/gl/window.c -) - -set(SOURCE_FILES - ${SRC_ROOT}/main.c - ${HONEY_LIB_FILES} - ) - -add_executable(honey ${SOURCE_FILES}) +set(HONEY_SOURCE ${SRC_ROOT}/main.c ${SRC_ROOT}/logging/logging.c) +add_executable(honey ${HONEY_SOURCE}) +add_subdirectory(${SRC_ROOT}/gl) set(LIBRARIES lua5.1 honeysuckle assimp glfw) if (WIN32) diff --git a/demo/honey.lua b/demo/honey.lua index 2b1759f..470880d 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -26,23 +26,21 @@ end) local vertexShaderSource = [[ #version 330 core layout (location = 0) in vec3 aPos; -out vec3 pos; void main() { gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); - pos = aPos; } ]] local fragmentShaderSource = [[ #version 330 core -in vec3 pos; +uniform vec4 color; out vec4 FragColor; void main() { - FragColor = vec4(pos.x+0.5, 0.0f, 2*(pos.y+pos.x), 1.0f); + FragColor = color; } ]] @@ -100,8 +98,12 @@ while not window.shouldClose(w) do gl.draw.clear(gl.draw.bufferMask.colorBuffer); gl.shader.use(shader) + local time = window.getTime() + local greenValue = (math.sin(time) / 2) + 0.5 + local colorLocation = gl.shader.getUniformLocation(shader, 'color') + gl.shader.uniform4f(colorLocation, 0, greenValue, 0, 1) + gl.data.bindVertexArray(vertexArray) - --gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0) window.swapBuffers(w) diff --git a/src/gl/CMakeLists.txt b/src/gl/CMakeLists.txt new file mode 100644 index 0000000..f995ef5 --- /dev/null +++ b/src/gl/CMakeLists.txt @@ -0,0 +1,12 @@ +project(honey_engine) + +set (GL ${CMAKE_CURRENT_LIST_DIR}) + +target_sources(honey PUBLIC + ${GL}/data.c + ${GL}/drawing.c + ${GL}/shader.c + ${GL}/window.c + ${GL}/gl.c + ${GL}/glad/glad.c +) diff --git a/src/gl/shader.c b/src/gl/shader.c index 96946fe..fb64bc0 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -13,6 +13,9 @@ int gl_program_attach_shader(lua_State *L); int gl_program_link(lua_State *L); int gl_program_use(lua_State *L); +int gl_uniform_get_location(lua_State *L); +int gl_uniform_4f(lua_State *L); + void setup_shader(lua_State *L, int gl_index) { @@ -32,6 +35,9 @@ void setup_shader(lua_State *L, int gl_index) hs_str_cfunc("link", gl_program_link), hs_str_cfunc("use", gl_program_use), + hs_str_cfunc("getUniformLocation", gl_uniform_get_location), + hs_str_cfunc("uniform4f", gl_uniform_4f), + hs_str_tbl("type", shader_types), ); @@ -122,3 +128,24 @@ int gl_program_use(lua_State *L) glUseProgram(program); return 0; } + + +int gl_uniform_get_location(lua_State *L) +{ + lua_Integer program; + char *name; + hs_parse_args(L, hs_int(program), hs_str(name)); + int location = glGetUniformLocation(program, (const GLchar*)name); + lua_pushinteger(L, location); + return 1; +} + + +int gl_uniform_4f(lua_State *L) +{ + lua_Integer location; + lua_Number v0, v1, v2, v3; + hs_parse_args(L, hs_int(location), hs_num(v0), hs_num(v1), hs_num(v2), hs_num(v3)); + glUniform4f(location, v0, v1, v2, v3); + return 0; +} diff --git a/src/gl/window.c b/src/gl/window.c index 075bd6c..1b8c4bb 100644 --- a/src/gl/window.c +++ b/src/gl/window.c @@ -27,6 +27,8 @@ int window_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); + void setup_window(lua_State *L, int honey_index) @@ -50,6 +52,7 @@ void setup_window(lua_State *L, int honey_index) 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_tbl("hintType", hint_types), hs_str_tbl("profileType", profile_types), @@ -158,3 +161,10 @@ int window_set_framebuffer_size_callback(lua_State *L) wdata->framebuffer_size_callback = hs_rstore(L); return 0; } + + +int window_get_time(lua_State *L) +{ + lua_pushnumber(L, glfwGetTime()); + return 1; +} |