| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <fstream> |
| |
| #include "utils.h" |
| #include "glcontext.h" |
| |
| |
| using namespace std; |
| |
| |
| Vector Utils::get_frame_size() { |
| int vp[4] = {}; |
| glGetIntegerv(GL_VIEWPORT, vp); |
| return Vector((Real)vp[2], (Real)vp[3]); |
| } |
| |
| void Utils::save_rgba( |
| const void *buffer, |
| int width, |
| int height, |
| bool flip, |
| const string &filename ) |
| { |
| |
| ofstream f(("results/" + filename).c_str(), ofstream::out | ofstream::trunc | ofstream::binary); |
| |
| |
| unsigned char targa_header[] = { |
| 0, |
| 0, |
| 2, |
| 0, 0, 0, 0, 0, |
| 0, 0, |
| 0, 0, |
| (unsigned char)(width & 0xff), |
| (unsigned char)(width >> 8), |
| (unsigned char)(height & 0xff), |
| (unsigned char)(height >> 8), |
| 32, |
| 0 |
| }; |
| f.write((char*)targa_header, sizeof(targa_header)); |
| |
| |
| if (flip) { |
| int line_size = 4*width; |
| const char *end = (char*)buffer; |
| const char *current = end + height*line_size; |
| while(current > end) { |
| current -= line_size; |
| f.write(current, line_size); |
| } |
| } else { |
| f.write((const char*)buffer, 4*width*height); |
| } |
| } |
| |
| void Utils::save_viewport(const string &filename) { |
| glFinish(); |
| |
| GLint vp[4] = {}; |
| glGetIntegerv(GL_VIEWPORT, vp); |
| |
| GLint draw_buffer = 0, read_buffer = 0; |
| glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_buffer); |
| glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &read_buffer); |
| if (draw_buffer != read_buffer) { |
| glBindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint)read_buffer); |
| glBindFramebuffer(GL_READ_FRAMEBUFFER, (GLuint)draw_buffer); |
| glBlitFramebuffer(vp[0], vp[1], vp[2], vp[3], vp[0], vp[1], vp[2], vp[3], GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| glFinish(); |
| glBindFramebuffer(GL_DRAW_FRAMEBUFFER, (GLuint)draw_buffer); |
| glBindFramebuffer(GL_READ_FRAMEBUFFER, (GLuint)read_buffer); |
| } |
| |
| char *buffer = new char[vp[2]*vp[3]*4]; |
| glReadPixels(vp[0], vp[1], vp[2], vp[3], GL_BGRA, GL_UNSIGNED_BYTE, buffer); |
| |
| |
| save_rgba(buffer, vp[2], vp[3], false, filename); |
| delete buffer; |
| } |
| |
| void Utils::save_surface(const Surface &surface, const string &filename) { |
| unsigned char *buffer = new unsigned char[4*surface.count()]; |
| unsigned char *j = buffer; |
| for(Color *i = surface.data, *end = i + surface.count(); i != end; ++i, j += 4) { |
| j[0] = (unsigned char)roundf(max(0.f, min(1.f, i->b))*255.f); |
| j[1] = (unsigned char)roundf(max(0.f, min(1.f, i->g))*255.f); |
| j[2] = (unsigned char)roundf(max(0.f, min(1.f, i->r))*255.f); |
| j[3] = (unsigned char)roundf(max(0.f, min(1.f, i->a))*255.f); |
| } |
| save_rgba(buffer, surface.width, surface.height, false, filename); |
| delete buffer; |
| } |