clray
view 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 source
1 struct RendInfo {
2 int xsz, ysz;
3 int num_sph, num_lights;
4 int max_iter;
5 };
7 struct Sphere {
8 float4 pos;
9 float4 kd, ks;
10 float radius;
11 float spow, kr, kt;
12 };
14 struct Light {
15 float4 pos, color;
16 };
18 struct Ray {
19 float4 origin, dir;
20 };
22 struct SurfPoint {
23 float t;
24 float4 pos, norm;
25 global const struct Sphere *obj;
26 };
28 #define EPSILON 1e-6
30 float4 shade(struct Ray ray, struct SurfPoint sp,
31 global const struct Light *lights, int num_lights);
32 bool intersect(struct Ray ray, global const struct Sphere *sph, struct SurfPoint *sp);
33 float4 reflect(float4 v, float4 n);
34 float4 transform(float4 v, global const float *xform);
35 struct Ray transform_ray(global const struct Ray *ray, global const float *xform);
38 kernel void render(global float4 *fb,
39 global const struct RendInfo *rinf,
40 global const struct Sphere *sphlist,
41 global const struct Light *lights,
42 global const struct Ray *primrays,
43 global const float *xform)
44 {
45 int idx = get_global_id(0);
47 struct Ray ray = transform_ray(primrays + idx, xform);
49 struct SurfPoint sp, sp0;
50 sp0.t = FLT_MAX;
51 sp0.obj = 0;
53 for(int i=0; i<rinf->num_sph; i++) {
54 if(intersect(ray, sphlist + i, &sp) && sp.t < sp0.t) {
55 sp0 = sp;
56 }
57 }
59 if(sp0.obj) {
60 fb[idx] = shade(ray, sp0, lights, rinf->num_lights);
61 } else {
62 fb[idx] = (float4)(0, 0, 0, 0);
63 }
64 }
66 float4 shade(struct Ray ray, struct SurfPoint sp,
67 global const struct Light *lights, int num_lights)
68 {
69 float4 dcol = (float4)(0, 0, 0, 0);
70 float4 scol = (float4)(0, 0, 0, 0);
72 for(int i=0; i<num_lights; i++) {
73 float4 ldir = normalize(lights[i].pos - sp.pos);
74 float4 vdir = -normalize(ray.dir);
75 float4 vref = reflect(vdir, sp.norm);
77 float diff = fmax(dot(ldir, sp.norm), 0.0f);
78 float spec = powr(fmax(dot(ldir, vref), 0.0f), sp.obj->spow);
80 dcol += sp.obj->kd * diff * lights[i].color;
81 scol += sp.obj->ks * spec * lights[i].color;
82 }
84 return dcol + scol;
85 }
87 bool intersect(struct Ray ray,
88 global const struct Sphere *sph,
89 struct SurfPoint *sp)
90 {
91 float4 dir = ray.dir;
92 float4 orig = ray.origin;
93 float4 spos = sph->pos;
95 float a = dot(dir, dir);
96 float b = 2.0 * dir.x * (orig.x - spos.x) +
97 2.0 * dir.y * (orig.y - spos.y) +
98 2.0 * dir.z * (orig.z - spos.z);
99 float c = dot(spos, spos) + dot(orig, orig) +
100 2.0 * dot(-spos, orig) - sph->radius * sph->radius;
102 float d = b * b - 4.0 * a * c;
103 if(d < 0.0) return false;
105 float sqrt_d = sqrt(d);
106 float t1 = (-b + sqrt_d) / (2.0 * a);
107 float t2 = (-b - sqrt_d) / (2.0 * a);
109 if(t1 < EPSILON) t1 = t2;
110 if(t2 < EPSILON) t2 = t1;
111 float t = t1 < t2 ? t1 : t2;
113 if(t < EPSILON || t > 1.0) {
114 return false;
115 }
117 sp->t = t;
118 sp->pos = orig + dir * sp->t;
119 sp->norm = (sp->pos - spos) / sph->radius;
120 sp->obj = sph;
121 return true;
122 }
124 float4 reflect(float4 v, float4 n)
125 {
126 return 2.0f * dot(v, n) * n - v;
127 }
129 float4 transform(float4 v, global const float *xform)
130 {
131 float4 res;
132 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
133 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
134 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
135 res.w = 1.0;
136 return res;
137 }
139 struct Ray transform_ray(global const struct Ray *ray, global const float *xform)
140 {
141 struct Ray res;
142 float rot[16];
144 for(int i=0; i<16; i++) {
145 rot[i] = xform[i];
146 }
147 rot[3] = rot[7] = rot[11] = rot[12] = rot[13] = rot[14] = 0.0f;
148 rot[15] = 1.0f;
150 res.origin = transform(ray->origin, xform);
151 res.dir = transform(ray->dir, xform);
152 return res;
153 }