clray
view rt.cl @ 7:575383f3a239
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 23 Jul 2010 01:22:03 +0100 |
parents | 9f0ddb701882 |
children | deaf85acf6af |
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 float3 pos, norm;
25 global const struct Sphere *obj;
26 };
28 struct Matrix4x4 {
29 float m[16];
30 };
32 #define EPSILON 1e-6
34 float4 shade(struct Ray ray, struct SurfPoint sp,
35 global const struct Light *lights, int num_lights);
36 bool intersect(struct Ray ray, global const struct Sphere *sph, struct SurfPoint *sp);
37 float3 reflect(float3 v, float3 n);
40 kernel void render(global float4 *fb,
41 global const struct RendInfo *rinf,
42 global const struct Sphere *sphlist,
43 global const struct Light *lights,
44 global const struct Ray *primrays,
45 global const struct Matrix4x4 xform)
46 {
47 int idx = get_global_id(0);
49 struct Ray ray = primrays[idx];
50 struct SurfPoint sp, sp0;
52 sp0.t = FLT_MAX;
53 sp0.obj = 0;
55 for(int i=0; i<rinf->num_sph; i++) {
56 if(intersect(ray, sphlist + i, &sp) && sp.t < sp0.t) {
57 sp0 = sp;
58 }
59 }
61 if(sp0.obj) {
62 fb[idx] = shade(ray, sp0, lights, rinf->num_lights);
63 } else {
64 fb[idx] = (float4)(0, 0, 0, 0);
65 }
66 }
68 float4 shade(struct Ray ray, struct SurfPoint sp,
69 global const struct Light *lights, int num_lights)
70 {
71 float3 dcol = (float3)(0, 0, 0);
72 float3 scol = (float3)(0, 0, 0);
74 for(int i=0; i<num_lights; i++) {
75 float3 ldir = normalize(lights[i].pos.xyz - sp.pos);
76 float3 vdir = -normalize(ray.dir.xyz);
77 float3 vref = reflect(vdir, sp.norm);
79 float diff = fmax(dot(ldir, sp.norm), 0.0f);
80 float spec = powr(fmax(dot(ldir, vref), 0.0f), sp.obj->spow);
82 dcol += sp.obj->kd.xyz * diff * lights[i].color.xyz;
83 scol += sp.obj->ks.xyz * spec * lights[i].color.xyz;
84 }
86 return (float4)(dcol + scol, 1.0f);
87 }
89 bool intersect(struct Ray ray,
90 global const struct Sphere *sph,
91 struct SurfPoint *sp)
92 {
93 float3 dir = ray.dir.xyz;
94 float3 orig = ray.origin.xyz;
95 float3 spos = sph->pos.xyz;
97 float a = dot(dir, dir);
98 float b = 2.0 * dir.x * (orig.x - spos.x) +
99 2.0 * dir.y * (orig.y - spos.y) +
100 2.0 * dir.z * (orig.z - spos.z);
101 float c = dot(spos, spos) + dot(orig, orig) +
102 2.0 * dot(-spos, orig) - sph->radius * sph->radius;
104 float d = b * b - 4.0 * a * c;
105 if(d < 0.0) return false;
107 float sqrt_d = sqrt(d);
108 float t1 = (-b + sqrt_d) / (2.0 * a);
109 float t2 = (-b - sqrt_d) / (2.0 * a);
111 if(t1 < EPSILON) t1 = t2;
112 if(t2 < EPSILON) t2 = t1;
113 float t = t1 < t2 ? t1 : t2;
115 if(t < EPSILON || t > 1.0) {
116 return false;
117 }
119 sp->t = t;
120 sp->pos = orig + dir * sp->t;
121 sp->norm = (sp->pos - spos) / sph->radius;
122 sp->obj = sph;
123 return true;
124 }
126 float3 reflect(float3 v, float3 n)
127 {
128 return 2.0f * dot(v, n) * n - v;
129 }