clray

view rt.cl @ 9:a09622aaa043

moving to triangles
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 Jul 2010 06:28:17 +0100
parents deaf85acf6af
children 85fd61f374d9
line source
1 struct RendInfo {
2 int xsz, ysz;
3 int num_faces, num_lights;
4 int max_iter;
5 };
7 struct Vertex {
8 float4 pos;
9 float4 normal;
10 float2 tex;
11 };
13 struct Face {
14 struct Vertex v[3];
15 float4 normal;
16 int matid;
17 };
19 struct Material {
20 float4 kd, ks;
21 float kr, kt;
22 float spow;
23 };
25 struct Light {
26 float4 pos, color;
27 };
29 struct Ray {
30 float4 origin, dir;
31 };
33 struct SurfPoint {
34 float t;
35 float4 pos, norm;
36 global const struct Face *obj;
37 global const struct Material *mat;
38 };
40 #define EPSILON 1e-6
42 float4 shade(struct Ray ray, struct SurfPoint sp,
43 global const struct Light *lights, int num_lights);
44 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
45 float4 reflect(float4 v, float4 n);
46 float4 transform(float4 v, global const float *xform);
47 struct Ray transform_ray(global const struct Ray *ray, global const float *xform);
48 float4 calc_bary(float4 pt, global const struct Face *face);
51 kernel void render(global float4 *fb,
52 global const struct RendInfo *rinf,
53 global const struct Face *faces,
54 global const struct Material *matlib,
55 global const struct Light *lights,
56 global const struct Ray *primrays,
57 global const float *xform)
58 {
59 int idx = get_global_id(0);
61 struct Ray ray = transform_ray(primrays + idx, xform);
63 struct SurfPoint sp, sp0;
64 sp0.t = FLT_MAX;
65 sp0.obj = 0;
67 for(int i=0; i<rinf->num_faces; i++) {
68 if(intersect(ray, faces + i, &sp) && sp.t < sp0.t) {
69 sp0 = sp;
70 }
71 }
73 if(sp0.obj) {
74 sp0.mat = matlib + sp0.obj->matid;
75 fb[idx] = shade(ray, sp0, lights, rinf->num_lights);
76 } else {
77 fb[idx] = (float4)(0, 0, 0, 0);
78 }
79 }
81 float4 shade(struct Ray ray, struct SurfPoint sp,
82 global const struct Light *lights, int num_lights)
83 {
84 float4 dcol = (float4)(0, 0, 0, 0);
85 float4 scol = (float4)(0, 0, 0, 0);
87 for(int i=0; i<num_lights; i++) {
88 float4 ldir = normalize(lights[i].pos - sp.pos);
89 float4 vdir = -normalize(ray.dir);
90 float4 vref = reflect(vdir, sp.norm);
92 float diff = fmax(dot(ldir, sp.norm), 0.0f);
93 float spec = powr(fmax(dot(ldir, vref), 0.0f), sp.mat->spow);
95 dcol += sp.mat->kd * diff * lights[i].color;
96 scol += sp.mat->ks * spec * lights[i].color;
97 }
99 return dcol + scol;
100 }
102 bool intersect(struct Ray ray,
103 global const struct Face *face,
104 struct SurfPoint *sp)
105 {
106 float ndotdir = dot(face->normal, ray.dir);
107 if(fabs(ndotdir) <= EPSILON) {
108 return false;
109 }
111 float4 pt = face->v[0].pos;
112 float4 vec = pt - ray.origin;
114 float ndotvec = dot(face->normal, vec);
115 float t = ndotvec / ndotdir;
117 if(t < EPSILON || t > 1.0) {
118 return false;
119 }
120 pt = ray.origin + ray.dir * t;
122 float4 bc = calc_bary(pt, face);
123 float bc_sum = bc.x + bc.y + bc.z;
125 if(bc_sum < -EPSILON || bc_sum > 1.0) {
126 return false;
127 }
129 sp->t = t;
130 sp->pos = pt;
131 sp->norm = face->normal;
132 sp->obj = face;
133 return true;
134 }
136 float4 reflect(float4 v, float4 n)
137 {
138 return 2.0f * dot(v, n) * n - v;
139 }
141 float4 transform(float4 v, global const float *xform)
142 {
143 float4 res;
144 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
145 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
146 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
147 res.w = 1.0;
148 return res;
149 }
151 struct Ray transform_ray(global const struct Ray *ray, global const float *xform)
152 {
153 struct Ray res;
154 float rot[16];
156 for(int i=0; i<16; i++) {
157 rot[i] = xform[i];
158 }
159 rot[3] = rot[7] = rot[11] = rot[12] = rot[13] = rot[14] = 0.0f;
160 rot[15] = 1.0f;
162 res.origin = transform(ray->origin, xform);
163 res.dir = transform(ray->dir, xform);
164 return res;
165 }
167 float4 calc_bary(float4 pt, global const struct Face *face)
168 {
169 float4 bc = {0, 0, 0, 0};
171 float4 vi = face->v[1].pos - face->v[0].pos;
172 float4 vj = face->v[2].pos - face->v[0].pos;
173 float area = fabs(dot(cross(vi, vj), face->normal) / 2.0);
174 if(area < EPSILON) {
175 return bc;
176 }
178 float4 pv0 = face->v[0].pos - pt;
179 float4 pv1 = face->v[1].pos - pt;
180 float4 pv2 = face->v[2].pos - pt;
182 // calculate the areas of each sub-triangle
183 float a0 = fabs(dot(cross(pv1, pv2), face->normal) / 2.0);
184 float a1 = fabs(dot(cross(pv2, pv0), face->normal) / 2.0);
185 float a2 = fabs(dot(cross(pv0, pv1), face->normal) / 2.0);
187 bc.x = a0 / area;
188 bc.y = a1 / area;
189 bc.z = a2 / area;
190 return bc;
191 }