# HG changeset patch # User John Tsiombikas # Date 1402056489 -10800 # Node ID fb20e3855740f1b3fefcc1b187bc466c343b81e1 # Parent 4a0a288ffb278110448f652591ad131f0f692ace write the rendered image when pressing '`' and on exit. diff -r 4a0a288ffb27 -r fb20e3855740 Makefile --- a/Makefile Fri Jun 06 14:39:40 2014 +0300 +++ b/Makefile Fri Jun 06 15:08:09 2014 +0300 @@ -5,7 +5,7 @@ CFLAGS = -pedantic -Wall -g -Iliberebus/src CXXFLAGS = -std=c++11 $(CFLAGS) -LDFLAGS = -Lliberebus -Wl,-rpath=liberebus $(libgl_$(sys)) -lm -lerebus -lvmath +LDFLAGS = -Lliberebus -Wl,-rpath=liberebus $(libgl_$(sys)) -lm -lerebus -lvmath -limago libgl_unix = -lGL -lGLU -lglut -lGLEW libgl_mac = -framework OpenGL -framework GLUT -lGLEW diff -r 4a0a288ffb27 -r fb20e3855740 liberebus/src/rt.cc --- a/liberebus/src/rt.cc Fri Jun 06 14:39:40 2014 +0300 +++ b/liberebus/src/rt.cc Fri Jun 06 15:08:09 2014 +0300 @@ -55,5 +55,7 @@ res += ray_trace(ctx, sample_ray, iter + 1) * color; } + + res.w = 1.0; return res; } diff -r 4a0a288ffb27 -r fb20e3855740 src/main.cc --- a/src/main.cc Fri Jun 06 14:39:40 2014 +0300 +++ b/src/main.cc Fri Jun 06 15:08:09 2014 +0300 @@ -1,8 +1,10 @@ #include #include #include +#include #include #include +#include #include "opengl.h" #include "erebus.h" @@ -16,6 +18,7 @@ static void update_rect(int x, int y, int xsz, int ysz, float *pixels); static void idle(); static void display(); +static void save_image(const char *fname = 0); static void reshape(int x, int y); static void keyb(unsigned char key, int x, int y); static void keyb_up(unsigned char key, int x, int y); @@ -24,6 +27,7 @@ static void sball_button(int bn, int st); static void sball_motion(int x, int y, int z); static int next_pow2(int x); +static void sighandler(int s); static int width, height, rtex_width, rtex_height; static unsigned int rtex; @@ -58,6 +62,11 @@ return 1; } atexit(cleanup); + signal(SIGINT, sighandler); + signal(SIGSEGV, sighandler); + signal(SIGILL, sighandler); + signal(SIGTERM, sighandler); + signal(SIGFPE, sighandler); glutMainLoop(); } @@ -93,6 +102,7 @@ static void cleanup() { + save_image("final.png"); erb_destroy(erb); } @@ -172,6 +182,15 @@ assert(glGetError() == GL_NO_ERROR); } +static void save_image(const char *fname) +{ + float *fb = erb_get_framebuffer(erb); + + if(img_save_pixels(fname ? fname : "output.png", fb, width, height, IMG_FMT_RGBAF) == -1) { + fprintf(stderr, "failed to save image\n"); + } +} + static void reshape(int x, int y) { glViewport(0, 0, x, y); @@ -193,6 +212,11 @@ glutIdleFunc(idle); erb_begin_frame(erb, 0); break; + + case '`': + printf("saving image.\n"); + save_image(); + break; } if(erb_input_keyboard(erb, key, true)) { @@ -243,3 +267,8 @@ } return res; } + +static void sighandler(int s) +{ + exit(0); +}