clray

diff rt.cl @ 8:deaf85acf6af

interactive spheres
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 Jul 2010 19:48:43 +0100
parents 575383f3a239
children a09622aaa043
line diff
     1.1 --- a/rt.cl	Fri Jul 23 01:22:03 2010 +0100
     1.2 +++ b/rt.cl	Fri Jul 23 19:48:43 2010 +0100
     1.3 @@ -21,20 +21,18 @@
     1.4  
     1.5  struct SurfPoint {
     1.6  	float t;
     1.7 -	float3 pos, norm;
     1.8 +	float4 pos, norm;
     1.9  	global const struct Sphere *obj;
    1.10  };
    1.11  
    1.12 -struct Matrix4x4 {
    1.13 -	float m[16];
    1.14 -};
    1.15 -
    1.16  #define EPSILON 1e-6
    1.17  
    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 +float4 reflect(float4 v, float4 n);
    1.23 +float4 transform(float4 v, global const float *xform);
    1.24 +struct Ray transform_ray(global const struct Ray *ray, global const float *xform);
    1.25  
    1.26  
    1.27  kernel void render(global float4 *fb,
    1.28 @@ -42,13 +40,13 @@
    1.29  		global const struct Sphere *sphlist,
    1.30  		global const struct Light *lights,
    1.31  		global const struct Ray *primrays,
    1.32 -		global const struct Matrix4x4 xform)
    1.33 +		global const float *xform)
    1.34  {
    1.35  	int idx = get_global_id(0);
    1.36  
    1.37 -	struct Ray ray = primrays[idx];
    1.38 +	struct Ray ray = transform_ray(primrays + idx, xform);
    1.39 +
    1.40  	struct SurfPoint sp, sp0;
    1.41 -
    1.42  	sp0.t = FLT_MAX;
    1.43  	sp0.obj = 0;
    1.44  
    1.45 @@ -68,31 +66,31 @@
    1.46  float4 shade(struct Ray ray, struct SurfPoint sp,
    1.47  		global const struct Light *lights, int num_lights)
    1.48  {
    1.49 -	float3 dcol = (float3)(0, 0, 0);
    1.50 -	float3 scol = (float3)(0, 0, 0);
    1.51 +	float4 dcol = (float4)(0, 0, 0, 0);
    1.52 +	float4 scol = (float4)(0, 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 +		float4 ldir = normalize(lights[i].pos - sp.pos);
    1.59 +		float4 vdir = -normalize(ray.dir);
    1.60 +		float4 vref = reflect(vdir, sp.norm);
    1.61  
    1.62  		float diff = fmax(dot(ldir, sp.norm), 0.0f);
    1.63  		float spec = powr(fmax(dot(ldir, vref), 0.0f), sp.obj->spow);
    1.64  
    1.65 -		dcol += sp.obj->kd.xyz * diff * lights[i].color.xyz;
    1.66 -		scol += sp.obj->ks.xyz * spec * lights[i].color.xyz;
    1.67 +		dcol += sp.obj->kd * diff * lights[i].color;
    1.68 +		scol += sp.obj->ks * spec * lights[i].color;
    1.69  	}
    1.70  
    1.71 -	return (float4)(dcol + scol, 1.0f);
    1.72 +	return dcol + scol;
    1.73  }
    1.74  
    1.75  bool intersect(struct Ray ray,
    1.76  		global const struct Sphere *sph,
    1.77  		struct SurfPoint *sp)
    1.78  {
    1.79 -	float3 dir = ray.dir.xyz;
    1.80 -	float3 orig = ray.origin.xyz;
    1.81 -	float3 spos = sph->pos.xyz;
    1.82 +	float4 dir = ray.dir;
    1.83 +	float4 orig = ray.origin;
    1.84 +	float4 spos = sph->pos;
    1.85  
    1.86  	float a = dot(dir, dir);
    1.87  	float b = 2.0 * dir.x * (orig.x - spos.x) +
    1.88 @@ -123,7 +121,33 @@
    1.89  	return true;
    1.90  }
    1.91  
    1.92 -float3 reflect(float3 v, float3 n)
    1.93 +float4 reflect(float4 v, float4 n)
    1.94  {
    1.95  	return 2.0f * dot(v, n) * n - v;
    1.96  }
    1.97 +
    1.98 +float4 transform(float4 v, global const float *xform)
    1.99 +{
   1.100 +	float4 res;
   1.101 +	res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
   1.102 +	res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
   1.103 +	res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
   1.104 +	res.w = 1.0;
   1.105 +	return res;
   1.106 +}
   1.107 +
   1.108 +struct Ray transform_ray(global const struct Ray *ray, global const float *xform)
   1.109 +{
   1.110 +	struct Ray res;
   1.111 +	float rot[16];
   1.112 +
   1.113 +	for(int i=0; i<16; i++) {
   1.114 +		rot[i] = xform[i];
   1.115 +	}
   1.116 +	rot[3] = rot[7] = rot[11] = rot[12] = rot[13] = rot[14] = 0.0f;
   1.117 +	rot[15] = 1.0f;
   1.118 +
   1.119 +	res.origin = transform(ray->origin, xform);
   1.120 +	res.dir = transform(ray->dir, xform);
   1.121 +	return res;
   1.122 +}