diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-11-18 20:24:48 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-11-18 20:24:48 +0000 |
commit | 98e844f27d7d84ef823c822d11306c17210cb5c4 (patch) | |
tree | 7d634a94cdf5ecf40a823b640e6ccc8184af151e /gfx | |
parent | Move OpenGL context behaviour tests into their own app (diff) | |
download | ilt-98e844f27d7d84ef823c822d11306c17210cb5c4.tar.bz2 ilt-98e844f27d7d84ef823c822d11306c17210cb5c4.tar.xz ilt-98e844f27d7d84ef823c822d11306c17210cb5c4.zip |
Simplified texture save
Read directly from texture into file buffer, no temporary framebuffer
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/models/texture.cpp | 27 |
1 files changed, 8 insertions, 19 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index ab1a5fe..313b3fa 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -43,24 +43,13 @@ Texture::bind(GLenum unit) const 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));
+ glGetTextureImage(texture, 0, GL_BGR, GL_UNSIGNED_BYTE, static_cast<GLsizei>(buffer.size()), 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);
}
|