#include #include #include #include #include "util/util.h" 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); int gl_tex_parameter_i(lua_State *L); void setup_texture(lua_State *L, int gl_index) { struct honey_tbl_t tbl[] = { /* functions */ H_FUNC("GenTextures", gl_texture_create), H_FUNC("BindTexture", gl_texture_bind), H_FUNC("TexImage2D", gl_texture_image_2d), H_FUNC("GenerateMipmap", gl_texture_generate_mipmaps), H_FUNC("ActiveTexture", gl_texture_set_active), H_FUNC("TexParameteri", gl_tex_parameter_i), /******** enums ********/ /* texture binding targets */ H_INT("TEXTURE_2D", GL_TEXTURE_2D), /* texture data formats */ H_INT("RGB", GL_RGB), H_INT("RGBA", GL_RGBA), /* texture parameters */ H_INT("TEXTURE_WRAP_S", GL_TEXTURE_WRAP_S), H_INT("TEXTURE_WRAP_T", GL_TEXTURE_WRAP_T), H_INT("TEXTURE_MIN_FILTER", GL_TEXTURE_MIN_FILTER), H_INT("TEXTURE_MAG_FILTER", GL_TEXTURE_MAG_FILTER), H_INT("TEXTURE_SWIZZLE_R", GL_TEXTURE_SWIZZLE_R), H_INT("TEXTURE_SWIZZLE_G", GL_TEXTURE_SWIZZLE_G), H_INT("TEXTURE_SWIZZLE_B", GL_TEXTURE_SWIZZLE_B), H_INT("TEXTURE_SWIZZLE_A", GL_TEXTURE_SWIZZLE_A), /* wrapping types */ H_INT("REPEAT", GL_REPEAT), /* filter types */ H_INT("NEAREST", GL_NEAREST), H_INT("LINEAR", GL_LINEAR), /* swizzle targets */ H_INT("RED", GL_RED), H_INT("GREEN", GL_GREEN), H_INT("BLUE", GL_BLUE), H_INT("ALPHA", GL_ALPHA), H_INT("ZERO", GL_ZERO), H_INT("ONE", GL_ONE), H_END }; create_table(L, tbl); append_table(L, gl_index, lua_gettop(L)); lua_pop(L, 1); } 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; target = luaL_checkinteger(L, 1); texture = luaL_checkinteger(L, 2); glBindTexture(target, texture); return 0; } int gl_texture_image_2d(lua_State *L) { lua_Integer target, mipmap_level, internal_format, width, height, format, type; target = luaL_checkinteger(L, 1); mipmap_level = luaL_checkinteger(L, 2); internal_format = luaL_checkinteger(L, 3); width = luaL_checkinteger(L, 4); height = luaL_checkinteger(L, 5); format = luaL_checkinteger(L, 6); type = luaL_checkinteger(L, 7); void *data = lua_touserdata(L, 8); 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 = luaL_checkinteger(L, 1); glGenerateMipmap(target); return 0; } int gl_texture_set_active(lua_State *L) { lua_Integer unit = luaL_checkinteger(L, 1); glActiveTexture(GL_TEXTURE0 + unit); return 0; } int gl_tex_parameter_i(lua_State *L) { lua_Integer target, pname, param; target = luaL_checkinteger(L, 1); pname = luaL_checkinteger(L, 2); param = luaL_checkinteger(L, 3); glTexParameteri(target, pname, param); return 0; }