clray

diff src/clray.cc @ 47:30bf84881553

added interactive controls for turning shadows/reflections on and off as well as selecting maximum ray tracing iterations
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 31 Aug 2010 01:47:27 +0100
parents b5eb404af481
children 1ae68d46cfda
line diff
     1.1 --- a/src/clray.cc	Sun Aug 29 14:02:37 2010 +0100
     1.2 +++ b/src/clray.cc	Tue Aug 31 01:47:27 2010 +0100
     1.3 @@ -19,6 +19,7 @@
     1.4  void keyb(unsigned char key, int x, int y);
     1.5  void mouse(int bn, int status, int x, int y);
     1.6  void motion(int x, int y);
     1.7 +bool capture(const char *namefmt);
     1.8  bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
     1.9  
    1.10  static int xsz, ysz;
    1.11 @@ -218,15 +219,26 @@
    1.12  	set_framebuffer(fb, x, y);*/
    1.13  }
    1.14  
    1.15 +void idle()
    1.16 +{
    1.17 +	need_update = true;
    1.18 +	glutPostRedisplay();
    1.19 +}
    1.20 +
    1.21  void keyb(unsigned char key, int x, int y)
    1.22  {
    1.23  	switch(key) {
    1.24  	case 27:
    1.25  		exit(0);
    1.26  
    1.27 -	case 'r':
    1.28 -		need_update = true;
    1.29 -		glutPostRedisplay();
    1.30 +	case '\b':
    1.31 +		{
    1.32 +			static bool busyloop;
    1.33 +
    1.34 +			busyloop = !busyloop;
    1.35 +			printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
    1.36 +			glutIdleFunc(busyloop ? idle : 0);
    1.37 +		}
    1.38  		break;
    1.39  
    1.40  	case 'd':
    1.41 @@ -253,6 +265,54 @@
    1.42  		}
    1.43  		break;
    1.44  
    1.45 +	case 's':
    1.46 +		{
    1.47 +			bool shadows = get_render_option_bool(ROPT_SHAD);
    1.48 +			shadows = !shadows;
    1.49 +			printf("%s shadows\n", shadows ? "enabling" : "disabling");
    1.50 +			set_render_option(ROPT_SHAD, shadows);
    1.51 +			need_update = true;
    1.52 +			glutPostRedisplay();
    1.53 +		}
    1.54 +		break;
    1.55 +
    1.56 +	case 'r':
    1.57 +		{
    1.58 +			bool refl = get_render_option_bool(ROPT_REFL);
    1.59 +			refl = !refl;
    1.60 +			printf("%s reflections\n", refl ? "enabling" : "disabling");
    1.61 +			set_render_option(ROPT_REFL, refl);
    1.62 +			need_update = true;
    1.63 +			glutPostRedisplay();
    1.64 +		}
    1.65 +		break;
    1.66 +
    1.67 +	case ']':
    1.68 +		{
    1.69 +			int iter = get_render_option_int(ROPT_ITER);
    1.70 +			printf("setting max iterations: %d\n", iter + 1);
    1.71 +			set_render_option(ROPT_ITER, iter + 1);
    1.72 +			need_update = true;
    1.73 +			glutPostRedisplay();
    1.74 +		}
    1.75 +		break;
    1.76 +
    1.77 +	case '[':
    1.78 +		{
    1.79 +			int iter = get_render_option_int(ROPT_ITER);
    1.80 +			if(iter-- > 0) {
    1.81 +				printf("setting max iterations: %d\n", iter);
    1.82 +				set_render_option(ROPT_ITER, iter);
    1.83 +				need_update = true;
    1.84 +				glutPostRedisplay();
    1.85 +			}
    1.86 +		}
    1.87 +		break;
    1.88 +
    1.89 +	case '`':
    1.90 +		capture("shot%03d.ppm");
    1.91 +		break;
    1.92 +
    1.93  	default:
    1.94  		break;
    1.95  	}
    1.96 @@ -301,6 +361,26 @@
    1.97  	}
    1.98  }
    1.99  
   1.100 +bool capture(const char *namefmt)
   1.101 +{
   1.102 +	static int num;
   1.103 +	char fname[256];
   1.104 +
   1.105 +	num++;
   1.106 +	snprintf(fname, sizeof fname, namefmt, num);
   1.107 +	printf("saving image %s\n", fname);
   1.108 +
   1.109 +	float *pixels = new float[4 * xsz * ysz];
   1.110 +	glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
   1.111 +
   1.112 +	bool res = write_ppm("shot.ppm", pixels, xsz, ysz);
   1.113 +	if(!res) {
   1.114 +		num--;
   1.115 +	}
   1.116 +	delete [] pixels;
   1.117 +	return res;
   1.118 +}
   1.119 +
   1.120  bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
   1.121  {
   1.122  	FILE *fp;