aboutsummaryrefslogtreecommitdiff
path: root/src/brussel.engine/Texture.hpp
blob: 108dfa739865469fbd668f9ff4c25ad665ac3dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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;
};