# HG changeset patch # User John Tsiombikas # Date 1281332757 -3600 # Node ID 8baea9b66b50417dc0dea13e9171554482bed31a # Parent 4b1604f9798abf82fb6499b4e246cdf41968a91e added reflection diff -r 4b1604f9798a -r 8baea9b66b50 rt.cl --- a/rt.cl Mon Aug 09 05:38:51 2010 +0100 +++ b/rt.cl Mon Aug 09 06:45:57 2010 +0100 @@ -41,7 +41,7 @@ float t; float4 pos, norm, dbg; global const struct Face *obj; - global const struct Material *mat; + struct Material mat; }; struct Scene { @@ -65,6 +65,7 @@ float4 transform(float4 v, global const float *xform); void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans); float4 calc_bary(float4 pt, global const struct Face *face, float4 norm); +float mean(float4 v); kernel void render(global float4 *fb, global const struct RendInfo *rinf, @@ -97,12 +98,27 @@ //fb[idx] = trace(ray, &scn); - struct SurfPoint sp; - if(find_intersection(ray, &scn, &sp)) { - fb[idx] = shade(ray, &scn, &sp); - } else { - fb[idx] = (float4)(0, 0, 0, 0); + float4 pixel = (float4)(0, 0, 0, 0); + float4 energy = (float4)(1.0, 1.0, 1.0, 1.0); + int iter = 0; + + while(iter++ < rinf->max_iter && mean(energy) > MIN_ENERGY) { + struct SurfPoint sp; + if(find_intersection(ray, &scn, &sp)) { + pixel += shade(ray, &scn, &sp) * energy; + + float4 refl_col = sp.mat.ks * sp.mat.kr; + + ray.origin = sp.pos; + ray.dir = reflect(-ray.dir, sp.norm); + + energy *= sp.mat.ks * sp.mat.kr; + } else { + iter = INT_MAX - 1; // to break out of the loop + } } + + fb[idx] = pixel; } /*float4 trace(struct Ray ray, struct Scene *scn) @@ -122,14 +138,13 @@ { float4 norm = sp->norm; bool entering = true; - struct Material mat = *sp->mat; if(dot(ray.dir, norm) >= 0.0) { norm = -norm; entering = false; } - float4 dcol = scn->ambient * mat.kd; + float4 dcol = scn->ambient * sp->mat.kd; float4 scol = (float4)(0, 0, 0, 0); for(int i=0; inum_lights; i++) { @@ -145,25 +160,13 @@ float4 vref = reflect(vdir, norm); float diff = fmax(dot(ldir, norm), 0.0f); - dcol += mat.kd * diff * scn->lights[i].color; + dcol += sp->mat.kd * diff * scn->lights[i].color; //float spec = powr(fmax(dot(ldir, vref), 0.0f), mat.spow); - //scol += mat.ks * spec * scn->lights[i].color; + //scol += sp->mat.ks * spec * scn->lights[i].color; } } - /*float4 refl_col = mat.ks * mat.kr; - float refl_coeff = (refl_col.x + refl_col.y + refl_col.z) / 3.0; - - if(refl_coeff > MIN_ENERGY) { - struct Ray refl_ray; - refl_ray.origin = sp->pos; - refl_ray.dir = reflect(-ray.dir, norm); - refl_ray.energy *= refl_coeff; - - scol += trace(refl_ray, scn) * refl_col; - }*/ - return dcol + scol; } @@ -186,7 +189,7 @@ if(spres) { *spres = sp0; - spres->mat = scn->matlib + sp0.obj->matid; + spres->mat = scn->matlib[sp0.obj->matid]; } return true; } @@ -285,3 +288,8 @@ bc.z = a2 / area; return bc; } + +float mean(float4 v) +{ + return native_divide(v.x + v.y + v.z, 3.0); +} diff -r 4b1604f9798a -r 8baea9b66b50 src/mesh.cc --- a/src/mesh.cc Mon Aug 09 05:38:51 2010 +0100 +++ b/src/mesh.cc Mon Aug 09 06:45:57 2010 +0100 @@ -99,6 +99,7 @@ name = tex_dif = tex_spec = tex_shin = tex_alpha = tex_refl = tex_bump = ""; shininess = 0; ior = alpha = 1; + refl = 0.0; } }; diff -r 4b1604f9798a -r 8baea9b66b50 src/rt.cc --- a/src/rt.cc Mon Aug 09 05:38:51 2010 +0100 +++ b/src/rt.cc Mon Aug 09 06:45:57 2010 +0100 @@ -46,7 +46,7 @@ static int global_size; static Light lightlist[] = { - {{-10, 13, -20, 0}, {1, 1, 1, 1}} + {{-8, 15, -18, 0}, {1, 1, 1, 1}} };