aboutsummaryrefslogtreecommitdiff
path: root/source/Texture.hpp
blob: c330bb31e63f56db803aacfc1934ea389ec7b891 (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
#pragma once

#include "RcPtr.hpp"

#include <absl/container/flat_hash_map.h>
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <memory>

// TODO abstract texture traits such as component sizes from OpenGL

class TextureInfo {
public:
	glm::ivec2 size;
	bool isAtlas = false;
};

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 Filtering {
		LinearFilter,
		NearestFilter,
	};

	struct TextureProperties {
		Filtering minifyingFilter = LinearFilter;
		Filtering magnifyingFilter = LinearFilter;
	};

	bool InitFromFile(const char* filePath, const TextureProperties& props, bool flipVertically = false);
	// bool InitFromImage(const Image& image, const TextureProperties& props, bool flipVertically = false);

	const TextureInfo& GetInfo() const;
	GLuint GetHandle() const;

	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 {
public:
	static inline TextureManager* instance = nullptr;

private:
	absl::flat_hash_map<std::string_view, RcPtr<Texture>> mTextures;

public:
	void DiscoverTextures();

	const auto& GetTextures() const { return mTextures; }
	Texture* FindTexture(std::string_view name);
};