aboutsummaryrefslogtreecommitdiff
path: root/source/30-game/Texture.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-05-30 17:03:20 -0700
committerrtk0c <[email protected]>2022-05-30 17:03:20 -0700
commite66286ebe30afc9acc4531fc2bea29b7fb924f93 (patch)
treefa6b76554c3eb88bc8f088fbab68e20c40118ca7 /source/30-game/Texture.hpp
parent366ef5a5450c6e0e680c924c3454943a9ae9814d (diff)
Changeset: 56 Buildsystem cleanup: change to layered structure for different targets
Diffstat (limited to 'source/30-game/Texture.hpp')
-rw-r--r--source/30-game/Texture.hpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/source/30-game/Texture.hpp b/source/30-game/Texture.hpp
new file mode 100644
index 0000000..108dfa7
--- /dev/null
+++ b/source/30-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;
+};