clray

diff rt.cl @ 5:9f0ddb701882

caught up
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 Jul 2010 08:39:38 +0300
parents 3c95d568d3c7
children 575383f3a239
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 +}