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/texture.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/gl/texture.c (limited to 'src/gl/texture.c') 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; +} -- 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/texture.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'src/gl/texture.c') diff --git a/src/gl/texture.c b/src/gl/texture.c index d0c3a0a..739dd13 100644 --- a/src/gl/texture.c +++ b/src/gl/texture.c @@ -2,6 +2,7 @@ #include #include #include +#include "util/util.h" int gl_texture_create(lua_State *L); @@ -13,27 +14,25 @@ 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 tbl = hs_create_table(L, + /* functions */ + hs_str_cfunc("GenTextures", gl_texture_create), + hs_str_cfunc("BindTexture", gl_texture_bind), + hs_str_cfunc("TexImage2D", gl_texture_image_2d), + hs_str_cfunc("GenerateMipmap", gl_texture_generate_mipmaps), + hs_str_cfunc("ActiveTexture", gl_texture_set_active), + + /******** enums ********/ + /* texture binding targets */ + hs_str_int("TEXTURE_2D", GL_TEXTURE_2D), + + /* texture data formats */ + hs_str_int("RGB", GL_RGB), + hs_str_int("RGBA", GL_RGBA), ); - 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"); + append_table(L, gl_index, tbl); + lua_pop(L, 1); } -- cgit v1.2.1 From 711f530f7afe3358c674cf418ee067e7a4501699 Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 23 Aug 2022 00:31:14 -0500 Subject: add gl.TexParameteri() --- src/gl/texture.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/gl/texture.c') diff --git a/src/gl/texture.c b/src/gl/texture.c index 739dd13..f9a65b3 100644 --- a/src/gl/texture.c +++ b/src/gl/texture.c @@ -10,6 +10,7 @@ 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); +int gl_tex_parameter_i(lua_State *L); void setup_texture(lua_State *L, int gl_index) @@ -21,6 +22,7 @@ void setup_texture(lua_State *L, int gl_index) hs_str_cfunc("TexImage2D", gl_texture_image_2d), hs_str_cfunc("GenerateMipmap", gl_texture_generate_mipmaps), hs_str_cfunc("ActiveTexture", gl_texture_set_active), + hs_str_cfunc("TexParameteri", gl_tex_parameter_i), /******** enums ********/ /* texture binding targets */ @@ -29,6 +31,19 @@ void setup_texture(lua_State *L, int gl_index) /* texture data formats */ hs_str_int("RGB", GL_RGB), hs_str_int("RGBA", GL_RGBA), + + /* texture parameters */ + hs_str_int("TEXTURE_WRAP_S", GL_TEXTURE_WRAP_S), + hs_str_int("TEXTURE_WRAP_T", GL_TEXTURE_WRAP_T), + hs_str_int("TEXTURE_MIN_FILTER", GL_TEXTURE_MIN_FILTER), + hs_str_int("TEXTURE_MAG_FILTER", GL_TEXTURE_MAG_FILTER), + + /* wrapping types */ + hs_str_int("REPEAT", GL_REPEAT), + + /* filter types */ + hs_str_int("NEAREST", GL_NEAREST), + hs_str_int("LINEAR", GL_LINEAR), ); append_table(L, gl_index, tbl); @@ -90,3 +105,12 @@ int gl_texture_set_active(lua_State *L) glActiveTexture(GL_TEXTURE0 + unit); return 0; } + + +int gl_tex_parameter_i(lua_State *L) +{ + lua_Integer target, pname, param; + hs_parse_args(L, hs_int(target), hs_int(pname), hs_int(param)); + glTexParameteri(target, pname, param); + return 0; +} -- cgit v1.2.1