# HG changeset patch # User John Tsiombikas # Date 1279172378 -10800 # Node ID 9f0ddb7018829343596e0a9d2a8f233d4bd3fb83 # Parent 3c95d568d3c7cb648d98d7462abeb69aec287609 caught up diff -r 3c95d568d3c7 -r 9f0ddb701882 rt.cl --- a/rt.cl Thu Jul 15 07:37:05 2010 +0300 +++ b/rt.cl Thu Jul 15 08:39:38 2010 +0300 @@ -6,8 +6,8 @@ struct Sphere { float4 pos; + float4 kd, ks; float radius; - float4 kd, ks; float spow, kr, kt; }; @@ -27,8 +27,10 @@ #define EPSILON 1e-6 -float4 shade(struct Ray ray, struct SurfPoint sp); +float4 shade(struct Ray ray, struct SurfPoint sp, + global const struct Light *lights, int num_lights); bool intersect(struct Ray ray, global const struct Sphere *sph, struct SurfPoint *sp); +float3 reflect(float3 v, float3 n); kernel void render(global float4 *fb, @@ -43,19 +45,40 @@ struct SurfPoint sp, sp0; sp0.t = FLT_MAX; + sp0.obj = 0; for(int i=0; inum_sph; i++) { - if(intersect(ray, sphlist, &sp) && sp.t < sp0.t) { + if(intersect(ray, sphlist + i, &sp) && sp.t < sp0.t) { sp0 = sp; } } - fb[idx] = shade(ray, sp0); + if(sp0.obj) { + fb[idx] = shade(ray, sp0, lights, rinf->num_lights); + } else { + fb[idx] = (float4)(0, 0, 0, 0); + } } -float4 shade(struct Ray ray, struct SurfPoint sp) +float4 shade(struct Ray ray, struct SurfPoint sp, + global const struct Light *lights, int num_lights) { - return sp.obj->kd; + float3 dcol = (float3)(0, 0, 0); + float3 scol = (float3)(0, 0, 0); + + for(int i=0; ispow); + + dcol += sp.obj->kd.xyz * diff * lights[i].color.xyz; + scol += sp.obj->ks.xyz * spec * lights[i].color.xyz; + } + + return (float4)(dcol + scol, 1.0f); } bool intersect(struct Ray ray, @@ -91,5 +114,11 @@ sp->t = t; sp->pos = orig + dir * sp->t; sp->norm = (sp->pos - spos) / sph->radius; + sp->obj = sph; return true; } + +float3 reflect(float3 v, float3 n) +{ + return 2.0f * dot(v, n) * n - v; +} diff -r 3c95d568d3c7 -r 9f0ddb701882 src/rt.cc --- a/src/rt.cc Thu Jul 15 07:37:05 2010 +0300 +++ b/src/rt.cc Thu Jul 15 08:39:38 2010 +0300 @@ -11,9 +11,8 @@ struct Sphere { cl_float4 pos; + cl_float4 kd, ks; cl_float radius; - - cl_float4 kd, ks; cl_float spow; cl_float kr, kt; } __attribute__((packed)); @@ -34,8 +33,8 @@ static int global_size; static Sphere sphlist[] = { - {{0, 0, 8, 1}, 1.0, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 60, 0, 0}, - {{-0.2, 0.4, 5, 1}, 0.25, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 40, 0, 0} + {{0, 0, 8, 1}, {0.7, 0.2, 0.15, 1}, {1, 1, 1, 1}, 1.0, 60, 0, 0}, + {{-0.2, 0.4, 5, 1}, {0.2, 0.9, 0.3, 1}, {1, 1, 1, 1}, 0.25, 40, 0, 0} }; static Light lightlist[] = {