mirror of https://github.com/vitalif/openscad
refactor to reduce duplicate code
parent
395628cb4e
commit
544a8e5b79
|
@ -4,31 +4,12 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include "fbo.h"
|
||||||
|
|
||||||
struct OffscreenContext *create_offscreen_context(int w, int h);
|
struct OffscreenContext *create_offscreen_context(int w, int h);
|
||||||
bool teardown_offscreen_context(OffscreenContext *ctx);
|
bool teardown_offscreen_context(OffscreenContext *ctx);
|
||||||
|
bool save_framebuffer(OffscreenContext *ctx, const char * filename);
|
||||||
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output);
|
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output);
|
||||||
std::string offscreen_context_getinfo(OffscreenContext *ctx);
|
std::string offscreen_context_getinfo(OffscreenContext *ctx);
|
||||||
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
inline bool save_framebuffer(OffscreenContext *ctx, const char *filename)
|
|
||||||
{
|
|
||||||
std::ofstream fstream(filename);
|
|
||||||
if (!fstream.is_open()) {
|
|
||||||
std::cerr << "Can't open file " << filename << " for writing";
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
save_framebuffer(ctx, fstream);
|
|
||||||
fstream.close();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -19,6 +19,8 @@ struct OffscreenContext
|
||||||
fbo_t *fbo;
|
fbo_t *fbo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include "OffscreenContextAll.hpp"
|
||||||
|
|
||||||
std::string offscreen_context_getinfo(OffscreenContext *ctx)
|
std::string offscreen_context_getinfo(OffscreenContext *ctx)
|
||||||
{
|
{
|
||||||
std::stringstream out;
|
std::stringstream out;
|
||||||
|
|
|
@ -62,6 +62,8 @@ struct OffscreenContext
|
||||||
fbo_t *fbo;
|
fbo_t *fbo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include "OffscreenContextAll.hpp"
|
||||||
|
|
||||||
void offscreen_context_init(OffscreenContext &ctx, int width, int height)
|
void offscreen_context_init(OffscreenContext &ctx, int width, int height)
|
||||||
{
|
{
|
||||||
ctx.width = width;
|
ctx.width = width;
|
||||||
|
@ -296,30 +298,9 @@ bool teardown_offscreen_context(OffscreenContext *ctx)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
Capture framebuffer from OpenGL and write it to the given ostream
|
|
||||||
*/
|
|
||||||
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
|
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
|
||||||
{
|
{
|
||||||
glXSwapBuffers(ctx->xdisplay, ctx->xwindow);
|
glXSwapBuffers(ctx->xdisplay, ctx->xwindow);
|
||||||
if (!ctx) return false;
|
return save_framebuffer_common(ctx, output);
|
||||||
int samplesPerPixel = 4; // R, G, B and A
|
|
||||||
GLubyte pixels[ctx->width * ctx->height * samplesPerPixel];
|
|
||||||
glReadPixels(0, 0, ctx->width, ctx->height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
cerr << "Unable to allocate flipped buffer for corrected image.";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
flip_image(pixels, flippedBuffer, samplesPerPixel, ctx->width, ctx->height);
|
|
||||||
|
|
||||||
bool writeok = write_png(output, flippedBuffer, ctx->width, ctx->height);
|
|
||||||
|
|
||||||
free(flippedBuffer);
|
|
||||||
|
|
||||||
return writeok;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,8 @@ struct OffscreenContext
|
||||||
fbo_t *fbo;
|
fbo_t *fbo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include "OffscreenContextAll.hpp"
|
||||||
|
|
||||||
void offscreen_context_init(OffscreenContext &ctx, int width, int height)
|
void offscreen_context_init(OffscreenContext &ctx, int width, int height)
|
||||||
{
|
{
|
||||||
ctx.window = (HWND)NULL;
|
ctx.window = (HWND)NULL;
|
||||||
|
@ -214,46 +216,10 @@ bool teardown_offscreen_context(OffscreenContext *ctx)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
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);
|
|
||||||
if (!fstream.is_open()) {
|
|
||||||
PRINTB("Can't open file \"%s\" for writing", filename);
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
save_framebuffer(ctx, fstream);
|
|
||||||
fstream.close();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Capture framebuffer from OpenGL and write it to the given filename as PNG.
|
|
||||||
*/
|
|
||||||
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
|
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
|
||||||
{
|
{
|
||||||
|
if (!ctx) return false;
|
||||||
wglSwapLayerBuffers( ctx->dev_context, WGL_SWAP_MAIN_PLANE );
|
wglSwapLayerBuffers( ctx->dev_context, WGL_SWAP_MAIN_PLANE );
|
||||||
if (!ctx || !filename) return false;
|
return save_framebuffer_common( ctx, output );
|
||||||
int samplesPerPixel = 4; // R, G, B and A
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue