diff options
Diffstat (limited to 'ProjectBrussel/Game/Texture.hpp')
-rw-r--r-- | ProjectBrussel/Game/Texture.hpp | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/ProjectBrussel/Game/Texture.hpp b/ProjectBrussel/Game/Texture.hpp new file mode 100644 index 0000000..108dfa7 --- /dev/null +++ b/ProjectBrussel/Game/Texture.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include "GraphicsTags.hpp" +#include "Image.hpp" +#include "Ires.hpp" +#include "RcPtr.hpp" + +#include <glad/glad.h> +#include <cstdint> +#include <glm/glm.hpp> +#include <span> + +// TODO abstract texture traits such as component sizes from OpenGL + +struct Subregion { + float u0 = 0.0f; + float v0 = 0.0f; + float u1 = 0.0f; + float v1 = 0.0f; +}; + +struct TextureInfo { + glm::ivec2 size; + Tags::TexFilter minifyingFilter = Tags::TF_Linear; + Tags::TexFilter magnifyingFilter = Tags::TF_Linear; +}; + +class Texture : public RefCounted { + friend class TextureStitcher; + +private: + TextureInfo mInfo; + GLuint mHandle = 0; + +public: + Texture() = default; + ~Texture(); + + Texture(const Texture&) = delete; + Texture& operator=(const Texture&) = delete; + Texture(Texture&&) = default; + Texture& operator=(Texture&&) = default; + + enum ErrorCode { + EC_Success, + EC_AlreadyInitialized, + EC_FileIoFailed, + EC_InvalidImage, + }; + + ErrorCode InitFromFile(const char* filePath); + ErrorCode InitFromImage(const Image& image); + + struct AtlasSource { + std::string name; + Image image; + }; + + struct AltasElement { + std::string name; + Subregion subregion; + glm::ivec2 subregionSize; + }; + + enum PackingMode { + PM_KeepSquare, + PM_VerticalExtension, + PM_HorizontalExtension, + }; + + struct AtlasInput { + std::span<AtlasSource> sources; + PackingMode packingMode; + }; + struct AtlasOutput { + std::vector<AltasElement> elements; + }; + ErrorCode InitAtlas(const AtlasInput& in, AtlasOutput* out = nullptr); + + const TextureInfo& GetInfo() const; + GLuint GetHandle() const; + + bool IsValid() const; +}; + +class IresTexture : public IresObject { +public: + RcPtr<Texture> mInstance; + +public: + IresTexture() + : IresObject(KD_Texture) {} + + Texture* CreateInstance() const; + Texture* GetInstance(); + + void Write(IresWritingContext& ctx, rapidjson::Value& value, rapidjson::Document& root) const override; + void Read(IresLoadingContext& ctx, const rapidjson::Value& value) override; +}; |