clray

view rt.cl @ 19:8baea9b66b50

added reflection
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 09 Aug 2010 06:45:57 +0100
parents 4b1604f9798a
children 63a6b46f58a0
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 };
40 struct SurfPoint {
41 float t;
42 float4 pos, norm, dbg;
43 global const struct Face *obj;
44 struct Material mat;
45 };
47 struct Scene {
48 float4 ambient;
49 global const struct Face *faces;
50 int num_faces;
51 global const struct Light *lights;
52 int num_lights;
53 global const struct Material *matlib;
54 };
56 #define MIN_ENERGY 0.001
57 #define EPSILON 1e-6
59 //float4 trace(struct Ray ray, struct Scene *scn);
60 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp);
61 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp);
62 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
64 float4 reflect(float4 v, float4 n);
65 float4 transform(float4 v, global const float *xform);
66 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans);
67 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm);
68 float mean(float4 v);
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 if(!idx) {
83 for(int i=0; i<rinf->num_faces; i++) {
84 outfaces[i] = faces[i];
85 }
86 }
88 struct Scene scn;
89 scn.ambient = rinf->ambient;
90 scn.faces = faces;
91 scn.num_faces = rinf->num_faces;
92 scn.lights = lights;
93 scn.num_lights = rinf->num_lights;
94 scn.matlib = matlib;
96 struct Ray ray = primrays[idx];
97 transform_ray(&ray, xform, invtrans);
99 //fb[idx] = trace(ray, &scn);
101 float4 pixel = (float4)(0, 0, 0, 0);
102 float4 energy = (float4)(1.0, 1.0, 1.0, 1.0);
103 int iter = 0;
105 while(iter++ < rinf->max_iter && mean(energy) > MIN_ENERGY) {
106 struct SurfPoint sp;
107 if(find_intersection(ray, &scn, &sp)) {
108 pixel += shade(ray, &scn, &sp) * energy;
110 float4 refl_col = sp.mat.ks * sp.mat.kr;
112 ray.origin = sp.pos;
113 ray.dir = reflect(-ray.dir, sp.norm);
115 energy *= sp.mat.ks * sp.mat.kr;
116 } else {
117 iter = INT_MAX - 1; // to break out of the loop
118 }
119 }
121 fb[idx] = pixel;
122 }
124 /*float4 trace(struct Ray ray, struct Scene *scn)
125 {
126 float4 color;
127 struct SurfPoint sp;
129 if(find_intersection(ray, scn, &sp)) {
130 color = shade(ray, scn, &sp);
131 } else {
132 color = (float4)(0, 0, 0, 0);
133 }
134 return color;
135 }*/
137 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp)
138 {
139 float4 norm = sp->norm;
140 bool entering = true;
142 if(dot(ray.dir, norm) >= 0.0) {
143 norm = -norm;
144 entering = false;
145 }
147 float4 dcol = scn->ambient * sp->mat.kd;
148 float4 scol = (float4)(0, 0, 0, 0);
150 for(int i=0; i<scn->num_lights; i++) {
151 float4 ldir = scn->lights[i].pos - sp->pos;
153 struct Ray shadowray;
154 shadowray.origin = sp->pos;
155 shadowray.dir = ldir;
157 if(!find_intersection(shadowray, scn, 0)) {
158 ldir = normalize(ldir);
159 float4 vdir = -normalize(ray.dir);
160 float4 vref = reflect(vdir, norm);
162 float diff = fmax(dot(ldir, norm), 0.0f);
163 dcol += sp->mat.kd * diff * scn->lights[i].color;
165 //float spec = powr(fmax(dot(ldir, vref), 0.0f), mat.spow);
166 //scol += sp->mat.ks * spec * scn->lights[i].color;
167 }
168 }
170 return dcol + scol;
171 }
174 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
175 {
176 struct SurfPoint sp, sp0;
177 sp0.t = 1.0;
178 sp0.obj = 0;
180 for(int i=0; i<scn->num_faces; i++) {
181 if(intersect(ray, scn->faces + i, &sp) && sp.t < sp0.t) {
182 sp0 = sp;
183 }
184 }
186 if(!sp0.obj) {
187 return false;
188 }
190 if(spres) {
191 *spres = sp0;
192 spres->mat = scn->matlib[sp0.obj->matid];
193 }
194 return true;
195 }
197 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp)
198 {
199 float4 origin = ray.origin;
200 float4 dir = ray.dir;
201 float4 norm = face->normal;
203 float ndotdir = dot(dir, norm);
205 if(fabs(ndotdir) <= EPSILON) {
206 return false;
207 }
209 float4 pt = face->v[0].pos;
210 float4 vec = pt - origin;
212 float ndotvec = dot(norm, vec);
213 float t = ndotvec / ndotdir;
215 if(t < EPSILON || t > 1.0) {
216 return false;
217 }
218 pt = origin + dir * t;
221 float4 bc = calc_bary(pt, face, norm);
222 float bc_sum = bc.x + bc.y + bc.z;
224 if(bc_sum < 0.0 || bc_sum > 1.0 + EPSILON) {
225 return false;
226 bc *= 1.2;
227 }
229 sp->t = t;
230 sp->pos = pt;
231 sp->norm = norm;
232 sp->obj = face;
233 sp->dbg = bc;
234 return true;
235 }
237 float4 reflect(float4 v, float4 n)
238 {
239 float4 res = 2.0f * dot(v, n) * n - v;
240 return res;
241 }
243 float4 transform(float4 v, global const float *xform)
244 {
245 float4 res;
246 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
247 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
248 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
249 res.w = 0.0;
250 return res;
251 }
253 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans)
254 {
255 ray->origin = transform(ray->origin, xform);
256 ray->dir = transform(ray->dir, invtrans);
257 }
259 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm)
260 {
261 float4 bc = (float4)(0, 0, 0, 0);
263 // calculate area of the whole triangle
264 float4 v1 = face->v[1].pos - face->v[0].pos;
265 float4 v2 = face->v[2].pos - face->v[0].pos;
266 float4 xv1v2 = cross(v1, v2);
268 float area = fabs(dot(xv1v2, norm)) * 0.5;
269 if(area < EPSILON) {
270 return bc;
271 }
273 float4 pv0 = face->v[0].pos - pt;
274 float4 pv1 = face->v[1].pos - pt;
275 float4 pv2 = face->v[2].pos - pt;
277 // calculate the area of each sub-triangle
278 float4 x12 = cross(pv1, pv2);
279 float4 x20 = cross(pv2, pv0);
280 float4 x01 = cross(pv0, pv1);
282 float a0 = fabs(dot(x12, norm)) * 0.5;
283 float a1 = fabs(dot(x20, norm)) * 0.5;
284 float a2 = fabs(dot(x01, norm)) * 0.5;
286 bc.x = a0 / area;
287 bc.y = a1 / area;
288 bc.z = a2 / area;
289 return bc;
290 }
292 float mean(float4 v)
293 {
294 return native_divide(v.x + v.y + v.z, 3.0);
295 }