aboutsummaryrefslogtreecommitdiff
path: root/source/Texture.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/Texture.hpp')
-rw-r--r--source/Texture.hpp92
1 files changed, 55 insertions, 37 deletions
diff --git a/source/Texture.hpp b/source/Texture.hpp
index c330bb3..ef4d136 100644
--- a/source/Texture.hpp
+++ b/source/Texture.hpp
@@ -1,18 +1,28 @@
#pragma once
+#include "GraphicsTags.hpp"
+#include "Image.hpp"
+#include "Ires.hpp"
#include "RcPtr.hpp"
-#include <absl/container/flat_hash_map.h>
#include <glad/glad.h>
+#include <cstdint>
#include <glm/glm.hpp>
-#include <memory>
+#include <span>
// TODO abstract texture traits such as component sizes from OpenGL
-class TextureInfo {
-public:
+struct Subregion {
+ float u0 = 0.0f;
+ float v0 = 0.0f;
+ float u1 = 0.0f;
+ float v1 = 0.0f;
+};
+
+struct TextureInfo {
glm::ivec2 size;
- bool isAtlas = false;
+ Tags::TexFilter minifyingFilter = Tags::TF_Linear;
+ Tags::TexFilter magnifyingFilter = Tags::TF_Linear;
};
class Texture : public RefCounted {
@@ -31,18 +41,41 @@ public:
Texture(Texture&&) = default;
Texture& operator=(Texture&&) = default;
- enum Filtering {
- LinearFilter,
- NearestFilter,
+ enum ErrorCode {
+ EC_Success,
+ EC_AlreadyInitialized,
+ EC_FileIoFailed,
+ EC_InvalidImage,
};
- struct TextureProperties {
- Filtering minifyingFilter = LinearFilter;
- Filtering magnifyingFilter = LinearFilter;
+ ErrorCode InitFromFile(const char* filePath);
+ ErrorCode InitFromImage(const Image& image);
+
+ struct AtlasSource {
+ std::string name;
+ Image image;
};
- bool InitFromFile(const char* filePath, const TextureProperties& props, bool flipVertically = false);
- // bool InitFromImage(const Image& image, const TextureProperties& props, bool flipVertically = false);
+ 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;
@@ -50,32 +83,17 @@ public:
bool IsValid() const;
};
-/// A pure numerical subregion of a texture. u0/v0 are the UV coordinates of bottom left
-/// corner, and u1/v1 are the top left corner.
-struct Subregion {
- /// Bottom left corner
- float u0 = 0.0f;
- float v0 = 0.0f;
- /// Top right corner
- float u1 = 0.0f;
- float v1 = 0.0f;
-};
-
-/// A subregion of a specific texture.
-struct TextureSubregion : public Subregion {
- RcPtr<Texture> atlasTexture;
-};
-
-class TextureManager {
+class IresTexture : public IresObject {
public:
- static inline TextureManager* instance = nullptr;
-
-private:
- absl::flat_hash_map<std::string_view, RcPtr<Texture>> mTextures;
+ RcPtr<Texture> mInstance;
public:
- void DiscoverTextures();
+ IresTexture()
+ : IresObject(KD_Texture) {}
+
+ Texture* CreateInstance() const;
+ Texture* GetInstance();
- const auto& GetTextures() const { return mTextures; }
- Texture* FindTexture(std::string_view name);
+ void Write(rapidjson::Value& value, rapidjson::Document& root) const override;
+ void Read(const rapidjson::Value& value) override;
};