Skip to content

Commit 6ed9fee

Browse files
committed
Fixed bug where images wouldn't be properly exported
1 parent 249813e commit 6ed9fee

4 files changed

Lines changed: 6 additions & 9 deletions

File tree

src/graphics/ui.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,6 @@ void render_and_update(FunctionState& state, ViewState& view_state, unsigned int
342342
}
343343

344344
if (UI::CollapsingHeader("Export")) {
345-
if (UI::Button("Render High Precision Popup")) {
346-
view_state.wants_high_precision = true;
347-
}
348345
static int export_w = 1920;
349346
static int export_h = 1080;
350347

@@ -462,7 +459,7 @@ void render_and_update(FunctionState& state, ViewState& view_state, unsigned int
462459
else {
463460
if (ImGui::Button("Save to PNG")) {
464461
std::string filename = "high_precision_" + std::to_string(view_state.hp_width) + "x" + std::to_string(view_state.hp_height) + ".png";
465-
export_plot_to_png(view_state.hp_width,view_state.hp_height,"complex-plot-high-precision");
462+
export_plot_to_png(view_state.hp_cpu_buffer.data(),view_state.hp_width,view_state.hp_height,"complex-plot-high-precision.png");
466463
view_state.show_export_success = true;
467464
}
468465
}

src/interactions/export.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
#include <emscripten.h>
1111
#endif
1212

13-
void export_plot_to_png(const int width, const int height, const char* filename) {
14-
unsigned char* pixels = new unsigned char[width * height * 4];
15-
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
13+
void export_plot_to_png(unsigned char* pixels, const int width, const int height, const char* filename) {
1614
stbi_set_flip_vertically_on_load(true);
1715
stbi_write_png(filename, width, height, 4, pixels, width * 4);
18-
delete[] pixels;
1916
#ifdef __EMSCRIPTEN__
2017
EM_ASM_({
2118
const js_filename = UTF8ToString($0);

src/interactions/export.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#pragma once
2-
void export_plot_to_png(const int width, const int height, const char* filename);
2+
void export_plot_to_png(unsigned char* pixels, const int width, const int height, const char* filename);

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void export_to_png(AppContext* ctx, int target_width, int target_height, const c
124124
glGenTextures(1, &texture);
125125
glBindTexture(GL_TEXTURE_2D, texture);
126126
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, target_width, target_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
127+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
128+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127129
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
128130

129131
glGenRenderbuffers(1, &rbo);
@@ -141,6 +143,7 @@ void export_to_png(AppContext* ctx, int target_width, int target_height, const c
141143
if (ctx->function_state->is_3d) glEnable(GL_DEPTH_TEST);
142144
else glDisable(GL_DEPTH_TEST);
143145

146+
draw_scene(ctx, (float)target_width, (float)target_height);
144147

145148
glPixelStorei(GL_PACK_ALIGNMENT, 1);
146149
unsigned char* pixels = new unsigned char[target_width * target_height * 4];

0 commit comments

Comments
 (0)