summaryrefslogtreecommitdiff
path: root/gfx/models/texture.h
blob: 86e76a027b09527c4a828d9d404ed35c0a8c7ef2 (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
#pragma once

#include <cache.h>
#include <filesystem>
#include <glArrays.h>
#include <glm/fwd.hpp>

// IWYU pragma: no_forward_declare Cache
class Image;

struct TextureOptions {
	enum class MapMode {
		Repeat,
		Clamp,
		Mirror,
		Decal,
	};
	MapMode wrapU {MapMode::Repeat}, wrapV {MapMode::Repeat};
	GLint minFilter {GL_LINEAR}, magFilter {GL_LINEAR};
	GLenum type {GL_TEXTURE_2D};
	static GLint glMapMode(MapMode);
};

class Texture {
public:
	virtual ~Texture() = default;
	DEFAULT_MOVE_NO_COPY(Texture);

	explicit Texture(const std::filesystem::path & fileName, TextureOptions = {});
	explicit Texture(const Image & image, TextureOptions = {});
	explicit Texture(GLsizei width, GLsizei height, TextureOptions = {});
	explicit Texture(GLsizei width, GLsizei height, const void * data, TextureOptions = {});

	static Cache<Texture, std::filesystem::path> cachedTexture;

	virtual void bind(GLenum unit = GL_TEXTURE0) const;

	void save(const char * path) const;
	static void save(const glTexture &, const char * path);
	static void saveDepth(const glTexture &, const char * path);
	static void saveNormal(const glTexture &, const char * path);

protected:
	static void save(const glTexture &, GLenum, GLenum, uint8_t channels, const char * path, uint8_t tgaFormat);
	static glm::ivec2 getSize(const glTexture &);

	glTexture m_texture;
	GLenum type;
};

class TextureAtlas : public Texture {
public:
	TextureAtlas(GLsizei width, GLsizei height, GLuint count);

	void bind(GLenum unit = GL_TEXTURE0) const override;
	GLuint add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions = {});

private:
	glTexture m_atlas;
	GLuint used {};
};