From 1f47b685f35455afcc7441389cdc60018f66d159 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 15:55:44 -0500 Subject: add textures --- src/gl/CMakeLists.txt | 1 + src/gl/gl.c | 2 ++ src/gl/gl.h | 1 + src/gl/shader.c | 11 ++++++ src/gl/texture.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 2 ++ 6 files changed, 110 insertions(+) create mode 100644 src/gl/texture.c (limited to 'src') diff --git a/src/gl/CMakeLists.txt b/src/gl/CMakeLists.txt index f995ef5..d4bc770 100644 --- a/src/gl/CMakeLists.txt +++ b/src/gl/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources(honey PUBLIC ${GL}/drawing.c ${GL}/shader.c ${GL}/window.c + ${GL}/texture.c ${GL}/gl.c ${GL}/glad/glad.c ) diff --git a/src/gl/gl.c b/src/gl/gl.c index 8f56d33..c4502dc 100644 --- a/src/gl/gl.c +++ b/src/gl/gl.c @@ -21,6 +21,7 @@ int gl_get_error(lua_State *L); void setup_gl(lua_State *L, int honey_index) { int data_types = hs_create_table(L, + hs_str_int("uchar", GL_UNSIGNED_BYTE), hs_str_int("uint", GL_UNSIGNED_INT), hs_str_int("int", GL_INT), hs_str_int("float", GL_FLOAT), @@ -48,6 +49,7 @@ void setup_gl(lua_State *L, int honey_index) setup_shader(L, gl_index); setup_drawing(L, gl_index); setup_data(L, gl_index); + setup_texture(L, gl_index); lua_setfield(L, honey_index, "gl"); } diff --git a/src/gl/gl.h b/src/gl/gl.h index 766fe63..8aa1ef7 100644 --- a/src/gl/gl.h +++ b/src/gl/gl.h @@ -7,6 +7,7 @@ void setup_gl(lua_State *L, int honey_index); void setup_shader(lua_State *L, int gl_index); void setup_drawing(lua_State *L, int gl_index); void setup_data(lua_State *L, int gl_index); +void setup_texture(lua_State *L, int gl_index); void setup_window(lua_State *L, int honey_index); diff --git a/src/gl/shader.c b/src/gl/shader.c index fb64bc0..c3b409c 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -14,6 +14,7 @@ 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_1i(lua_State *L); int gl_uniform_4f(lua_State *L); @@ -36,6 +37,7 @@ void setup_shader(lua_State *L, int gl_index) hs_str_cfunc("use", gl_program_use), hs_str_cfunc("getUniformLocation", gl_uniform_get_location), + hs_str_cfunc("uniform1i", gl_uniform_1i), hs_str_cfunc("uniform4f", gl_uniform_4f), hs_str_tbl("type", shader_types), @@ -141,6 +143,15 @@ int gl_uniform_get_location(lua_State *L) } +int gl_uniform_1i(lua_State *L) +{ + lua_Integer location, v0; + hs_parse_args(L, hs_int(location), hs_int(v0)); + glUniform1i(location, v0); + return 0; +} + + int gl_uniform_4f(lua_State *L) { lua_Integer location; diff --git a/src/gl/texture.c b/src/gl/texture.c new file mode 100644 index 0000000..d0c3a0a --- /dev/null +++ b/src/gl/texture.c @@ -0,0 +1,93 @@ +#include "gl/glad/glad.h" +#include +#include +#include + + +int gl_texture_create(lua_State *L); +int gl_texture_bind(lua_State *L); +int gl_texture_image_2d(lua_State *L); +int gl_texture_generate_mipmaps(lua_State *L); +int gl_texture_set_active(lua_State *L); + + +void setup_texture(lua_State *L, int gl_index) +{ + int bind_targets = hs_create_table(L, + hs_str_int("texture2d", GL_TEXTURE_2D), + ); + + int formats = hs_create_table(L, + hs_str_int("rgb", GL_RGB), + hs_str_int("rgba", GL_RGBA), + ); + + hs_create_table(L, + hs_str_cfunc("create", gl_texture_create), + hs_str_cfunc("bind", gl_texture_bind), + hs_str_cfunc("bufferImage2d", gl_texture_image_2d), + hs_str_cfunc("generateMipmaps", gl_texture_generate_mipmaps), + hs_str_cfunc("setActiveUnit", gl_texture_set_active), + + hs_str_tbl("bindTarget", bind_targets), + hs_str_tbl("format", formats), + ); + + lua_setfield(L, gl_index, "texture"); +} + + +int gl_texture_create(lua_State *L) +{ + unsigned int texture; + glGenTextures(1, &texture); + lua_pushinteger(L, texture); + return 1; +} + + +int gl_texture_bind(lua_State *L) +{ + lua_Integer target, texture; + hs_parse_args(L, hs_int(target), hs_int(texture)); + glBindTexture(target, texture); + return 0; +} + + +int gl_texture_image_2d(lua_State *L) +{ + lua_Integer target, mipmap_level, + internal_format, + width, height, + format, type; + void *data; + hs_parse_args(L, + hs_int(target), hs_int(mipmap_level), + hs_int(internal_format), + hs_int(width), hs_int(height), + hs_int(format), hs_int(type), + hs_light(data) + ); + + glTexImage2D(target, mipmap_level, internal_format, width, height, 0, format, type, data); + return 0; +} + + +int gl_texture_generate_mipmaps(lua_State *L) +{ + lua_Integer target; + hs_parse_args(L, hs_int(target)); + glGenerateMipmap(target); + return 0; +} + + +int gl_texture_set_active(lua_State *L) +{ + lua_Integer unit; + hs_parse_args(L, hs_int(unit)); + glActiveTexture(GL_TEXTURE0 + unit); + return 0; +} diff --git a/src/main.c b/src/main.c index c989dbd..66b1f11 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include #include "gl/gl.h" +#include "image/image.h" int main(int argc, char **argv) @@ -14,6 +15,7 @@ int main(int argc, char **argv) int honey_index = lua_gettop(L); setup_gl(L, honey_index); setup_window(L, honey_index); + setup_image(L, honey_index); lua_setglobal(L, "honey"); int err = luaL_loadfile(L, "honey.lua"); -- cgit v1.2.1