erebus

changeset 29:fb20e3855740

write the rendered image when pressing '`' and on exit.
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 06 Jun 2014 15:08:09 +0300
parents 4a0a288ffb27
children e78f68d03ae9
files Makefile liberebus/src/rt.cc src/main.cc
diffstat 3 files changed, 32 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Fri Jun 06 14:39:40 2014 +0300
     1.2 +++ b/Makefile	Fri Jun 06 15:08:09 2014 +0300
     1.3 @@ -5,7 +5,7 @@
     1.4  
     1.5  CFLAGS = -pedantic -Wall -g -Iliberebus/src
     1.6  CXXFLAGS = -std=c++11 $(CFLAGS)
     1.7 -LDFLAGS = -Lliberebus -Wl,-rpath=liberebus $(libgl_$(sys)) -lm -lerebus -lvmath
     1.8 +LDFLAGS = -Lliberebus -Wl,-rpath=liberebus $(libgl_$(sys)) -lm -lerebus -lvmath -limago
     1.9  
    1.10  libgl_unix = -lGL -lGLU -lglut -lGLEW
    1.11  libgl_mac = -framework OpenGL -framework GLUT -lGLEW
     2.1 --- a/liberebus/src/rt.cc	Fri Jun 06 14:39:40 2014 +0300
     2.2 +++ b/liberebus/src/rt.cc	Fri Jun 06 15:08:09 2014 +0300
     2.3 @@ -55,5 +55,7 @@
     2.4  
     2.5  		res += ray_trace(ctx, sample_ray, iter + 1) * color;
     2.6  	}
     2.7 +
     2.8 +	res.w = 1.0;
     2.9  	return res;
    2.10  }
     3.1 --- a/src/main.cc	Fri Jun 06 14:39:40 2014 +0300
     3.2 +++ b/src/main.cc	Fri Jun 06 15:08:09 2014 +0300
     3.3 @@ -1,8 +1,10 @@
     3.4  #include <stdio.h>
     3.5  #include <stdlib.h>
     3.6  #include <assert.h>
     3.7 +#include <signal.h>
     3.8  #include <vector>
     3.9  #include <chrono>
    3.10 +#include <imago2.h>
    3.11  #include "opengl.h"
    3.12  #include "erebus.h"
    3.13  
    3.14 @@ -16,6 +18,7 @@
    3.15  static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
    3.16  static void idle();
    3.17  static void display();
    3.18 +static void save_image(const char *fname = 0);
    3.19  static void reshape(int x, int y);
    3.20  static void keyb(unsigned char key, int x, int y);
    3.21  static void keyb_up(unsigned char key, int x, int y);
    3.22 @@ -24,6 +27,7 @@
    3.23  static void sball_button(int bn, int st);
    3.24  static void sball_motion(int x, int y, int z);
    3.25  static int next_pow2(int x);
    3.26 +static void sighandler(int s);
    3.27  
    3.28  static int width, height, rtex_width, rtex_height;
    3.29  static unsigned int rtex;
    3.30 @@ -58,6 +62,11 @@
    3.31  		return 1;
    3.32  	}
    3.33  	atexit(cleanup);
    3.34 +	signal(SIGINT, sighandler);
    3.35 +	signal(SIGSEGV, sighandler);
    3.36 +	signal(SIGILL, sighandler);
    3.37 +	signal(SIGTERM, sighandler);
    3.38 +	signal(SIGFPE, sighandler);
    3.39  
    3.40  	glutMainLoop();
    3.41  }
    3.42 @@ -93,6 +102,7 @@
    3.43  
    3.44  static void cleanup()
    3.45  {
    3.46 +	save_image("final.png");
    3.47  	erb_destroy(erb);
    3.48  }
    3.49  
    3.50 @@ -172,6 +182,15 @@
    3.51  	assert(glGetError() == GL_NO_ERROR);
    3.52  }
    3.53  
    3.54 +static void save_image(const char *fname)
    3.55 +{
    3.56 +	float *fb = erb_get_framebuffer(erb);
    3.57 +
    3.58 +	if(img_save_pixels(fname ? fname : "output.png", fb, width, height, IMG_FMT_RGBAF) == -1) {
    3.59 +		fprintf(stderr, "failed to save image\n");
    3.60 +	}
    3.61 +}
    3.62 +
    3.63  static void reshape(int x, int y)
    3.64  {
    3.65  	glViewport(0, 0, x, y);
    3.66 @@ -193,6 +212,11 @@
    3.67  		glutIdleFunc(idle);
    3.68  		erb_begin_frame(erb, 0);
    3.69  		break;
    3.70 +
    3.71 +	case '`':
    3.72 +		printf("saving image.\n");
    3.73 +		save_image();
    3.74 +		break;
    3.75  	}
    3.76  
    3.77  	if(erb_input_keyboard(erb, key, true)) {
    3.78 @@ -243,3 +267,8 @@
    3.79  	}
    3.80  	return res;
    3.81  }
    3.82 +
    3.83 +static void sighandler(int s)
    3.84 +{
    3.85 +	exit(0);
    3.86 +}