diff options
author | donbright <hugh.m.bright@gmail.com> | 2013-03-05 23:47:14 (GMT) |
---|---|---|
committer | donbright <hugh.m.bright@gmail.com> | 2013-03-05 23:47:14 (GMT) |
commit | 42f21c3a0850083d245aa3ac346a53e876f0679e (patch) | |
tree | facf41750e0fe27cc4cdaf352c9c8e687011d103 /src/OffscreenContextAll.hpp | |
parent | 422c668dcb538f181683ae51305bf8d3404f48d6 (diff) | |
parent | 4734172c3a16cc06b09e4d2131aa8e380bd0f226 (diff) |
Merge pull request #288 from openscad/issue11_2
Issue11 2
Diffstat (limited to 'src/OffscreenContextAll.hpp')
-rw-r--r-- | src/OffscreenContextAll.hpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/OffscreenContextAll.hpp b/src/OffscreenContextAll.hpp new file mode 100644 index 0000000..b845114 --- /dev/null +++ b/src/OffscreenContextAll.hpp @@ -0,0 +1,73 @@ +// Functions shared by OffscreenContext[platform].cc +// #include this directly after definition of struct OffscreenContext. + +#include <vector> +#include <ostream> + +void bind_offscreen_context(OffscreenContext *ctx) +{ + if (ctx) fbo_bind(ctx->fbo); +} + +/* + Capture framebuffer from OpenGL and write it to the given filename as PNG. + */ +bool save_framebuffer(OffscreenContext *ctx, const char *filename) +{ + std::ofstream fstream(filename,std::ios::out|std::ios::binary); + if (!fstream.is_open()) { + std::cerr << "Can't open file " << filename << " for writing"; + return false; + } else { + save_framebuffer(ctx, fstream); + fstream.close(); + } + return true; +} + +/*! + Capture framebuffer from OpenGL and write it to the given ostream. + Called by save_framebuffer() from platform-specific code. + */ +bool save_framebuffer_common(OffscreenContext *ctx, std::ostream &output) +{ + if (!ctx) return false; + int samplesPerPixel = 4; // R, G, B and A + std::vector<GLubyte> pixels(ctx->width * ctx->height * samplesPerPixel); + glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]); + + // Flip it vertically - images read from OpenGL buffers are upside-down + int rowBytes = samplesPerPixel * ctx->width; + + unsigned char *flippedBuffer = (unsigned char *)malloc(rowBytes * ctx->height); + if (!flippedBuffer) { + std::cerr << "Unable to allocate flipped buffer for corrected image."; + return 1; + } + flip_image(&pixels[0], flippedBuffer, samplesPerPixel, ctx->width, ctx->height); + + bool writeok = write_png(output, flippedBuffer, ctx->width, ctx->height); + + free(flippedBuffer); + + return writeok; +} + +// Called by create_offscreen_context() from platform-specific code. +OffscreenContext *create_offscreen_context_common(OffscreenContext *ctx) +{ + if (!ctx) return NULL; + GLenum err = glewInit(); // must come after Context creation and before FBO c$ + if (GLEW_OK != err) { + std::cerr << "Unable to init GLEW: " << glewGetErrorString(err) << "\n"; + return NULL; + } + + ctx->fbo = fbo_new(); + if (!fbo_init(ctx->fbo, ctx->width, ctx->height)) { + return NULL; + } + + return ctx; +} + |