From ccd98d4dbdb7acde2433153a01d00a3b9bed02c0 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 27 Oct 2020 15:31:14 -0500 Subject: fix bug in honey.shader.new and add basic primitives bindings --- demo/main.lua | 25 ++++++++++++++++++++--- src/honey.c | 8 ++++++++ src/mesh/mesh.c | 2 +- src/primitives/primitives.c | 26 +++++++++++++++++++++++ src/primitives/primitives.h | 3 +++ src/shader/shader.c | 50 +++++++++++++++------------------------------ 6 files changed, 76 insertions(+), 38 deletions(-) diff --git a/demo/main.lua b/demo/main.lua index 0ec77a9..bad2c68 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -4,9 +4,28 @@ end honey.input.key.bind(honey.input.key.escape, honey.exit) +local vertex_shader = [[ +#version 330 core + +layout(location = 0) in vec3 position; + +void main() +{ + gl_Position.xyz = position; + gl_Position.w = 1.0; +} ]] +local fragment_shader = [[ +#version 330 core + +out vec4 color; +void main() { color = vec4(1,0,0,1); } ]] + +local shader = honey.shader.new(vertex_shader, fragment_shader) +local plane = honey.primitives.plane(1,1) + function honey.update(dt) end ---function honey.draw() --- print('draw!') ---end +function honey.draw() + honey.mesh.draw(plane, shader) +end diff --git a/src/honey.c b/src/honey.c index cc6b980..7451098 100644 --- a/src/honey.c +++ b/src/honey.c @@ -79,6 +79,9 @@ bool honey_setup(lua_State** L) honey_setup_mesh(*L); lua_setfield(*L, -2, "mesh"); + honey_setup_primitives(*L); + lua_setfield(*L, -2, "primitives"); + lua_pushcfunction(*L, honey_exit); lua_setfield(*L, -2, "exit"); @@ -139,6 +142,9 @@ bool honey_run(lua_State* L, honey_options opts) { } } + glClearColor(0,0,0,1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + if (draw_callback != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, draw_callback); int result = honey_lua_pcall(L, 0, 0); @@ -148,6 +154,8 @@ bool honey_run(lua_State* L, honey_options opts) { glfwSetWindowShouldClose(window, true); } } + + glfwSwapBuffers(window); } lua_close(L); diff --git a/src/mesh/mesh.c b/src/mesh/mesh.c index 8e7e444..9c045ec 100644 --- a/src/mesh/mesh.c +++ b/src/mesh/mesh.c @@ -90,7 +90,7 @@ honey_result honey_mesh_new(honey_mesh* mesh, /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ void honey_mesh_draw(honey_mesh mesh, int shader) { - honey_shader_use(shader); + glUseProgram(shader); glBindVertexArray(mesh.vertex_array); glDrawElements(GL_TRIANGLES, mesh.n_indices, GL_UNSIGNED_INT, 0); diff --git a/src/primitives/primitives.c b/src/primitives/primitives.c index efc4a0f..1bfbe14 100644 --- a/src/primitives/primitives.c +++ b/src/primitives/primitives.c @@ -1,5 +1,31 @@ #include "primitives.h" +static int honey_mesh_lua_plane(lua_State* L) +{ + float width, height; + honey_lua_parse_arguments(L, 2, + HONEY_NUMBER, &width, + HONEY_NUMBER, &height); + + honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + if (honey_mesh_new_textured_plane(mesh, width, height) != HONEY_OK) { + lua_pushstring(L, "error encountered while building plane"); + lua_error(L); + } + return 1; +} + +void honey_setup_primitives(lua_State* L) +{ + honey_lua_element primitive_elements[] = { + { "plane", HONEY_FUNCTION, { .function = honey_mesh_lua_plane } }, + }; + + honey_lua_create_table(L, primitive_elements, 1); +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + honey_result honey_mesh_new_textured_plane(honey_mesh* mesh, float width, float height) { diff --git a/src/primitives/primitives.h b/src/primitives/primitives.h index fbf7753..ff4b01b 100644 --- a/src/primitives/primitives.h +++ b/src/primitives/primitives.h @@ -9,6 +9,9 @@ #include "../common.h" #include "../mesh/mesh.h" +/** @brief Push table of lua bindings for creating primitives to the stack. */ +void honey_setup_primitives(lua_State* L); + /** @brief Create a textured plane. * * This function creates a plane with vertex positions in attribute 0, diff --git a/src/shader/shader.c b/src/shader/shader.c index d733c60..122d59e 100644 --- a/src/shader/shader.c +++ b/src/shader/shader.c @@ -19,28 +19,6 @@ void honey_setup_shader(lua_State* L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -static bool compile_shader(int* shader, - const char* source, - int shader_type, - char* error_message, - size_t error_size) -{ - int success; - - *shader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(*shader, 1, source, NULL); - glCompileShader(*shader); - glGetShaderiv(*shader, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(*shader, error_size, NULL, error_message); - return false; - } - - return true; -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - int honey_shader_new(lua_State* L) { if (!honey_lua_validate_types(L, 2, HONEY_STRING, HONEY_STRING)) @@ -48,23 +26,28 @@ int honey_shader_new(lua_State* L) const char* vertex_shader_source = lua_tostring(L, 1); const char* fragment_shader_source = lua_tostring(L, 2); - + + int success; char error[1024]; - int vertex_shader, fragment_shader; - - if (!compile_shader(&vertex_shader, - vertex_shader_source, - GL_VERTEX_SHADER, - error, 1024)) { + int vertex_shader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex_shader, 1, + &vertex_shader_source, NULL); + glCompileShader(vertex_shader); + glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertex_shader, 1024, NULL, error); lua_pushstring(L, error); lua_error(L); } - if (!compile_shader(&fragment_shader, - fragment_shader_source, - GL_FRAGMENT_SHADER, - error, 1024)) { + int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment_shader, 1, + &fragment_shader_source, NULL); + glCompileShader(fragment_shader); + glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragment_shader, 1024, NULL, error); lua_pushstring(L, error); lua_error(L); } @@ -74,7 +57,6 @@ int honey_shader_new(lua_State* L) glAttachShader(shader, fragment_shader); glLinkProgram(shader); - int success; glGetShaderiv(shader, GL_LINK_STATUS, &success); if (!success) { glGetShaderInfoLog(shader, 1024, NULL, error); -- cgit v1.2.1