clray

view rt.cl @ 6:b06518bb16e9

adding polygon meshes and obj loading
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 20 Jul 2010 20:02:41 +0100
parents 3c95d568d3c7
children 575383f3a239
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 #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 float3 reflect(float3 v, float3 n);
36 kernel void render(global float4 *fb,
37 global const struct RendInfo *rinf,
38 global const struct Sphere *sphlist,
39 global const struct Light *lights,
40 global const struct Ray *primrays)
41 {
42 int idx = get_global_id(0);
44 struct Ray ray = primrays[idx];
45 struct SurfPoint sp, sp0;
47 sp0.t = FLT_MAX;
48 sp0.obj = 0;
50 for(int i=0; i<rinf->num_sph; i++) {
51 if(intersect(ray, sphlist + i, &sp) && sp.t < sp0.t) {
52 sp0 = sp;
53 }
54 }
56 if(sp0.obj) {
57 fb[idx] = shade(ray, sp0, lights, rinf->num_lights);
58 } else {
59 fb[idx] = (float4)(0, 0, 0, 0);
60 }
61 }
63 float4 shade(struct Ray ray, struct SurfPoint sp,
64 global const struct Light *lights, int num_lights)
65 {
66 float3 dcol = (float3)(0, 0, 0);
67 float3 scol = (float3)(0, 0, 0);
69 for(int i=0; i<num_lights; i++) {
70 float3 ldir = normalize(lights[i].pos.xyz - sp.pos);
71 float3 vdir = -normalize(ray.dir.xyz);
72 float3 vref = reflect(vdir, sp.norm);
74 float diff = fmax(dot(ldir, sp.norm), 0.0f);
75 float spec = powr(fmax(dot(ldir, vref), 0.0f), sp.obj->spow);
77 dcol += sp.obj->kd.xyz * diff * lights[i].color.xyz;
78 scol += sp.obj->ks.xyz * spec * lights[i].color.xyz;
79 }
81 return (float4)(dcol + scol, 1.0f);
82 }
84 bool intersect(struct Ray ray,
85 global const struct Sphere *sph,
86 struct SurfPoint *sp)
87 {
88 float3 dir = ray.dir.xyz;
89 float3 orig = ray.origin.xyz;
90 float3 spos = sph->pos.xyz;
92 float a = dot(dir, dir);
93 float b = 2.0 * dir.x * (orig.x - spos.x) +
94 2.0 * dir.y * (orig.y - spos.y) +
95 2.0 * dir.z * (orig.z - spos.z);
96 float c = dot(spos, spos) + dot(orig, orig) +
97 2.0 * dot(-spos, orig) - sph->radius * sph->radius;
99 float d = b * b - 4.0 * a * c;
100 if(d < 0.0) return false;
102 float sqrt_d = sqrt(d);
103 float t1 = (-b + sqrt_d) / (2.0 * a);
104 float t2 = (-b - sqrt_d) / (2.0 * a);
106 if(t1 < EPSILON) t1 = t2;
107 if(t2 < EPSILON) t2 = t1;
108 float t = t1 < t2 ? t1 : t2;
110 if(t < EPSILON || t > 1.0) {
111 return false;
112 }
114 sp->t = t;
115 sp->pos = orig + dir * sp->t;
116 sp->norm = (sp->pos - spos) / sph->radius;
117 sp->obj = sph;
118 return true;
119 }
121 float3 reflect(float3 v, float3 n)
122 {
123 return 2.0f * dot(v, n) * n - v;
124 }