clray

view rt.cl @ 17:074a64b9d6bd

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 05:03:17 +0100
parents 9e4a28063394
children 4b1604f9798a
line source
1 /* vim: set ft=opencl:ts=4:sw=4 */
3 struct RendInfo {
4 int xsz, ysz;
5 int num_faces, num_lights;
6 int max_iter;
7 float4 ambient;
8 int dbg;
9 };
11 struct Vertex {
12 float4 pos;
13 float4 normal;
14 float4 tex;
15 float4 padding;
16 };
18 struct Face {
19 struct Vertex v[3];
20 float4 normal;
21 int matid;
22 int padding[3];
23 };
25 struct Material {
26 float4 kd, ks;
27 float kr, kt;
28 float spow;
29 float padding;
30 };
32 struct Light {
33 float4 pos, color;
34 };
36 struct Ray {
37 float4 origin, dir;
38 float energy, pad[3];
39 };
41 struct SurfPoint {
42 float t;
43 float4 pos, norm, dbg;
44 global const struct Face *obj;
45 global const struct Material *mat;
46 };
48 struct Scene {
49 float4 ambient;
50 global const struct Face *faces;
51 int num_faces;
52 global const struct Light *lights;
53 int num_lights;
54 global const struct Material *matlib;
55 };
57 #define MIN_ENERGY 0.001
58 #define EPSILON 1e-6
60 //float4 trace(struct Ray ray, struct Scene *scn);
61 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp);
62 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp);
63 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
65 float4 reflect(float4 v, float4 n);
66 float4 transform(float4 v, global const float *xform);
67 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans);
68 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm);
70 kernel void render(global float4 *fb,
71 global const struct RendInfo *rinf,
72 global const struct Face *faces,
73 global const struct Material *matlib,
74 global const struct Light *lights,
75 global const struct Ray *primrays,
76 global const float *xform,
77 global const float *invtrans,
78 global struct Face *outfaces)
79 {
80 int idx = get_global_id(0);
82 struct Scene scn;
83 scn.ambient = rinf->ambient;
84 scn.faces = faces;
85 scn.num_faces = rinf->num_faces;
86 scn.lights = lights;
87 scn.num_lights = rinf->num_lights;
88 scn.matlib = matlib;
90 struct Ray ray = primrays[idx];
91 transform_ray(&ray, xform, invtrans);
93 //fb[idx] = trace(ray, &scn);
95 struct SurfPoint sp;
96 if(find_intersection(ray, &scn, &sp)) {
97 fb[idx] = shade(ray, &scn, &sp);
98 } else {
99 fb[idx] = (float4)(0, 0, 0, 0);
100 }
101 }
103 /*float4 trace(struct Ray ray, struct Scene *scn)
104 {
105 float4 color;
106 struct SurfPoint sp;
108 if(find_intersection(ray, scn, &sp)) {
109 color = shade(ray, scn, &sp);
110 } else {
111 color = (float4)(0, 0, 0, 0);
112 }
113 return color;
114 }*/
116 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp)
117 {
118 float4 norm = sp->norm;
119 bool entering = true;
120 struct Material mat = *sp->mat;
122 if(dot(ray.dir, norm) >= 0.0) {
123 norm = -norm;
124 entering = false;
125 }
127 float4 dcol = scn->ambient * mat.kd;
128 float4 scol = (float4)(0, 0, 0, 0);
130 for(int i=0; i<scn->num_lights; i++) {
131 float4 ldir = scn->lights[i].pos - sp->pos;
133 struct Ray shadowray;
134 shadowray.origin = sp->pos;
135 shadowray.dir = ldir;
137 if(!find_intersection(shadowray, scn, 0)) {
138 ldir = normalize(ldir);
139 float4 vdir = -normalize(ray.dir);
140 float4 vref = reflect(vdir, norm);
142 float diff = fmax(dot(ldir, norm), 0.0f);
143 dcol += mat.kd * diff * scn->lights[i].color;
145 //float spec = powr(fmax(dot(ldir, vref), 0.0f), mat.spow);
146 //scol += mat.ks * spec * scn->lights[i].color;
147 }
148 }
150 /*float4 refl_col = mat.ks * mat.kr;
151 float refl_coeff = (refl_col.x + refl_col.y + refl_col.z) / 3.0;
153 if(refl_coeff > MIN_ENERGY) {
154 struct Ray refl_ray;
155 refl_ray.origin = sp->pos;
156 refl_ray.dir = reflect(-ray.dir, norm);
157 refl_ray.energy *= refl_coeff;
159 scol += trace(refl_ray, scn) * refl_col;
160 }*/
162 return dcol + scol;
163 }
166 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
167 {
168 struct SurfPoint sp, sp0;
169 sp0.t = 1.0;
170 sp0.obj = 0;
172 for(int i=0; i<scn->num_faces; i++) {
173 if(intersect(ray, scn->faces + i, &sp) && sp.t < sp0.t) {
174 sp0 = sp;
175 }
176 }
178 if(!sp0.obj) {
179 return false;
180 }
182 if(spres) {
183 *spres = sp0;
184 spres->mat = scn->matlib + sp0.obj->matid;
185 }
186 return true;
187 }
189 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp)
190 {
191 float4 origin = ray.origin;
192 float4 dir = ray.dir;
193 float4 norm = face->normal;
195 float ndotdir = dot(dir, norm);
197 if(fabs(ndotdir) <= EPSILON) {
198 return false;
199 }
201 float4 pt = face->v[0].pos;
202 float4 vec = pt - origin;
204 float ndotvec = dot(norm, vec);
205 float t = ndotvec / ndotdir;
207 if(t < EPSILON || t > 1.0) {
208 return false;
209 }
210 pt = origin + dir * t;
213 float4 bc = calc_bary(pt, face, norm);
214 float bc_sum = bc.x + bc.y + bc.z;
216 if(bc_sum < 0.0 || bc_sum > 1.0 + EPSILON) {
217 return false;
218 bc *= 1.2;
219 }
221 sp->t = t;
222 sp->pos = pt;
223 sp->norm = norm;
224 sp->obj = face;
225 sp->dbg = bc;
226 return true;
227 }
229 float4 reflect(float4 v, float4 n)
230 {
231 float4 res = 2.0f * dot(v, n) * n - v;
232 return res;
233 }
235 float4 transform(float4 v, global const float *xform)
236 {
237 float4 res;
238 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
239 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
240 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
241 res.w = 0.0;
242 return res;
243 }
245 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans)
246 {
247 ray->origin = transform(ray->origin, xform);
248 ray->dir = transform(ray->dir, invtrans);
249 }
251 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm)
252 {
253 float4 bc = (float4)(0, 0, 0, 0);
255 // calculate area of the whole triangle
256 float4 v1 = face->v[1].pos - face->v[0].pos;
257 float4 v2 = face->v[2].pos - face->v[0].pos;
258 float4 xv1v2 = cross(v1, v2);
260 float area = fabs(dot(xv1v2, norm)) * 0.5;
261 if(area < EPSILON) {
262 return bc;
263 }
265 float4 pv0 = face->v[0].pos - pt;
266 float4 pv1 = face->v[1].pos - pt;
267 float4 pv2 = face->v[2].pos - pt;
269 // calculate the area of each sub-triangle
270 float4 x12 = cross(pv1, pv2);
271 float4 x20 = cross(pv2, pv0);
272 float4 x01 = cross(pv0, pv1);
274 float a0 = fabs(dot(x12, norm)) * 0.5;
275 float a1 = fabs(dot(x20, norm)) * 0.5;
276 float a2 = fabs(dot(x01, norm)) * 0.5;
278 bc.x = a0 / area;
279 bc.y = a1 / area;
280 bc.z = a2 / area;
281 return bc;
282 }