Unbreak PNG export.

Before this commit, on Win32 the toolbar was visible, and
the framebuffer was flipped, whereas on other platforms just a black
image was exported.
This commit is contained in:
whitequark 2016-08-13 09:55:37 +00:00
parent 6e860fb148
commit efd358d734

View File

@ -1089,6 +1089,11 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename,
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void SolveSpaceUI::ExportAsPngTo(const std::string &filename) { void SolveSpaceUI::ExportAsPngTo(const std::string &filename) {
#if !defined(HEADLESS) #if !defined(HEADLESS)
// Somewhat hacky way to invoke glReadPixels without dragging in all OpenGL headers.
OpenGl1Renderer canvas = {};
canvas.camera = SS.GW.GetCamera();
std::shared_ptr<Pixmap> screenshot;
// No guarantee that the back buffer contains anything valid right now, // No guarantee that the back buffer contains anything valid right now,
// so repaint the scene. And hide the toolbar too. // so repaint the scene. And hide the toolbar too.
bool prevShowToolbar = SS.showToolbar; bool prevShowToolbar = SS.showToolbar;
@ -1097,17 +1102,22 @@ void SolveSpaceUI::ExportAsPngTo(const std::string &filename) {
GlOffscreen offscreen; GlOffscreen offscreen;
offscreen.Render((int)SS.GW.width, (int)SS.GW.height, [&] { offscreen.Render((int)SS.GW.width, (int)SS.GW.height, [&] {
SS.GW.Paint(); SS.GW.Paint();
screenshot = canvas.ReadFrame();
}); });
#else
SS.GW.Paint();
screenshot = canvas.ReadFrame();
#endif #endif
SS.showToolbar = prevShowToolbar; SS.showToolbar = prevShowToolbar;
// Somewhat hacky way to invoke glReadPixels without dragging in all OpenGL headers. #if defined(WIN32) || defined(HAVE_GTK)
OpenGl1Renderer canvas = {}; bool flip = true;
canvas.camera = SS.GW.GetCamera(); #else
std::shared_ptr<Pixmap> screenshot = canvas.ReadFrame(); bool flip = false;
#endif
FILE *f = ssfopen(filename, "wb"); FILE *f = ssfopen(filename, "wb");
if(!f || !screenshot->WritePng(f, /*flip=*/FLIP_FRAMEBUFFER)) { if(!f || !screenshot->WritePng(f, flip)) {
Error("Couldn't write to '%s'", filename.c_str()); Error("Couldn't write to '%s'", filename.c_str());
} }
if(f) fclose(f); if(f) fclose(f);