diff --git a/src/inferno/asset/texture.cpp b/src/inferno/asset/texture.cpp index 0f7011c..43809f4 100644 --- a/src/inferno/asset/texture.cpp +++ b/src/inferno/asset/texture.cpp @@ -8,6 +8,7 @@ #include // uint8_t, uint32_t #include // std::shared_ptr +#include "assimp/texture.h" #include "glad/glad.h" #include "ruc/meta/assert.h" #define STB_IMAGE_IMPLEMENTATION @@ -56,6 +57,32 @@ std::shared_ptr Texture2D::create(std::string_view path) return result; } +std::shared_ptr Texture2D::create(aiTexture* texture) +{ + auto result = std::shared_ptr(new Texture2D(texture->mFilename.C_Str())); + + int width = static_cast(texture->mWidth); + int height = static_cast(texture->mHeight); + int channels = 0; + unsigned char* data = reinterpret_cast(texture->pcData); + bool isCompressed = height == 0; // height 0 is compression, byte length stored in width variable + + if (isCompressed) { + stbi_set_flip_vertically_on_load(0); + data = stbi_load_from_memory(data, width, &width, &height, &channels, STBI_default); + } + else { + channels = 4; // ARGB888 + // TODO: imprement format hints? `archFormatHint' + VERIFY_NOT_REACHED(); + } + + result->init(width, height, channels); + result->create(data); + + return result; +} + void Texture2D::bind(uint32_t unit) const { // Set active unit @@ -198,6 +225,3 @@ void TextureCubemap::create() } } // namespace Inferno - -// FIXME -// auto texture = (type == Texture::TwoDimensional) ? Texture2D::create(path) : TextureCubemap::create(path); diff --git a/src/inferno/asset/texture.h b/src/inferno/asset/texture.h index 298d5f8..f73e594 100644 --- a/src/inferno/asset/texture.h +++ b/src/inferno/asset/texture.h @@ -12,6 +12,8 @@ #include "inferno/asset/asset-manager.h" +struct aiTexture; + namespace Inferno { class Texture2D; @@ -63,6 +65,7 @@ public: // Factory function static std::shared_ptr create(std::string_view path); + static std::shared_ptr create(aiTexture* texture); virtual void bind(uint32_t unit = 0) const override; virtual void unbind() const override;