From dccc9bef56d51c5cad13c2568c5a3b97f637fd6e Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 10:45:52 -0500 Subject: refactor: move drawing code into separate file --- src/gl/drawing.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/gl/drawing.c (limited to 'src/gl/drawing.c') diff --git a/src/gl/drawing.c b/src/gl/drawing.c new file mode 100644 index 0000000..a64f56d --- /dev/null +++ b/src/gl/drawing.c @@ -0,0 +1,71 @@ +#include "gl/glad/glad.h" +#include +#include +#include + +int gl_set_viewport(lua_State *L); +int gl_draw_arrays(lua_State *L); +int gl_set_clear_color(lua_State *L); +int gl_clear(lua_State *L); + +void setup_drawing(lua_State *L, int gl_index) +{ + int primitive_types = hs_create_table(L, + hs_str_int("points", GL_POINTS), + hs_str_int("lines", GL_LINES), + hs_str_int("triangles", GL_TRIANGLES), + ); + + int buffer_masks = hs_create_table(L, + hs_str_int("colorBuffer", GL_COLOR_BUFFER_BIT), + hs_str_int("depthBuffer", GL_DEPTH_BUFFER_BIT), + hs_str_int("stencilBuffer", GL_STENCIL_BUFFER_BIT), + ); + + hs_create_table(L, + hs_str_cfunc("drawArrays", gl_draw_arrays), + hs_str_cfunc("setClearColor", gl_set_clear_color), + hs_str_cfunc("clear", gl_clear), + hs_str_cfunc("setViewport", gl_set_viewport), + + hs_str_tbl("primitiveType", primitive_types), + hs_str_tbl("bufferMask", buffer_masks), + ); + + lua_setfield(L, gl_index, "draw"); +} + +int gl_set_clear_color(lua_State *L) +{ + lua_Number r, g, b, a; + hs_parse_args(L, hs_num(r), hs_num(g), hs_num(b), hs_num(a)); + glClearColor(r, g, b, a); + return 0; +} + + +int gl_clear(lua_State *L) +{ + lua_Integer mask; + hs_parse_args(L, hs_int(mask)); + glClear(mask); + return 0; +} + + +int gl_draw_arrays(lua_State *L) +{ + lua_Integer mode, first, count; + hs_parse_args(L, hs_int(mode), hs_int(first), hs_int(count)); + glDrawArrays(mode, first, count); + return 0; +} + + +int gl_set_viewport(lua_State *L) +{ + lua_Integer x, y, w, h; + hs_parse_args(L, hs_int(x), hs_int(y), hs_int(w), hs_int(h)); + glViewport(x, y, w, h); + return 0; +} -- cgit v1.2.1 From 249833947ff691dd79453c4d817387a6922236d6 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 11:38:26 -0500 Subject: add element buffers --- src/gl/drawing.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/gl/drawing.c') diff --git a/src/gl/drawing.c b/src/gl/drawing.c index a64f56d..ca914e0 100644 --- a/src/gl/drawing.c +++ b/src/gl/drawing.c @@ -5,6 +5,7 @@ int gl_set_viewport(lua_State *L); int gl_draw_arrays(lua_State *L); +int gl_draw_elements(lua_State *L); int gl_set_clear_color(lua_State *L); int gl_clear(lua_State *L); @@ -24,6 +25,7 @@ void setup_drawing(lua_State *L, int gl_index) hs_create_table(L, hs_str_cfunc("drawArrays", gl_draw_arrays), + hs_str_cfunc("drawElements", gl_draw_elements), hs_str_cfunc("setClearColor", gl_set_clear_color), hs_str_cfunc("clear", gl_clear), hs_str_cfunc("setViewport", gl_set_viewport), @@ -62,6 +64,15 @@ int gl_draw_arrays(lua_State *L) } +int gl_draw_elements(lua_State *L) +{ + lua_Integer mode, count, type, offset; + hs_parse_args(L, hs_int(mode), hs_int(count), hs_int(type), hs_int(offset)); + glDrawElements(mode, count, type, (const void*)offset); + return 0; +} + + int gl_set_viewport(lua_State *L) { lua_Integer x, y, w, h; -- cgit v1.2.1 From c52e1b30391d730cd6d20f65ec75d61884355c5c Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 23:58:00 -0500 Subject: refactor: rename opengl functions to match their C counterparts --- src/gl/drawing.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'src/gl/drawing.c') diff --git a/src/gl/drawing.c b/src/gl/drawing.c index ca914e0..e6b1e13 100644 --- a/src/gl/drawing.c +++ b/src/gl/drawing.c @@ -2,6 +2,7 @@ #include #include #include +#include "util/util.h" int gl_set_viewport(lua_State *L); int gl_draw_arrays(lua_State *L); @@ -11,30 +12,28 @@ int gl_clear(lua_State *L); void setup_drawing(lua_State *L, int gl_index) { - int primitive_types = hs_create_table(L, - hs_str_int("points", GL_POINTS), - hs_str_int("lines", GL_LINES), - hs_str_int("triangles", GL_TRIANGLES), - ); - - int buffer_masks = hs_create_table(L, - hs_str_int("colorBuffer", GL_COLOR_BUFFER_BIT), - hs_str_int("depthBuffer", GL_DEPTH_BUFFER_BIT), - hs_str_int("stencilBuffer", GL_STENCIL_BUFFER_BIT), - ); + int tbl = hs_create_table(L, + /* functions */ + hs_str_cfunc("DrawArrays", gl_draw_arrays), + hs_str_cfunc("DrawElements", gl_draw_elements), + hs_str_cfunc("ClearColor", gl_set_clear_color), + hs_str_cfunc("Clear", gl_clear), + hs_str_cfunc("Viewport", gl_set_viewport), - hs_create_table(L, - hs_str_cfunc("drawArrays", gl_draw_arrays), - hs_str_cfunc("drawElements", gl_draw_elements), - hs_str_cfunc("setClearColor", gl_set_clear_color), - hs_str_cfunc("clear", gl_clear), - hs_str_cfunc("setViewport", gl_set_viewport), + /******** enums ********/ + /* rendering primitives */ + hs_str_int("POINTS", GL_POINTS), + hs_str_int("LINES", GL_LINES), + hs_str_int("TRIANGLES", GL_TRIANGLES), - hs_str_tbl("primitiveType", primitive_types), - hs_str_tbl("bufferMask", buffer_masks), + /* clear bitmasks */ + hs_str_int("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT), + hs_str_int("DEPTH_BUFFER_BIT", GL_DEPTH_BUFFER_BIT), + hs_str_int("STENCIL_BUFFER_BIT", GL_STENCIL_BUFFER_BIT), ); - lua_setfield(L, gl_index, "draw"); + append_table(L, gl_index, tbl); + lua_pop(L, 1); } int gl_set_clear_color(lua_State *L) -- cgit v1.2.1