From 051abd04e4527095ef15412939450fbe504daebe Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 26 Oct 2021 19:33:40 +0800 Subject: +texture & imagedata --- Runtime/Scripting/Rendering/ImageData.bind.cpp | 38 +++++++ Runtime/Scripting/Rendering/Rendering.bind.cpp | 32 ++++++ Runtime/Scripting/Rendering/Texture.bind.cpp | 143 +++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 Runtime/Scripting/Rendering/ImageData.bind.cpp create mode 100644 Runtime/Scripting/Rendering/Texture.bind.cpp (limited to 'Runtime/Scripting') diff --git a/Runtime/Scripting/Rendering/ImageData.bind.cpp b/Runtime/Scripting/Rendering/ImageData.bind.cpp new file mode 100644 index 0000000..88412e5 --- /dev/null +++ b/Runtime/Scripting/Rendering/ImageData.bind.cpp @@ -0,0 +1,38 @@ +#include "Runtime/Graphics/ImageData.h" + +using namespace LuaBind; + +LUA_BIND_REGISTRY(ImageData) +{ + LUA_BIND_REGISTER_METHODS(state, + { "GetWidth", _GetWidth }, + { "GetHeight", _GetHeight }, + { "GetSize", _GetSize } + ); +} + +LUA_BIND_POSTPROCESS(ImageData) +{ +} + +LUA_BIND_IMPL_METHOD(ImageData, _GetWidth) +{ + LUA_BIND_PREPARE(L, ImageData); + state.Push(self->width); + return 1; +} + +LUA_BIND_IMPL_METHOD(ImageData, _GetHeight) +{ + LUA_BIND_PREPARE(L, ImageData); + state.Push(self->height); + return 1; +} + +LUA_BIND_IMPL_METHOD(ImageData, _GetSize) +{ + LUA_BIND_PREPARE(L, ImageData); + state.Push(self->width); + state.Push(self->height); + return 2; +} diff --git a/Runtime/Scripting/Rendering/Rendering.bind.cpp b/Runtime/Scripting/Rendering/Rendering.bind.cpp index 29fa421..40b3ac5 100644 --- a/Runtime/Scripting/Rendering/Rendering.bind.cpp +++ b/Runtime/Scripting/Rendering/Rendering.bind.cpp @@ -1,4 +1,32 @@ #include "Runtime/Graphics/Shader.h" +#include "Runtime/Graphics/Texture.h" +#include "Runtime/Graphics/ImageData.h" + +#define STB_IMAGE_IMPLEMENTATION +#include "ThirdParty/stb/stb_image.h" + +// imgData = Rendering.LoadImage(path) +int LoadImage(lua_State* L) +{ + LUA_BIND_STATE(L); + + const char* path = state.GetValue(1, ""); + + stbi_set_flip_vertically_on_load(true); + ImageData* data = new ImageData(state.GetVM()); + int channels; + data->pixels = stbi_load(path, &data->width, &data->height, &channels, 0); + data->format = ImageData::EPixelFormat::RGB; + data->type = ImageData::EPixelElementType::UNSIGNED_BYTE; + + data->PushUserdata(state); + return 1; +} + +static luaL_Reg funcs[] = { + {"LoadImage", LoadImage}, + {0, 0} +}; int luaopen_GameLab_Engine_Rendering(lua_State* L) { @@ -11,7 +39,11 @@ int luaopen_GameLab_Engine_Rendering(lua_State* L) state.PushNamespace("Engine"); state.PushNamespace("Rendering"); + state.RegisterMethods(funcs); + state.RegisterNativeClass(); + state.RegisterNativeClass(); + state.RegisterNativeClass(); return 1; } \ No newline at end of file diff --git a/Runtime/Scripting/Rendering/Texture.bind.cpp b/Runtime/Scripting/Rendering/Texture.bind.cpp new file mode 100644 index 0000000..061a303 --- /dev/null +++ b/Runtime/Scripting/Rendering/Texture.bind.cpp @@ -0,0 +1,143 @@ +#include "Runtime/Graphics/Texture.h" + +using namespace LuaBind; + +LUA_BIND_REGISTRY(Texture) +{ + LUA_BIND_REGISTER_METHODS(state, + { "New", _New }, + { "GetWidth", _GetWidth }, + { "GetHeight", _GetHeight }, + { "GetSize", _GetSize }, + { "GetType", _GetType }, + { "GetFormat", _GetFormat }, + { "GetWrapMode", _GetWrapMode }, + { "GetImageData", _GetImageData }, + { "IsKeepImageData", _IsKeepImageData }, + { "GetFilterMode", _GetFilterMode } + ); +} + +LUA_BIND_POSTPROCESS(Texture) +{ + LUA_BIND_REGISTER_ENUM(state, "ETextureType", + { "TEX_2D", ETextureType::TEX_2D }, + { "TEX_CUBE", ETextureType::TEX_CUBE } + ); + LUA_BIND_REGISTER_ENUM(state, "ETextureFormat", + { "RGBA32", ETextureFormat::RGBA32 }, + { "RGB24", ETextureFormat::RGB24 }, + { "RGB16", ETextureFormat::RGB16 }, + { "R8", ETextureFormat::R8 }, + { "A8", ETextureFormat::A8 } + ); + LUA_BIND_REGISTER_ENUM(state, "ETextureWrapMode", + { "Clamp", ETextureWrapMode::Clamp }, + { "Repeat", ETextureWrapMode::Repeat }, + { "Mirror", ETextureWrapMode::Mirror } + ); + LUA_BIND_REGISTER_ENUM(state, "ETextureFilterMode", + { "Nearest", ETextureFilterMode::Nearest }, + { "Linear", ETextureFilterMode::Linear } + ); +} + +// Texture.New(imgData, keepImgData, type, format, wrapMode, filterMode) +LUA_BIND_IMPL_METHOD(Texture, _New) +{ + LUA_BIND_STATE(L); + LUA_BIND_CHECK(L, "U"); + + ImageData* imgData = state.GetUserdata(1); + + TextureSetting setting; + setting.keepImageData = state.GetValue(2, false); + setting.type = state.GetValue(3, (int)ETextureType::TEX_2D); + setting.format = state.GetValue(4, (int)ETextureFormat::RGBA32); + setting.wrapMode = state.GetValue(5, (int)ETextureWrapMode::Clamp); + setting.filterMode = state.GetValue(6, (int)ETextureFilterMode::Linear); + + try + { + Texture* tex = new Texture(state.GetVM(), setting, imgData); + tex->PushUserdata(state); + return 1; + } + catch (TextureException e) + { + luaL_error(L, "Failed to create texture."); + return 0; + } + return 0; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetWidth) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_Width); + return 1; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetHeight) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_Height); + return 1; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetSize) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_Width); + state.Push(self->m_Height); + return 2; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetType) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_Type); + return 1; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetFormat) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_Format); + return 1; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetWrapMode) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_WrapMode); + return 1; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetFilterMode) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_FilterMode); + return 1; +} + +LUA_BIND_IMPL_METHOD(Texture, _IsKeepImageData) +{ + LUA_BIND_PREPARE(L, Texture); + state.Push(self->m_KeepPixelData); + return 1; +} + +LUA_BIND_IMPL_METHOD(Texture, _GetImageData) +{ + LUA_BIND_PREPARE(L, Texture); + if (self->m_KeepPixelData) + { + self->PushMemberRef(state, self->m_ImageData); + } + else + { + state.PushNil(); + } + return 1; +} -- cgit v1.1-26-g67d0