rev |
line source |
nuclear@12
|
1 /* vim: set ft=opencl:ts=4:sw=4 */
|
nuclear@12
|
2
|
nuclear@2
|
3 struct RendInfo {
|
nuclear@22
|
4 float4 ambient;
|
nuclear@2
|
5 int xsz, ysz;
|
nuclear@9
|
6 int num_faces, num_lights;
|
nuclear@2
|
7 int max_iter;
|
nuclear@28
|
8 int kd_depth;
|
nuclear@2
|
9 };
|
nuclear@2
|
10
|
nuclear@9
|
11 struct Vertex {
|
nuclear@2
|
12 float4 pos;
|
nuclear@9
|
13 float4 normal;
|
nuclear@12
|
14 float4 tex;
|
nuclear@12
|
15 float4 padding;
|
nuclear@9
|
16 };
|
nuclear@9
|
17
|
nuclear@9
|
18 struct Face {
|
nuclear@9
|
19 struct Vertex v[3];
|
nuclear@9
|
20 float4 normal;
|
nuclear@9
|
21 int matid;
|
nuclear@12
|
22 int padding[3];
|
nuclear@9
|
23 };
|
nuclear@9
|
24
|
nuclear@9
|
25 struct Material {
|
nuclear@5
|
26 float4 kd, ks;
|
nuclear@9
|
27 float kr, kt;
|
nuclear@9
|
28 float spow;
|
nuclear@12
|
29 float padding;
|
nuclear@2
|
30 };
|
nuclear@2
|
31
|
nuclear@3
|
32 struct Light {
|
nuclear@3
|
33 float4 pos, color;
|
nuclear@3
|
34 };
|
nuclear@3
|
35
|
nuclear@2
|
36 struct Ray {
|
nuclear@2
|
37 float4 origin, dir;
|
nuclear@2
|
38 };
|
nuclear@2
|
39
|
nuclear@2
|
40 struct SurfPoint {
|
nuclear@2
|
41 float t;
|
nuclear@12
|
42 float4 pos, norm, dbg;
|
nuclear@9
|
43 global const struct Face *obj;
|
nuclear@19
|
44 struct Material mat;
|
nuclear@2
|
45 };
|
nuclear@2
|
46
|
nuclear@16
|
47 struct Scene {
|
nuclear@16
|
48 float4 ambient;
|
nuclear@16
|
49 global const struct Face *faces;
|
nuclear@16
|
50 int num_faces;
|
nuclear@16
|
51 global const struct Light *lights;
|
nuclear@16
|
52 int num_lights;
|
nuclear@16
|
53 global const struct Material *matlib;
|
nuclear@28
|
54 global const struct KDNode *kdtree;
|
nuclear@28
|
55 };
|
nuclear@28
|
56
|
nuclear@28
|
57 struct AABBox {
|
nuclear@28
|
58 float4 min, max;
|
nuclear@28
|
59 };
|
nuclear@28
|
60
|
nuclear@28
|
61 struct KDNode {
|
nuclear@28
|
62 AABBox aabb;
|
nuclear@28
|
63 int face_idx[32];
|
nuclear@28
|
64 int num_faces;
|
nuclear@28
|
65 int padding[3];
|
nuclear@16
|
66 };
|
nuclear@2
|
67
|
nuclear@16
|
68 #define MIN_ENERGY 0.001
|
nuclear@21
|
69 #define EPSILON 1e-5
|
nuclear@16
|
70
|
nuclear@16
|
71 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp);
|
nuclear@16
|
72 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *sp);
|
nuclear@9
|
73 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp);
|
nuclear@28
|
74 bool intersect_aabb(struct Ray ray, struct AABBox aabb);
|
nuclear@16
|
75
|
nuclear@8
|
76 float4 reflect(float4 v, float4 n);
|
nuclear@8
|
77 float4 transform(float4 v, global const float *xform);
|
nuclear@16
|
78 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans);
|
nuclear@12
|
79 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm);
|
nuclear@19
|
80 float mean(float4 v);
|
nuclear@4
|
81
|
nuclear@4
|
82 kernel void render(global float4 *fb,
|
nuclear@4
|
83 global const struct RendInfo *rinf,
|
nuclear@9
|
84 global const struct Face *faces,
|
nuclear@9
|
85 global const struct Material *matlib,
|
nuclear@4
|
86 global const struct Light *lights,
|
nuclear@7
|
87 global const struct Ray *primrays,
|
nuclear@12
|
88 global const float *xform,
|
nuclear@28
|
89 global const float *invtrans,
|
nuclear@28
|
90 global const struct KDNode *kdtree)
|
nuclear@2
|
91 {
|
nuclear@2
|
92 int idx = get_global_id(0);
|
nuclear@2
|
93
|
nuclear@16
|
94 struct Scene scn;
|
nuclear@16
|
95 scn.ambient = rinf->ambient;
|
nuclear@16
|
96 scn.faces = faces;
|
nuclear@16
|
97 scn.num_faces = rinf->num_faces;
|
nuclear@16
|
98 scn.lights = lights;
|
nuclear@16
|
99 scn.num_lights = rinf->num_lights;
|
nuclear@16
|
100 scn.matlib = matlib;
|
nuclear@8
|
101
|
nuclear@16
|
102 struct Ray ray = primrays[idx];
|
nuclear@16
|
103 transform_ray(&ray, xform, invtrans);
|
nuclear@4
|
104
|
nuclear@19
|
105 float4 pixel = (float4)(0, 0, 0, 0);
|
nuclear@22
|
106 float4 energy = (float4)(1.0, 1.0, 1.0, 0.0);
|
nuclear@19
|
107 int iter = 0;
|
nuclear@19
|
108
|
nuclear@19
|
109 while(iter++ < rinf->max_iter && mean(energy) > MIN_ENERGY) {
|
nuclear@19
|
110 struct SurfPoint sp;
|
nuclear@19
|
111 if(find_intersection(ray, &scn, &sp)) {
|
nuclear@19
|
112 pixel += shade(ray, &scn, &sp) * energy;
|
nuclear@19
|
113
|
nuclear@19
|
114 float4 refl_col = sp.mat.ks * sp.mat.kr;
|
nuclear@19
|
115
|
nuclear@19
|
116 ray.origin = sp.pos;
|
nuclear@19
|
117 ray.dir = reflect(-ray.dir, sp.norm);
|
nuclear@19
|
118
|
nuclear@19
|
119 energy *= sp.mat.ks * sp.mat.kr;
|
nuclear@19
|
120 } else {
|
nuclear@19
|
121 iter = INT_MAX - 1; // to break out of the loop
|
nuclear@19
|
122 }
|
nuclear@17
|
123 }
|
nuclear@19
|
124
|
nuclear@19
|
125 fb[idx] = pixel;
|
nuclear@4
|
126 }
|
nuclear@4
|
127
|
nuclear@16
|
128 float4 shade(struct Ray ray, struct Scene *scn, const struct SurfPoint *sp)
|
nuclear@16
|
129 {
|
nuclear@16
|
130 float4 norm = sp->norm;
|
nuclear@12
|
131 bool entering = true;
|
nuclear@12
|
132
|
nuclear@12
|
133 if(dot(ray.dir, norm) >= 0.0) {
|
nuclear@12
|
134 norm = -norm;
|
nuclear@12
|
135 entering = false;
|
nuclear@12
|
136 }
|
nuclear@12
|
137
|
nuclear@19
|
138 float4 dcol = scn->ambient * sp->mat.kd;
|
nuclear@8
|
139 float4 scol = (float4)(0, 0, 0, 0);
|
nuclear@5
|
140
|
nuclear@16
|
141 for(int i=0; i<scn->num_lights; i++) {
|
nuclear@16
|
142 float4 ldir = scn->lights[i].pos - sp->pos;
|
nuclear@5
|
143
|
nuclear@16
|
144 struct Ray shadowray;
|
nuclear@16
|
145 shadowray.origin = sp->pos;
|
nuclear@16
|
146 shadowray.dir = ldir;
|
nuclear@5
|
147
|
nuclear@16
|
148 if(!find_intersection(shadowray, scn, 0)) {
|
nuclear@16
|
149 ldir = normalize(ldir);
|
nuclear@16
|
150 float4 vdir = -normalize(ray.dir);
|
nuclear@16
|
151 float4 vref = reflect(vdir, norm);
|
nuclear@16
|
152
|
nuclear@16
|
153 float diff = fmax(dot(ldir, norm), 0.0f);
|
nuclear@22
|
154 dcol += sp->mat.kd * scn->lights[i].color * diff;
|
nuclear@16
|
155
|
nuclear@20
|
156 float spec = powr(fmax(dot(ldir, vref), 0.0f), sp->mat.spow);
|
nuclear@22
|
157 scol += sp->mat.ks * scn->lights[i].color * spec;
|
nuclear@16
|
158 }
|
nuclear@16
|
159 }
|
nuclear@16
|
160
|
nuclear@8
|
161 return dcol + scol;
|
nuclear@2
|
162 }
|
nuclear@2
|
163
|
nuclear@28
|
164 bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
|
nuclear@28
|
165 {
|
nuclear@28
|
166 return false;
|
nuclear@28
|
167 }
|
nuclear@16
|
168
|
nuclear@28
|
169 /*bool find_intersection(struct Ray ray, const struct Scene *scn, struct SurfPoint *spres)
|
nuclear@12
|
170 {
|
nuclear@16
|
171 struct SurfPoint sp, sp0;
|
nuclear@16
|
172 sp0.t = 1.0;
|
nuclear@16
|
173 sp0.obj = 0;
|
nuclear@16
|
174
|
nuclear@16
|
175 for(int i=0; i<scn->num_faces; i++) {
|
nuclear@16
|
176 if(intersect(ray, scn->faces + i, &sp) && sp.t < sp0.t) {
|
nuclear@16
|
177 sp0 = sp;
|
nuclear@16
|
178 }
|
nuclear@16
|
179 }
|
nuclear@16
|
180
|
nuclear@16
|
181 if(!sp0.obj) {
|
nuclear@16
|
182 return false;
|
nuclear@16
|
183 }
|
nuclear@16
|
184
|
nuclear@16
|
185 if(spres) {
|
nuclear@16
|
186 *spres = sp0;
|
nuclear@19
|
187 spres->mat = scn->matlib[sp0.obj->matid];
|
nuclear@16
|
188 }
|
nuclear@16
|
189 return true;
|
nuclear@28
|
190 }*/
|
nuclear@12
|
191
|
nuclear@16
|
192 bool intersect(struct Ray ray, global const struct Face *face, struct SurfPoint *sp)
|
nuclear@2
|
193 {
|
nuclear@12
|
194 float4 origin = ray.origin;
|
nuclear@12
|
195 float4 dir = ray.dir;
|
nuclear@12
|
196 float4 norm = face->normal;
|
nuclear@12
|
197
|
nuclear@16
|
198 float ndotdir = dot(dir, norm);
|
nuclear@12
|
199
|
nuclear@9
|
200 if(fabs(ndotdir) <= EPSILON) {
|
nuclear@9
|
201 return false;
|
nuclear@9
|
202 }
|
nuclear@2
|
203
|
nuclear@9
|
204 float4 pt = face->v[0].pos;
|
nuclear@12
|
205 float4 vec = pt - origin;
|
nuclear@2
|
206
|
nuclear@16
|
207 float ndotvec = dot(norm, vec);
|
nuclear@9
|
208 float t = ndotvec / ndotdir;
|
nuclear@2
|
209
|
nuclear@2
|
210 if(t < EPSILON || t > 1.0) {
|
nuclear@2
|
211 return false;
|
nuclear@2
|
212 }
|
nuclear@12
|
213 pt = origin + dir * t;
|
nuclear@9
|
214
|
nuclear@12
|
215
|
nuclear@12
|
216 float4 bc = calc_bary(pt, face, norm);
|
nuclear@9
|
217 float bc_sum = bc.x + bc.y + bc.z;
|
nuclear@9
|
218
|
nuclear@20
|
219 if(bc_sum < 1.0 - EPSILON || bc_sum > 1.0 + EPSILON) {
|
nuclear@9
|
220 return false;
|
nuclear@12
|
221 bc *= 1.2;
|
nuclear@9
|
222 }
|
nuclear@2
|
223
|
nuclear@2
|
224 sp->t = t;
|
nuclear@9
|
225 sp->pos = pt;
|
nuclear@21
|
226 sp->norm = normalize(face->v[0].normal * bc.x + face->v[1].normal * bc.y + face->v[2].normal * bc.z);
|
nuclear@9
|
227 sp->obj = face;
|
nuclear@12
|
228 sp->dbg = bc;
|
nuclear@2
|
229 return true;
|
nuclear@2
|
230 }
|
nuclear@5
|
231
|
nuclear@28
|
232 bool intersect_aabb(struct Ray ray, struct AABBox aabb)
|
nuclear@28
|
233 {
|
nuclear@28
|
234 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z &&
|
nuclear@28
|
235 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) {
|
nuclear@28
|
236 return true;
|
nuclear@28
|
237 }
|
nuclear@28
|
238
|
nuclear@28
|
239 float4 bbox[2] = {aabb.min, aabb.max};
|
nuclear@28
|
240
|
nuclear@28
|
241 int xsign = (int)(ray.dir.x < 0.0);
|
nuclear@28
|
242 float invdirx = 1.0 / ray.dir.x;
|
nuclear@28
|
243 float tmin = (bbox[xsign].x - ray.origin.x) * invdirx;
|
nuclear@28
|
244 float tmax = (bbox[1 - xsign].x - ray.origin.x) * invdirx;
|
nuclear@28
|
245
|
nuclear@28
|
246 int ysign = (int)(ray.dir.y < 0.0);
|
nuclear@28
|
247 float invdiry = 1.0 / ray.dir.y;
|
nuclear@28
|
248 float tymin = (bbox[ysign].y - ray.origin.y) * invdiry;
|
nuclear@28
|
249 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry;
|
nuclear@28
|
250
|
nuclear@28
|
251 if(tmin > tymax || tymin > tmax) {
|
nuclear@28
|
252 return false;
|
nuclear@28
|
253 }
|
nuclear@28
|
254
|
nuclear@28
|
255 if(tymin > tmin) tmin = tymin;
|
nuclear@28
|
256 if(tymax < tmax) tmax = tymax;
|
nuclear@28
|
257
|
nuclear@28
|
258 int zsign = (int)(ray.dir.z < 0.0);
|
nuclear@28
|
259 float invdirz = 1.0 / ray.dir.z;
|
nuclear@28
|
260 float tzmin = (bbox[zsign].z - ray.origin.z) * invdirz;
|
nuclear@28
|
261 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz;
|
nuclear@28
|
262
|
nuclear@28
|
263 if(tmin > tzmax || tzmin > tmax) {
|
nuclear@28
|
264 return false;
|
nuclear@28
|
265 }
|
nuclear@28
|
266
|
nuclear@28
|
267 return tmin < t1 && tmax > t0;
|
nuclear@28
|
268 }
|
nuclear@28
|
269
|
nuclear@8
|
270 float4 reflect(float4 v, float4 n)
|
nuclear@5
|
271 {
|
nuclear@23
|
272 return 2.0f * dot(v, n) * n - v;
|
nuclear@5
|
273 }
|
nuclear@8
|
274
|
nuclear@8
|
275 float4 transform(float4 v, global const float *xform)
|
nuclear@8
|
276 {
|
nuclear@8
|
277 float4 res;
|
nuclear@8
|
278 res.x = v.x * xform[0] + v.y * xform[4] + v.z * xform[8] + xform[12];
|
nuclear@8
|
279 res.y = v.x * xform[1] + v.y * xform[5] + v.z * xform[9] + xform[13];
|
nuclear@8
|
280 res.z = v.x * xform[2] + v.y * xform[6] + v.z * xform[10] + xform[14];
|
nuclear@12
|
281 res.w = 0.0;
|
nuclear@8
|
282 return res;
|
nuclear@8
|
283 }
|
nuclear@8
|
284
|
nuclear@16
|
285 void transform_ray(struct Ray *ray, global const float *xform, global const float *invtrans)
|
nuclear@8
|
286 {
|
nuclear@16
|
287 ray->origin = transform(ray->origin, xform);
|
nuclear@16
|
288 ray->dir = transform(ray->dir, invtrans);
|
nuclear@8
|
289 }
|
nuclear@9
|
290
|
nuclear@12
|
291 float4 calc_bary(float4 pt, global const struct Face *face, float4 norm)
|
nuclear@9
|
292 {
|
nuclear@12
|
293 float4 bc = (float4)(0, 0, 0, 0);
|
nuclear@9
|
294
|
nuclear@12
|
295 // calculate area of the whole triangle
|
nuclear@12
|
296 float4 v1 = face->v[1].pos - face->v[0].pos;
|
nuclear@12
|
297 float4 v2 = face->v[2].pos - face->v[0].pos;
|
nuclear@12
|
298 float4 xv1v2 = cross(v1, v2);
|
nuclear@12
|
299
|
nuclear@16
|
300 float area = fabs(dot(xv1v2, norm)) * 0.5;
|
nuclear@9
|
301 if(area < EPSILON) {
|
nuclear@9
|
302 return bc;
|
nuclear@9
|
303 }
|
nuclear@9
|
304
|
nuclear@9
|
305 float4 pv0 = face->v[0].pos - pt;
|
nuclear@9
|
306 float4 pv1 = face->v[1].pos - pt;
|
nuclear@9
|
307 float4 pv2 = face->v[2].pos - pt;
|
nuclear@9
|
308
|
nuclear@12
|
309 // calculate the area of each sub-triangle
|
nuclear@12
|
310 float4 x12 = cross(pv1, pv2);
|
nuclear@12
|
311 float4 x20 = cross(pv2, pv0);
|
nuclear@12
|
312 float4 x01 = cross(pv0, pv1);
|
nuclear@12
|
313
|
nuclear@16
|
314 float a0 = fabs(dot(x12, norm)) * 0.5;
|
nuclear@16
|
315 float a1 = fabs(dot(x20, norm)) * 0.5;
|
nuclear@16
|
316 float a2 = fabs(dot(x01, norm)) * 0.5;
|
nuclear@9
|
317
|
nuclear@9
|
318 bc.x = a0 / area;
|
nuclear@9
|
319 bc.y = a1 / area;
|
nuclear@9
|
320 bc.z = a2 / area;
|
nuclear@9
|
321 return bc;
|
nuclear@9
|
322 }
|
nuclear@19
|
323
|
nuclear@19
|
324 float mean(float4 v)
|
nuclear@19
|
325 {
|
nuclear@19
|
326 return native_divide(v.x + v.y + v.z, 3.0);
|
nuclear@19
|
327 }
|