From 867daf817bba83ab22c5e04409c4afb1075dd158 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 4 Dec 2022 17:00:54 +0000 Subject: Support saving a depth texture --- gfx/gl/shadowMapper.cpp | 1 - gfx/models/texture.cpp | 23 +++++++++++++++++++---- gfx/models/texture.h | 4 ++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index a74f3dd..1169524 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -51,7 +51,6 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir) const scene.shadows(*this); glCullFace(GL_BACK); - // Texture::saveDepth(depthMap, size, "/tmp/depth.tga"); return lightViewProjection; } diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 313b3fa..645920f 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -41,15 +41,30 @@ Texture::bind(GLenum unit) const } void -Texture::save(const glTexture & texture, const glm::ivec2 & size, const char * path) +Texture::save(const glTexture & texture, GLenum format, const glm::ivec2 & size, unsigned short channels, + const char * path, short tgaFormat) { - std::vector buffer(static_cast(size.x * size.y * 3)); - glGetTextureImage(texture, 0, GL_BGR, GL_UNSIGNED_BYTE, static_cast(buffer.size()), buffer.data()); + using PixelData = std::vector; + PixelData buffer(static_cast(size.x * size.y * channels)); + glGetTextureImage(texture, 0, format, GL_UNSIGNED_BYTE, static_cast(buffer.size()), buffer.data()); auto out = open(path, O_WRONLY | O_CREAT, 0660); - short TGAhead[] = {0, 2, 0, 0, 0, 0, static_cast(size.x), static_cast(size.y), 24}; + const short TGAhead[] = {0, tgaFormat, 0, 0, 0, 0, static_cast(size.x), static_cast(size.y), + static_cast(8 * channels)}; std::ignore = write(out, &TGAhead, sizeof(TGAhead)); std::ignore = write(out, buffer.data(), buffer.size()); std::ignore = ftruncate(out, static_cast(buffer.size() + sizeof(TGAhead))); close(out); } + +void +Texture::save(const glTexture & texture, const glm::ivec2 & size, const char * path) +{ + save(texture, GL_BGR, size, 3, path, 2); +} + +void +Texture::saveDepth(const glTexture & texture, const glm::ivec2 & size, const char * path) +{ + save(texture, GL_DEPTH_COMPONENT, size, 1, path, 3); +} diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 09538b3..de532a5 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -19,7 +19,11 @@ public: void bind(GLenum unit = GL_TEXTURE0) 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); private: + static void save(const glTexture &, GLenum, const glm::ivec2 & size, unsigned short channels, const char * path, + short tgaFormat); + glTexture m_texture; }; -- cgit v1.2.3