summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
Diffstat (limited to 'gfx')
-rw-r--r--gfx/models/texture.cpp27
-rw-r--r--gfx/models/texture.h3
2 files changed, 30 insertions, 0 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp
index ab04f4f..ff38d0f 100644
--- a/gfx/models/texture.cpp
+++ b/gfx/models/texture.cpp
@@ -2,7 +2,9 @@
#include "glArrays.h"
#include <GL/glew.h>
#include <cache.h>
+#include <fcntl.h>
#include <gfx/image.h>
+#include <glm/geometric.hpp>
#include <resource.h>
#include <stb/stb_image.h>
@@ -36,3 +38,28 @@ Texture::bind(GLenum unit) const
glActiveTexture(unit);
glBindTexture(GL_TEXTURE_2D, m_texture);
}
+
+void
+Texture::save(const glTexture & texture, const glm::ivec2 & size, const char * path)
+{
+ GLint drawFboId = 0, readFboId = 0;
+ glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFboId);
+ glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFboId);
+ std::vector<unsigned char> buffer(static_cast<size_t>(size.x * size.y * 3));
+ {
+ glFrameBuffer tmp;
+ glBindFramebuffer(GL_FRAMEBUFFER, tmp);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
+ glReadPixels(0, 0, size.x, size.y, GL_BGR, GL_UNSIGNED_BYTE, buffer.data());
+ }
+ {
+ auto out = open(path, O_WRONLY | O_CREAT, 0660);
+ short TGAhead[] = {0, 2, 0, 0, 0, 0, static_cast<short>(size.x), static_cast<short>(size.y), 24};
+ std::ignore = write(out, &TGAhead, sizeof(TGAhead));
+ std::ignore = write(out, buffer.data(), buffer.size());
+ std::ignore = ftruncate(out, static_cast<off_t>(buffer.size() + sizeof(TGAhead)));
+ close(out);
+ }
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, static_cast<GLuint>(drawFboId));
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, static_cast<GLuint>(readFboId));
+}
diff --git a/gfx/models/texture.h b/gfx/models/texture.h
index 2bbf1f4..09538b3 100644
--- a/gfx/models/texture.h
+++ b/gfx/models/texture.h
@@ -3,6 +3,7 @@
#include <cache.h>
#include <filesystem>
#include <glArrays.h>
+#include <glm/fwd.hpp>
// IWYU pragma: no_forward_declare Cache
class Image;
@@ -17,6 +18,8 @@ public:
void bind(GLenum unit = GL_TEXTURE0) const;
+ static void save(const glTexture &, const glm::ivec2 & size, const char * path);
+
private:
glTexture m_texture;
};