summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-04-13 02:10:44 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2023-04-13 02:10:44 +0100
commit1529d37d585ec38aee475519e444d2718a81fda5 (patch)
tree625216101e75e76a15f96ff43a4dae9d1c0256c4
parentFix submitting of integer values via vertex arrays (diff)
downloadilt-1529d37d585ec38aee475519e444d2718a81fda5.tar.bz2
ilt-1529d37d585ec38aee475519e444d2718a81fda5.tar.xz
ilt-1529d37d585ec38aee475519e444d2718a81fda5.zip
Add TextureAtlas class as an extension of Texture
-rw-r--r--gfx/models/texture.cpp32
-rw-r--r--gfx/models/texture.h19
2 files changed, 49 insertions, 2 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp
index efc76e1..a500fed 100644
--- a/gfx/models/texture.cpp
+++ b/gfx/models/texture.cpp
@@ -85,3 +85,35 @@ Texture::saveNormal(const glTexture & texture, const glm::ivec2 & size, const ch
{
save(texture, GL_BGR, GL_BYTE, size, 3, path, 2);
}
+
+TextureAtlas::TextureAtlas(GLsizei width, GLsizei height, GLuint count) : Texture(width, height, nullptr, {})
+{
+ glBindTexture(GL_TEXTURE_RECTANGLE, m_atlas);
+
+ glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+ glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA16UI, 2, static_cast<GLsizei>(count), 0, GL_RGBA_INTEGER,
+ GL_UNSIGNED_BYTE, nullptr);
+}
+
+void
+TextureAtlas::bind(GLenum unit) const
+{
+ Texture::bind(unit);
+ glActiveTexture(unit + 1);
+ glBindTexture(GL_TEXTURE_RECTANGLE, m_atlas);
+}
+
+GLuint
+TextureAtlas::add(glm::ivec2 position, glm::ivec2 size, void * data, TextureOptions)
+{
+ glTextureSubImage2D(m_texture, 0, position.x, position.y, size.x, size.y, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ struct Material {
+ glm::vec<2, uint16_t> position, size, unused1 {}, unused2 {};
+ } material {position, size};
+ glTextureSubImage2D(m_atlas, 0, 0, static_cast<GLsizei>(used), 2, 1, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, &material);
+ return ++used;
+}
diff --git a/gfx/models/texture.h b/gfx/models/texture.h
index ffc9a4a..7900f17 100644
--- a/gfx/models/texture.h
+++ b/gfx/models/texture.h
@@ -16,6 +16,9 @@ struct TextureOptions {
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 = {});
@@ -23,17 +26,29 @@ public:
static Cache<Texture, std::filesystem::path> cachedTexture;
- void bind(GLenum unit = GL_TEXTURE0) const;
+ virtual void bind(GLenum unit = GL_TEXTURE0) const;
void save(const glm::ivec2 & size, const char * path) const;
static void save(const glTexture &, const glm::ivec2 & size, const char * path);
static void saveDepth(const glTexture &, const glm::ivec2 & size, const char * path);
static void saveNormal(const glTexture &, const glm::ivec2 & size, const char * path);
-private:
+protected:
static void save(const glTexture &, GLenum, GLenum, const glm::ivec2 & size, unsigned short channels,
const char * path, short tgaFormat);
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 {};
+};