clray

changeset 5:9f0ddb701882

caught up
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 Jul 2010 08:39:38 +0300
parents 3c95d568d3c7
children b06518bb16e9
files rt.cl src/rt.cc
diffstat 2 files changed, 38 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/rt.cl	Thu Jul 15 07:37:05 2010 +0300
     1.2 +++ b/rt.cl	Thu Jul 15 08:39:38 2010 +0300
     1.3 @@ -6,8 +6,8 @@
     1.4  
     1.5  struct Sphere {
     1.6  	float4 pos;
     1.7 +	float4 kd, ks;
     1.8  	float radius;
     1.9 -	float4 kd, ks;
    1.10  	float spow, kr, kt;
    1.11  };
    1.12  
    1.13 @@ -27,8 +27,10 @@
    1.14  
    1.15  #define EPSILON 1e-6
    1.16  
    1.17 -float4 shade(struct Ray ray, struct SurfPoint sp);
    1.18 +float4 shade(struct Ray ray, struct SurfPoint sp,
    1.19 +		global const struct Light *lights, int num_lights);
    1.20  bool intersect(struct Ray ray, global const struct Sphere *sph, struct SurfPoint *sp);
    1.21 +float3 reflect(float3 v, float3 n);
    1.22  
    1.23  
    1.24  kernel void render(global float4 *fb,
    1.25 @@ -43,19 +45,40 @@
    1.26  	struct SurfPoint sp, sp0;
    1.27  
    1.28  	sp0.t = FLT_MAX;
    1.29 +	sp0.obj = 0;
    1.30  
    1.31  	for(int i=0; i<rinf->num_sph; i++) {
    1.32 -		if(intersect(ray, sphlist, &sp) && sp.t < sp0.t) {
    1.33 +		if(intersect(ray, sphlist + i, &sp) && sp.t < sp0.t) {
    1.34  			sp0 = sp;
    1.35  		}
    1.36  	}
    1.37  
    1.38 -	fb[idx] = shade(ray, sp0);
    1.39 +	if(sp0.obj) {
    1.40 +		fb[idx] = shade(ray, sp0, lights, rinf->num_lights);
    1.41 +	} else {
    1.42 +		fb[idx] = (float4)(0, 0, 0, 0);
    1.43 +	}
    1.44  }
    1.45  
    1.46 -float4 shade(struct Ray ray, struct SurfPoint sp)
    1.47 +float4 shade(struct Ray ray, struct SurfPoint sp,
    1.48 +		global const struct Light *lights, int num_lights)
    1.49  {
    1.50 -	return sp.obj->kd;
    1.51 +	float3 dcol = (float3)(0, 0, 0);
    1.52 +	float3 scol = (float3)(0, 0, 0);
    1.53 +
    1.54 +	for(int i=0; i<num_lights; i++) {
    1.55 +		float3 ldir = normalize(lights[i].pos.xyz - sp.pos);
    1.56 +		float3 vdir = -normalize(ray.dir.xyz);
    1.57 +		float3 vref = reflect(vdir, sp.norm);
    1.58 +
    1.59 +		float diff = fmax(dot(ldir, sp.norm), 0.0f);
    1.60 +		float spec = powr(fmax(dot(ldir, vref), 0.0f), sp.obj->spow);
    1.61 +
    1.62 +		dcol += sp.obj->kd.xyz * diff * lights[i].color.xyz;
    1.63 +		scol += sp.obj->ks.xyz * spec * lights[i].color.xyz;
    1.64 +	}
    1.65 +
    1.66 +	return (float4)(dcol + scol, 1.0f);
    1.67  }
    1.68  
    1.69  bool intersect(struct Ray ray,
    1.70 @@ -91,5 +114,11 @@
    1.71  	sp->t = t;
    1.72  	sp->pos = orig + dir * sp->t;
    1.73  	sp->norm = (sp->pos - spos) / sph->radius;
    1.74 +	sp->obj = sph;
    1.75  	return true;
    1.76  }
    1.77 +
    1.78 +float3 reflect(float3 v, float3 n)
    1.79 +{
    1.80 +	return 2.0f * dot(v, n) * n - v;
    1.81 +}
     2.1 --- a/src/rt.cc	Thu Jul 15 07:37:05 2010 +0300
     2.2 +++ b/src/rt.cc	Thu Jul 15 08:39:38 2010 +0300
     2.3 @@ -11,9 +11,8 @@
     2.4  
     2.5  struct Sphere {
     2.6  	cl_float4 pos;
     2.7 +	cl_float4 kd, ks;
     2.8  	cl_float radius;
     2.9 -
    2.10 -	cl_float4 kd, ks;
    2.11  	cl_float spow;
    2.12  	cl_float kr, kt;
    2.13  } __attribute__((packed));
    2.14 @@ -34,8 +33,8 @@
    2.15  static int global_size;
    2.16  
    2.17  static Sphere sphlist[] = {
    2.18 -	{{0, 0, 8, 1}, 1.0, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 60, 0, 0},
    2.19 -	{{-0.2, 0.4, 5, 1}, 0.25, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 40, 0, 0}
    2.20 +	{{0, 0, 8, 1}, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 1.0, 60, 0, 0},
    2.21 +	{{-0.2, 0.4, 5, 1}, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 0.25, 40, 0, 0}
    2.22  };
    2.23  
    2.24  static Light lightlist[] = {