From 22d8509fe8e9e7191b16a2d586f3e324afefa304 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sat, 23 Jun 2012 14:38:06 -0700 Subject: [PATCH] Fixed GL screen shot. --- src/bgfx.cpp | 38 +++++++++++++++++++++++++++----------- src/bgfx_p.h | 2 +- src/renderer_gl.cpp | 12 +++++++++++- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 4de4d336..30fe3bf0 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -102,35 +102,51 @@ namespace bgfx _result[15] = 1.0f; } - void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data) + void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip) { FILE* file = fopen(_filePath, "wb"); if ( NULL != file ) { + uint8_t type = _grayscale ? 3 : 2; + uint8_t bpp = _grayscale ? 8 : 32; + putc(0, file); putc(0, file); - putc(2, file); // uncompressed RGBA + putc(type, file); putc(0, file); putc(0, file); putc(0, file); putc(0, file); putc(0, file); putc(0, file); - putc(0, file); // x origin + putc(0, file); putc(0, file); - putc(0, file); // y origin - putc( _width&0xff, file); + putc(0, file); + putc(_width&0xff, file); putc( (_width>>8)&0xff, file); - putc( _height&0xff, file); + putc(_height&0xff, file); putc( (_height>>8)&0xff, file); - putc(32, file); + putc(bpp, file); putc(32, file); - uint8_t* data = (uint8_t*)_data; - for (uint32_t yy = 0; yy < _height; ++yy) + uint32_t dstPitch = _width*bpp/8; + if (_yflip) { - fwrite(data, _width*4, 1, file); - data += _pitch; + uint8_t* data = (uint8_t*)_src + dstPitch*_height - _srcPitch; + for (uint32_t yy = 0; yy < _height; ++yy) + { + fwrite(data, dstPitch, 1, file); + data -= _srcPitch; + } + } + else + { + uint8_t* data = (uint8_t*)_src; + for (uint32_t yy = 0; yy < _height; ++yy) + { + fwrite(data, dstPitch, 1, file); + data += _srcPitch; + } } fclose(file); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 267fbcde..74c77d86 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -122,7 +122,7 @@ namespace bgfx void fatal(bgfx::Fatal::Enum _code, const char* _format, ...); void release(Memory* _mem); - void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data); + void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale = false, bool _yflip = false); const char* getAttribName(Attrib::Enum _attr); bool renderFrame(); diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 270e55fa..627bbbf2 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -388,7 +388,17 @@ namespace bgfx #if BGFX_CONFIG_RENDERER_OPENGL void* data = g_realloc(NULL, m_resolution.m_width*m_resolution.m_height*4); glReadPixels(0, 0, m_resolution.m_width, m_resolution.m_height, GL_RGBA, GL_UNSIGNED_BYTE, data); - saveTga( (const char*)_mem->data, m_resolution.m_width, m_resolution.m_height, m_resolution.m_width*4, data); + + uint8_t* rgba = (uint8_t*)data; + for (uint32_t ii = 0, num = m_resolution.m_width*m_resolution.m_height; ii < num; ++ii) + { + uint8_t temp = rgba[0]; + rgba[0] = rgba[2]; + rgba[2] = temp; + rgba += 4; + } + + saveTga( (const char*)_mem->data, m_resolution.m_width, m_resolution.m_height, m_resolution.m_width*4, data, false, true); g_free(data); #endif // BGFX_CONFIG_RENDERER_OPENGL }