clray

changeset 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 154f03802aea
files rt.cl src/clray.cc src/rt.cc src/rt.h
diffstat 4 files changed, 205 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/rt.cl	Sun Aug 29 14:02:37 2010 +0100
     1.2 +++ b/rt.cl	Tue Aug 31 01:47:27 2010 +0100
     1.3 @@ -6,7 +6,7 @@
     1.4  	int xsz, ysz;
     1.5  	int num_faces, num_lights;
     1.6  	int max_iter;
     1.7 -	int kd_depth;
     1.8 +	int cast_shadows;
     1.9  };
    1.10  
    1.11  struct Vertex {
    1.12 @@ -53,6 +53,7 @@
    1.13  	int num_lights;
    1.14  	global const struct Material *matlib;
    1.15  	//global const struct KDNode *kdtree;
    1.16 +	bool cast_shadows;
    1.17  };
    1.18  
    1.19  struct AABBox {
    1.20 @@ -104,7 +105,7 @@
    1.21  	scn.lights = lights;
    1.22  	scn.num_lights = rinf->num_lights;
    1.23  	scn.matlib = matlib;
    1.24 -	//scn.kdtree_img = kdtree_img;
    1.25 +	scn.cast_shadows = rinf->cast_shadows;
    1.26  
    1.27  	struct Ray ray = primrays[idx];
    1.28  	transform_ray(&ray, xform, invtrans);
    1.29 @@ -113,7 +114,7 @@
    1.30  	float4 energy = (float4)(1.0, 1.0, 1.0, 0.0);
    1.31  	int iter = 0;
    1.32  
    1.33 -	while(iter++ < rinf->max_iter && mean(energy) > MIN_ENERGY) {
    1.34 +	while(iter++ <= rinf->max_iter && mean(energy) > MIN_ENERGY) {
    1.35  		struct SurfPoint sp;
    1.36  		if(find_intersection(ray, &scn, &sp, kdtree_img)) {
    1.37  			pixel += shade(ray, &scn, &sp, kdtree_img) * energy;
    1.38 @@ -156,7 +157,7 @@
    1.39  		shadowray.origin = sp->pos;
    1.40  		shadowray.dir = ldir;
    1.41  
    1.42 -		if(!find_intersection(shadowray, scn, 0, kdimg)) {
    1.43 +		if(!scn->cast_shadows || !find_intersection(shadowray, scn, 0, kdimg)) {
    1.44  			ldir = normalize(ldir);
    1.45  			float4 vdir = -ray.dir;
    1.46  			vdir.x = native_divide(vdir.x, RAY_MAG);
     2.1 --- a/src/clray.cc	Sun Aug 29 14:02:37 2010 +0100
     2.2 +++ b/src/clray.cc	Tue Aug 31 01:47:27 2010 +0100
     2.3 @@ -19,6 +19,7 @@
     2.4  void keyb(unsigned char key, int x, int y);
     2.5  void mouse(int bn, int status, int x, int y);
     2.6  void motion(int x, int y);
     2.7 +bool capture(const char *namefmt);
     2.8  bool write_ppm(const char *fname, float *fb, int xsz, int ysz);
     2.9  
    2.10  static int xsz, ysz;
    2.11 @@ -218,15 +219,26 @@
    2.12  	set_framebuffer(fb, x, y);*/
    2.13  }
    2.14  
    2.15 +void idle()
    2.16 +{
    2.17 +	need_update = true;
    2.18 +	glutPostRedisplay();
    2.19 +}
    2.20 +
    2.21  void keyb(unsigned char key, int x, int y)
    2.22  {
    2.23  	switch(key) {
    2.24  	case 27:
    2.25  		exit(0);
    2.26  
    2.27 -	case 'r':
    2.28 -		need_update = true;
    2.29 -		glutPostRedisplay();
    2.30 +	case '\b':
    2.31 +		{
    2.32 +			static bool busyloop;
    2.33 +
    2.34 +			busyloop = !busyloop;
    2.35 +			printf("%s busy-looping\n", busyloop ? "WARNING: enabling" : "disabling");
    2.36 +			glutIdleFunc(busyloop ? idle : 0);
    2.37 +		}
    2.38  		break;
    2.39  
    2.40  	case 'd':
    2.41 @@ -253,6 +265,54 @@
    2.42  		}
    2.43  		break;
    2.44  
    2.45 +	case 's':
    2.46 +		{
    2.47 +			bool shadows = get_render_option_bool(ROPT_SHAD);
    2.48 +			shadows = !shadows;
    2.49 +			printf("%s shadows\n", shadows ? "enabling" : "disabling");
    2.50 +			set_render_option(ROPT_SHAD, shadows);
    2.51 +			need_update = true;
    2.52 +			glutPostRedisplay();
    2.53 +		}
    2.54 +		break;
    2.55 +
    2.56 +	case 'r':
    2.57 +		{
    2.58 +			bool refl = get_render_option_bool(ROPT_REFL);
    2.59 +			refl = !refl;
    2.60 +			printf("%s reflections\n", refl ? "enabling" : "disabling");
    2.61 +			set_render_option(ROPT_REFL, refl);
    2.62 +			need_update = true;
    2.63 +			glutPostRedisplay();
    2.64 +		}
    2.65 +		break;
    2.66 +
    2.67 +	case ']':
    2.68 +		{
    2.69 +			int iter = get_render_option_int(ROPT_ITER);
    2.70 +			printf("setting max iterations: %d\n", iter + 1);
    2.71 +			set_render_option(ROPT_ITER, iter + 1);
    2.72 +			need_update = true;
    2.73 +			glutPostRedisplay();
    2.74 +		}
    2.75 +		break;
    2.76 +
    2.77 +	case '[':
    2.78 +		{
    2.79 +			int iter = get_render_option_int(ROPT_ITER);
    2.80 +			if(iter-- > 0) {
    2.81 +				printf("setting max iterations: %d\n", iter);
    2.82 +				set_render_option(ROPT_ITER, iter);
    2.83 +				need_update = true;
    2.84 +				glutPostRedisplay();
    2.85 +			}
    2.86 +		}
    2.87 +		break;
    2.88 +
    2.89 +	case '`':
    2.90 +		capture("shot%03d.ppm");
    2.91 +		break;
    2.92 +
    2.93  	default:
    2.94  		break;
    2.95  	}
    2.96 @@ -301,6 +361,26 @@
    2.97  	}
    2.98  }
    2.99  
   2.100 +bool capture(const char *namefmt)
   2.101 +{
   2.102 +	static int num;
   2.103 +	char fname[256];
   2.104 +
   2.105 +	num++;
   2.106 +	snprintf(fname, sizeof fname, namefmt, num);
   2.107 +	printf("saving image %s\n", fname);
   2.108 +
   2.109 +	float *pixels = new float[4 * xsz * ysz];
   2.110 +	glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, pixels);
   2.111 +
   2.112 +	bool res = write_ppm("shot.ppm", pixels, xsz, ysz);
   2.113 +	if(!res) {
   2.114 +		num--;
   2.115 +	}
   2.116 +	delete [] pixels;
   2.117 +	return res;
   2.118 +}
   2.119 +
   2.120  bool write_ppm(const char *fname, float *fb, int xsz, int ysz)
   2.121  {
   2.122  	FILE *fp;
     3.1 --- a/src/rt.cc	Sun Aug 29 14:02:37 2010 +0100
     3.2 +++ b/src/rt.cc	Tue Aug 31 01:47:27 2010 +0100
     3.3 @@ -2,6 +2,7 @@
     3.4  #include <string.h>
     3.5  #include <math.h>
     3.6  #include <assert.h>
     3.7 +#include "rt.h"
     3.8  #include "ogl.h"
     3.9  #include "ocl.h"
    3.10  #include "scene.h"
    3.11 @@ -28,7 +29,7 @@
    3.12  	int xsz, ysz;
    3.13  	int num_faces, num_lights;
    3.14  	int max_iter;
    3.15 -	int kd_depth;
    3.16 +	int cast_shadows;
    3.17  };
    3.18  
    3.19  struct Ray {
    3.20 @@ -39,6 +40,7 @@
    3.21  	float pos[4], color[4];
    3.22  };
    3.23  
    3.24 +static void update_render_info();
    3.25  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
    3.26  static float *create_kdimage(const KDNodeGPU *kdtree, int num_nodes, int *xsz_ret, int *ysz_ret);
    3.27  
    3.28 @@ -53,6 +55,7 @@
    3.29  
    3.30  
    3.31  static RendInfo rinf;
    3.32 +static int saved_iter_val;
    3.33  
    3.34  static long timing_sample_sum;
    3.35  static long num_timing_samples;
    3.36 @@ -68,8 +71,8 @@
    3.37  	rinf.ysz = ysz;
    3.38  	rinf.num_faces = scn->get_num_faces();
    3.39  	rinf.num_lights = sizeof lightlist / sizeof *lightlist;
    3.40 -	rinf.max_iter = 6;
    3.41 -	rinf.kd_depth = kdtree_depth(scn->kdtree);
    3.42 +	rinf.max_iter = saved_iter_val = 6;
    3.43 +	rinf.cast_shadows = true;
    3.44  
    3.45  	/* calculate primary rays */
    3.46  	prim_rays = new Ray[xsz * ysz];
    3.47 @@ -279,6 +282,101 @@
    3.48  	unmap_mem_buffer(mbuf_invtrans);
    3.49  }
    3.50  
    3.51 +void set_render_option(int opt, bool val)
    3.52 +{
    3.53 +	switch(opt) {
    3.54 +	case ROPT_ITER:
    3.55 +	case ROPT_REFL:
    3.56 +		rinf.max_iter = val ? saved_iter_val : 0;
    3.57 +		break;
    3.58 +
    3.59 +	case ROPT_SHAD:
    3.60 +		rinf.cast_shadows = val;
    3.61 +		break;
    3.62 +
    3.63 +	default:
    3.64 +		return;
    3.65 +	}
    3.66 +
    3.67 +	update_render_info();
    3.68 +}
    3.69 +
    3.70 +void set_render_option(int opt, int val)
    3.71 +{
    3.72 +	switch(opt) {
    3.73 +	case ROPT_ITER:
    3.74 +		rinf.max_iter = saved_iter_val = val;
    3.75 +		break;
    3.76 +
    3.77 +	case ROPT_SHAD:
    3.78 +		rinf.cast_shadows = val;
    3.79 +		break;
    3.80 +
    3.81 +	case ROPT_REFL:
    3.82 +		rinf.max_iter = val ? saved_iter_val : 0;
    3.83 +		break;
    3.84 +
    3.85 +	default:
    3.86 +		return;
    3.87 +	}
    3.88 +
    3.89 +	update_render_info();
    3.90 +}
    3.91 +
    3.92 +void set_render_option(int opt, float val)
    3.93 +{
    3.94 +	set_render_option(opt, (int)val);
    3.95 +}
    3.96 +
    3.97 +bool get_render_option_bool(int opt)
    3.98 +{
    3.99 +	switch(opt) {
   3.100 +	case ROPT_ITER:
   3.101 +		return rinf.max_iter;
   3.102 +	case ROPT_SHAD:
   3.103 +		return rinf.cast_shadows;
   3.104 +	case ROPT_REFL:
   3.105 +		return rinf.max_iter == saved_iter_val;
   3.106 +	default:
   3.107 +		break;
   3.108 +	}
   3.109 +	return false;
   3.110 +}
   3.111 +
   3.112 +int get_render_option_int(int opt)
   3.113 +{
   3.114 +	switch(opt) {
   3.115 +	case ROPT_ITER:
   3.116 +		return rinf.max_iter;
   3.117 +	case ROPT_SHAD:
   3.118 +		return rinf.cast_shadows ? 1 : 0;
   3.119 +	case ROPT_REFL:
   3.120 +		return rinf.max_iter == saved_iter_val ? 1 : 0;
   3.121 +	default:
   3.122 +		break;
   3.123 +	}
   3.124 +	return -1;
   3.125 +}
   3.126 +
   3.127 +float get_render_option_float(int opt)
   3.128 +{
   3.129 +	return (float)get_render_option_int(opt);
   3.130 +}
   3.131 +
   3.132 +static void update_render_info()
   3.133 +{
   3.134 +	if(!prog) {
   3.135 +		return;
   3.136 +	}
   3.137 +
   3.138 +	CLMemBuffer *mbuf = prog->get_arg_buffer(KARG_RENDER_INFO);
   3.139 +	assert(mbuf);
   3.140 +
   3.141 +	RendInfo *rinf_ptr = (RendInfo*)map_mem_buffer(mbuf, MAP_WR);
   3.142 +	*rinf_ptr = rinf;
   3.143 +	unmap_mem_buffer(mbuf);
   3.144 +}
   3.145 +
   3.146  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg)
   3.147  {
   3.148  	float vfov = M_PI * vfov_deg / 180.0;
     4.1 --- a/src/rt.h	Sun Aug 29 14:02:37 2010 +0100
     4.2 +++ b/src/rt.h	Tue Aug 31 01:47:27 2010 +0100
     4.3 @@ -3,11 +3,27 @@
     4.4  
     4.5  #include "scene.h"
     4.6  
     4.7 +enum {
     4.8 +	ROPT_ITER,
     4.9 +	ROPT_SHAD,
    4.10 +	ROPT_REFL,
    4.11 +
    4.12 +	NUM_RENDER_OPTIONS
    4.13 +};
    4.14 +
    4.15  bool init_renderer(int xsz, int ysz, Scene *scn, unsigned int tex);
    4.16  void destroy_renderer();
    4.17  bool render();
    4.18  void set_xform(float *matrix, float *invtrans);
    4.19  
    4.20 +void set_render_option(int opt, bool val);
    4.21 +void set_render_option(int opt, int val);
    4.22 +void set_render_option(int opt, float val);
    4.23 +
    4.24 +bool get_render_option_bool(int opt);
    4.25 +int get_render_option_int(int opt);
    4.26 +float get_render_option_float(int opt);
    4.27 +
    4.28  void dbg_render_gl(Scene *scn, bool show_tree = false, bool show_obj = true);
    4.29  
    4.30  #endif	/* RT_H_ */