clray

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