# HG changeset patch # User John Tsiombikas # Date 1279168625 -10800 # Node ID 3c95d568d3c7cb648d98d7462abeb69aec287609 # Parent 88ac4eb2d18a260b4f038805a8ff374a14801658 wow a ball diff -r 88ac4eb2d18a -r 3c95d568d3c7 rt.cl --- a/rt.cl Tue Jul 13 03:38:29 2010 +0300 +++ b/rt.cl Thu Jul 15 07:37:05 2010 +0300 @@ -7,7 +7,8 @@ struct Sphere { float4 pos; float radius; - float4 color; + float4 kd, ks; + float spow, kr, kt; }; struct Light { @@ -21,34 +22,44 @@ struct SurfPoint { float t; float3 pos, norm; + global const struct Sphere *obj; }; #define EPSILON 1e-6 -bool intersect(struct Ray ray, __global const struct Sphere *sph, struct SurfPoint *sp); +float4 shade(struct Ray ray, struct SurfPoint sp); +bool intersect(struct Ray ray, global const struct Sphere *sph, struct SurfPoint *sp); -__kernel void render(__global float4 *fb, - __global const struct RendInfo *rinf, - __global const struct Sphere *sphlist, - __global const struct Light *lights, - __global const struct Ray *primrays) + +kernel void render(global float4 *fb, + global const struct RendInfo *rinf, + global const struct Sphere *sphlist, + global const struct Light *lights, + global const struct Ray *primrays) { int idx = get_global_id(0); struct Ray ray = primrays[idx]; - struct SurfPoint sp; + struct SurfPoint sp, sp0; - if(intersect(ray, sphlist, &sp)) { - fb[idx] = (float4)(1, 0, 0, 1); - } else { - fb[idx] = (float4)(0, 0, 0, 1); + sp0.t = FLT_MAX; + + for(int i=0; inum_sph; i++) { + if(intersect(ray, sphlist, &sp) && sp.t < sp0.t) { + sp0 = sp; + } } - fb[idx] = primrays[idx].dir * 0.5 + 0.5; + fb[idx] = shade(ray, sp0); +} + +float4 shade(struct Ray ray, struct SurfPoint sp) +{ + return sp.obj->kd; } bool intersect(struct Ray ray, - __global const struct Sphere *sph, + global const struct Sphere *sph, struct SurfPoint *sp) { float3 dir = ray.dir.xyz; diff -r 88ac4eb2d18a -r 3c95d568d3c7 src/rt.cc --- a/src/rt.cc Tue Jul 13 03:38:29 2010 +0300 +++ b/src/rt.cc Thu Jul 15 07:37:05 2010 +0300 @@ -13,7 +13,9 @@ cl_float4 pos; cl_float radius; - cl_float4 color; + cl_float4 kd, ks; + cl_float spow; + cl_float kr, kt; } __attribute__((packed)); struct Ray { @@ -31,21 +33,27 @@ static CLProgram *prog; 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} +}; + +static Light lightlist[] = { + {{-10, 10, -20, 1}, {1, 1, 1, 1}} +}; + +static RendInfo rinf; + + bool init_renderer(int xsz, int ysz, float *fb) { - Sphere sphlist[] = { - {{0, 0, 10, 1}, 1.0, {1, 0, 0, 1}} - }; - Light lightlist[] = { - {{-10, 10, -20, 1}, {1, 1, 1, 1}} - }; - RendInfo rinf = { - xsz, ysz, - sizeof sphlist / sizeof *sphlist, - sizeof lightlist / sizeof *lightlist, - 6 - }; - + // render info + rinf.xsz = xsz; + rinf.ysz = ysz; + rinf.num_sph = sizeof sphlist / sizeof *sphlist; + rinf.num_lights = sizeof lightlist / sizeof *lightlist; + rinf.max_iter = 6; + /* calculate primary rays */ prim_rays = new Ray[xsz * ysz]; @@ -105,7 +113,9 @@ float py = 1.0 - ((float)y / (float)h) * ysz; float pz = 1.0 / tan(0.5 * vfov); - pz *= 1000.0; + px *= 100.0; + py *= 100.0; + pz *= 100.0; Ray ray = {{0, 0, 0, 1}, {px, py, pz, 1}}; return ray;