clray

changeset 19:8baea9b66b50

added reflection
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 06:45:57 +0100
parents 4b1604f9798a
children 63a6b46f58a0
files rt.cl src/mesh.cc src/rt.cc
diffstat 3 files changed, 33 insertions(+), 24 deletions(-) [+]
line diff
     1.1 --- a/rt.cl	Mon Aug 09 05:38:51 2010 +0100
     1.2 +++ b/rt.cl	Mon Aug 09 06:45:57 2010 +0100
     1.3 @@ -41,7 +41,7 @@
     1.4  	float t;
     1.5  	float4 pos, norm, dbg;
     1.6  	global const struct Face *obj;
     1.7 -	global const struct Material *mat;
     1.8 +	struct Material mat;
     1.9  };
    1.10  
    1.11  struct Scene {
    1.12 @@ -65,6 +65,7 @@
    1.13  float4 transform(float4 v, global const float *xform);
    1.14  void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans);
    1.15  float4 calc_bary(float4 pt, global const struct Face *face, float4 norm);
    1.16 +float mean(float4 v);
    1.17  
    1.18  kernel void render(global float4 *fb,
    1.19  		global const struct RendInfo *rinf,
    1.20 @@ -97,12 +98,27 @@
    1.21  
    1.22  	//fb[idx] = trace(ray, &scn);
    1.23  
    1.24 -	struct SurfPoint sp;
    1.25 -	if(find_intersection(ray, &scn, &sp)) {
    1.26 -		fb[idx] = shade(ray, &scn, &sp);
    1.27 -	} else {
    1.28 -		fb[idx] = (float4)(0, 0, 0, 0);
    1.29 +	float4 pixel = (float4)(0, 0, 0, 0);
    1.30 +	float4 energy = (float4)(1.0, 1.0, 1.0, 1.0);
    1.31 +	int iter = 0;
    1.32 +
    1.33 +	while(iter++ < rinf->max_iter && mean(energy) > MIN_ENERGY) {
    1.34 +		struct SurfPoint sp;
    1.35 +		if(find_intersection(ray, &scn, &sp)) {
    1.36 +			pixel += shade(ray, &scn, &sp) * energy;
    1.37 +
    1.38 +			float4 refl_col = sp.mat.ks * sp.mat.kr;
    1.39 +
    1.40 +			ray.origin = sp.pos;
    1.41 +			ray.dir = reflect(-ray.dir, sp.norm);
    1.42 +
    1.43 +			energy *= sp.mat.ks * sp.mat.kr;
    1.44 +		} else {
    1.45 +			iter = INT_MAX - 1;	// to break out of the loop
    1.46 +		}
    1.47  	}
    1.48 +
    1.49 +	fb[idx] = pixel;
    1.50  }
    1.51  
    1.52  /*float4 trace(struct Ray ray, struct Scene *scn)
    1.53 @@ -122,14 +138,13 @@
    1.54  {
    1.55  	float4 norm = sp->norm;
    1.56  	bool entering = true;
    1.57 -	struct Material mat = *sp->mat;
    1.58  
    1.59  	if(dot(ray.dir, norm) >= 0.0) {
    1.60  		norm = -norm;
    1.61  		entering = false;
    1.62  	}
    1.63  
    1.64 -	float4 dcol = scn->ambient * mat.kd;
    1.65 +	float4 dcol = scn->ambient * sp->mat.kd;
    1.66  	float4 scol = (float4)(0, 0, 0, 0);
    1.67  
    1.68  	for(int i=0; i<scn->num_lights; i++) {
    1.69 @@ -145,25 +160,13 @@
    1.70  			float4 vref = reflect(vdir, norm);
    1.71  
    1.72  			float diff = fmax(dot(ldir, norm), 0.0f);
    1.73 -			dcol += mat.kd * diff * scn->lights[i].color;
    1.74 +			dcol += sp->mat.kd * diff * scn->lights[i].color;
    1.75  
    1.76  			//float spec = powr(fmax(dot(ldir, vref), 0.0f), mat.spow);
    1.77 -			//scol += mat.ks * spec * scn->lights[i].color;
    1.78 +			//scol += sp->mat.ks * spec * scn->lights[i].color;
    1.79  		}
    1.80  	}
    1.81  
    1.82 -	/*float4 refl_col = mat.ks * mat.kr;
    1.83 -	float refl_coeff = (refl_col.x + refl_col.y + refl_col.z) / 3.0;
    1.84 -
    1.85 -	if(refl_coeff > MIN_ENERGY) {
    1.86 -		struct Ray refl_ray;
    1.87 -		refl_ray.origin = sp->pos;
    1.88 -		refl_ray.dir = reflect(-ray.dir, norm);
    1.89 -		refl_ray.energy *= refl_coeff;
    1.90 -
    1.91 -		scol += trace(refl_ray, scn) * refl_col;
    1.92 -	}*/
    1.93 -
    1.94  	return dcol + scol;
    1.95  }
    1.96  
    1.97 @@ -186,7 +189,7 @@
    1.98  
    1.99  	if(spres) {
   1.100  		*spres = sp0;
   1.101 -		spres->mat = scn->matlib + sp0.obj->matid;
   1.102 +		spres->mat = scn->matlib[sp0.obj->matid];
   1.103  	}
   1.104  	return true;
   1.105  }
   1.106 @@ -285,3 +288,8 @@
   1.107  	bc.z = a2 / area;
   1.108  	return bc;
   1.109  }
   1.110 +
   1.111 +float mean(float4 v)
   1.112 +{
   1.113 +	return native_divide(v.x + v.y + v.z, 3.0);
   1.114 +}
     2.1 --- a/src/mesh.cc	Mon Aug 09 05:38:51 2010 +0100
     2.2 +++ b/src/mesh.cc	Mon Aug 09 06:45:57 2010 +0100
     2.3 @@ -99,6 +99,7 @@
     2.4  		name = tex_dif = tex_spec = tex_shin = tex_alpha = tex_refl = tex_bump = "";
     2.5  		shininess = 0;
     2.6  		ior = alpha = 1;
     2.7 +		refl = 0.0;
     2.8  	}
     2.9  };
    2.10  
     3.1 --- a/src/rt.cc	Mon Aug 09 05:38:51 2010 +0100
     3.2 +++ b/src/rt.cc	Mon Aug 09 06:45:57 2010 +0100
     3.3 @@ -46,7 +46,7 @@
     3.4  static int global_size;
     3.5  
     3.6  static Light lightlist[] = {
     3.7 -	{{-10, 13, -20, 0}, {1, 1, 1, 1}}
     3.8 +	{{-8, 15, -18, 0}, {1, 1, 1, 1}}
     3.9  };
    3.10  
    3.11