clray

view src/dbgray.cc @ 62:d9520da6b801

minor readme fix
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Dec 2015 10:31:58 +0200
parents 6a30f27fa1e6
children
line source
1 #include <string.h>
2 #include <assert.h>
3 #include <limits.h>
4 #include "rt.h"
5 #include "ogl.h"
6 #include "vector.h"
7 #include "timer.h"
9 struct SurfPoint {
10 float t;
11 Vector3 pos, norm;
12 const Face *face;
13 };
15 static void trace_ray(float *pixel, const Ray &ray, int iter, float energy = 1.0f);
16 static void shade(float *pixel, const Ray &ray, const SurfPoint &sp, int iter, float energy = 1.0f);
17 static bool find_intersection(const Ray &ray, const Scene *scn, const KDNode *kd, SurfPoint *spret);
18 static bool ray_aabb_test(const Ray &ray, const AABBox &aabb);
19 static bool ray_triangle_test(const Ray &ray, const Face *face, SurfPoint *sp);
20 static Vector3 calc_bary(const Vector3 &pt, const Face *face, const Vector3 &norm);
21 static void transform(float *res, const float *v, const float *xform);
22 static void transform_ray(Ray *ray, const float *xform, const float *invtrans_xform);
24 static int xsz, ysz;
25 static float *fb;
26 static unsigned int tex;
27 static Scene *scn;
28 static const Ray *prim_rays;
29 static int max_iter;
31 static RenderStats *rstat;
32 static int cur_ray_aabb_tests, cur_ray_triangle_tests;
34 bool init_dbg_renderer(int width, int height, Scene *scene, unsigned int texid)
35 {
36 try {
37 fb = new float[3 * width * height];
38 }
39 catch(...) {
40 return false;
41 }
43 xsz = width;
44 ysz = height;
45 tex = texid;
46 scn = scene;
48 rstat = (RenderStats*)get_render_stats();
50 return true;
51 }
53 void destroy_dbg_renderer()
54 {
55 delete [] fb;
56 delete [] prim_rays;
57 }
59 void dbg_set_primary_rays(const Ray *rays)
60 {
61 prim_rays = rays;
62 }
64 void dbg_render(const float *xform, const float *invtrans_xform, int num_threads)
65 {
66 unsigned long t0 = get_msec();
68 max_iter = get_render_option_int(ROPT_ITER);
70 // initialize render-stats
71 memset(rstat, 0, sizeof *rstat);
72 rstat->min_aabb_tests = rstat->min_triangle_tests = INT_MAX;
73 rstat->max_aabb_tests = rstat->max_triangle_tests = 0;
75 int offs = 0;
76 for(int i=0; i<ysz; i++) {
77 for(int j=0; j<xsz; j++) {
78 Ray ray = prim_rays[offs];
79 transform_ray(&ray, xform, invtrans_xform);
81 cur_ray_aabb_tests = cur_ray_triangle_tests = 0;
83 trace_ray(fb + offs * 3, ray, max_iter, 1.0);
84 offs++;
86 // update stats as needed
87 if(cur_ray_aabb_tests < rstat->min_aabb_tests) {
88 rstat->min_aabb_tests = cur_ray_aabb_tests;
89 }
90 if(cur_ray_aabb_tests > rstat->max_aabb_tests) {
91 rstat->max_aabb_tests = cur_ray_aabb_tests;
92 }
93 if(cur_ray_triangle_tests < rstat->min_triangle_tests) {
94 rstat->min_triangle_tests = cur_ray_triangle_tests;
95 }
96 if(cur_ray_triangle_tests > rstat->max_triangle_tests) {
97 rstat->max_triangle_tests = cur_ray_triangle_tests;
98 }
99 rstat->prim_rays++;
100 rstat->aabb_tests += cur_ray_aabb_tests;
101 rstat->triangle_tests += cur_ray_triangle_tests;
102 }
103 }
105 unsigned long t1 = get_msec();
107 glPushAttrib(GL_TEXTURE_BIT);
108 glBindTexture(GL_TEXTURE_2D, tex);
109 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, fb);
110 glPopAttrib();
111 glFinish();
113 rstat->render_time = t1 - t0;
114 rstat->tex_update_time = get_msec() - t1;
116 rstat->rays_cast = rstat->prim_rays + rstat->refl_rays + rstat->shadow_rays;
117 rstat->rays_per_sec = 1000 * rstat->rays_cast / rstat->render_time;
118 rstat->avg_aabb_tests = (float)rstat->aabb_tests / (float)rstat->rays_cast;
119 rstat->avg_triangle_tests = (float)rstat->triangle_tests / (float)rstat->rays_cast;
120 }
122 static void trace_ray(float *pixel, const Ray &ray, int iter, float energy)
123 {
124 SurfPoint sp;
126 if(find_intersection(ray, scn, scn->kdtree, &sp)) {
127 shade(pixel, ray, sp, iter, energy);
128 } else {
129 pixel[0] = pixel[1] = pixel[2] = 0.05f;
130 }
131 }
133 #define MAX(a, b) ((a) > (b) ? (a) : (b))
135 static void shade(float *pixel, const Ray &ray, const SurfPoint &sp, int iter, float energy)
136 {
137 const Material *mat = scn->get_materials() + sp.face->matid;
139 bool cast_shadows = get_render_option_bool(ROPT_SHAD);
140 Vector3 raydir(ray.dir);
141 Vector3 norm = sp.norm;
143 if(dot(raydir, norm) >= 0.0) {
144 norm = -norm;
145 }
147 float dcol[3] = {0, 0, 0};
148 float scol[3] = {0, 0, 0};
150 for(int i=0; i<scn->get_num_lights(); i++) {
151 Vector3 lpos(scn->lights[i].pos);
152 Vector3 ldir = lpos - sp.pos;
154 Ray shadowray;
155 shadowray.origin[0] = sp.pos.x;
156 shadowray.origin[1] = sp.pos.y;
157 shadowray.origin[2] = sp.pos.z;
158 shadowray.dir[0] = ldir.x;
159 shadowray.dir[1] = ldir.y;
160 shadowray.dir[2] = ldir.z;
162 if(!cast_shadows || !find_intersection(shadowray, scn, scn->kdtree, 0)) {
163 rstat->brdf_evals++;
165 ldir.normalize();
167 Vector3 vdir = -raydir / RAY_MAG;
168 Vector3 vref = reflect(vdir, norm);
170 float ndotl = dot(ldir, norm);
171 float diff = MAX(ndotl, 0.0f);
173 dcol[0] += mat->kd[0] * diff;
174 dcol[1] += mat->kd[1] * diff;
175 dcol[2] += mat->kd[2] * diff;
177 float ldotvr = dot(ldir, vref);
178 float spec = pow(MAX(ldotvr, 0.0f), mat->spow);
180 scol[0] += mat->ks[0] * spec;
181 scol[1] += mat->ks[1] * spec;
182 scol[2] += mat->ks[2] * spec;
183 }
185 if(cast_shadows) {
186 rstat->shadow_rays++;
187 }
188 }
190 float refl_color[3];
191 refl_color[0] = mat->ks[0] * mat->kr;
192 refl_color[1] = mat->ks[1] * mat->kr;
193 refl_color[2] = mat->ks[2] * mat->kr;
195 energy *= (refl_color[0] + refl_color[1] + refl_color[2]) / 3.0;
196 if(iter >= 0 && energy > MIN_ENERGY) {
197 Vector3 rdir = reflect(-raydir, norm);
199 Ray refl;
200 refl.origin[0] = sp.pos.x;
201 refl.origin[1] = sp.pos.y;
202 refl.origin[2] = sp.pos.z;
203 refl.dir[0] = rdir.x;
204 refl.dir[1] = rdir.y;
205 refl.dir[2] = rdir.z;
207 float rcol[3];
208 trace_ray(rcol, refl, iter - 1, energy);
209 scol[0] += rcol[0] * mat->ks[0] * mat->kr;
210 scol[1] += rcol[1] * mat->ks[1] * mat->kr;
211 scol[2] += rcol[2] * mat->ks[2] * mat->kr;
213 rstat->refl_rays++;
214 }
216 pixel[0] = dcol[0] + scol[0];
217 pixel[1] = dcol[1] + scol[1];
218 pixel[2] = dcol[2] + scol[2];
219 }
221 static bool find_intersection(const Ray &ray, const Scene *scn, const KDNode *kd, SurfPoint *spret)
222 {
223 if(!ray_aabb_test(ray, kd->aabb)) {
224 return false;
225 }
227 SurfPoint sp, sptmp;
228 if(!spret) {
229 spret = &sptmp;
230 }
232 spret->t = RAY_MAG;
233 spret->face = 0;
235 if(kd->left) {
236 assert(kd->right);
238 bool found = find_intersection(ray, scn, kd->left, spret);
239 if(find_intersection(ray, scn, kd->right, &sp)) {
240 if(!found || sp.t < spret->t) {
241 *spret = sp;
242 }
243 found = true;
244 }
245 return found;
246 }
248 const Face *faces = scn->get_face_buffer();
250 for(size_t i=0; i<kd->face_idx.size(); i++) {
251 if(ray_triangle_test(ray, faces + kd->face_idx[i], &sp) && sp.t < spret->t) {
252 *spret = sp;
253 }
254 }
255 return spret->face != 0;
256 }
258 static bool ray_aabb_test(const Ray &ray, const AABBox &aabb)
259 {
260 cur_ray_aabb_tests++;
262 if(ray.origin[0] >= aabb.min[0] && ray.origin[1] >= aabb.min[1] && ray.origin[2] >= aabb.min[2] &&
263 ray.origin[0] < aabb.max[0] && ray.origin[1] < aabb.max[1] && ray.origin[2] < aabb.max[2]) {
264 return true;
265 }
267 float bbox[][3] = {
268 {aabb.min[0], aabb.min[1], aabb.min[2]},
269 {aabb.max[0], aabb.max[1], aabb.max[2]}
270 };
272 int xsign = (int)(ray.dir[0] < 0.0);
273 float invdirx = 1.0 / ray.dir[0];
274 float tmin = (bbox[xsign][0] - ray.origin[0]) * invdirx;
275 float tmax = (bbox[1 - xsign][0] - ray.origin[0]) * invdirx;
277 int ysign = (int)(ray.dir[1] < 0.0);
278 float invdiry = 1.0 / ray.dir[1];
279 float tymin = (bbox[ysign][1] - ray.origin[1]) * invdiry;
280 float tymax = (bbox[1 - ysign][1] - ray.origin[1]) * invdiry;
282 if(tmin > tymax || tymin > tmax) {
283 return false;
284 }
286 if(tymin > tmin) tmin = tymin;
287 if(tymax < tmax) tmax = tymax;
289 int zsign = (int)(ray.dir[2] < 0.0);
290 float invdirz = 1.0 / ray.dir[2];
291 float tzmin = (bbox[zsign][2] - ray.origin[2]) * invdirz;
292 float tzmax = (bbox[1 - zsign][2] - ray.origin[2]) * invdirz;
294 if(tmin > tzmax || tzmin > tmax) {
295 return false;
296 }
298 return tmin < 1.0 && tmax > 0.0;
300 }
302 static bool ray_triangle_test(const Ray &ray, const Face *face, SurfPoint *sp)
303 {
304 cur_ray_triangle_tests++;
306 Vector3 origin = ray.origin;
307 Vector3 dir = ray.dir;
308 Vector3 norm = face->normal;
310 float ndotdir = dot(dir, norm);
312 if(fabs(ndotdir) <= EPSILON) {
313 return false;
314 }
316 Vector3 pt = face->v[0].pos;
317 Vector3 vec = pt - origin;
319 float ndotvec = dot(norm, vec);
320 float t = ndotvec / ndotdir;
322 if(t < EPSILON || t > 1.0) {
323 return false;
324 }
325 pt = origin + dir * t;
328 Vector3 bc = calc_bary(pt, face, norm);
329 float bc_sum = bc.x + bc.y + bc.z;
331 if(bc_sum < 1.0 - EPSILON || bc_sum > 1.0 + EPSILON) {
332 return false;
333 }
335 Vector3 n0(face->v[0].normal);
336 Vector3 n1(face->v[1].normal);
337 Vector3 n2(face->v[2].normal);
339 sp->t = t;
340 sp->pos = pt;
341 sp->norm = n0 * bc.x + n1 * bc.y + n2 * bc.z;
342 sp->norm.normalize();
343 sp->face = face;
344 return true;
345 }
347 static Vector3 calc_bary(const Vector3 &pt, const Face *face, const Vector3 &norm)
348 {
349 Vector3 bc(0.0f, 0.0f, 0.0f);
351 Vector3 v1 = Vector3(face->v[1].pos) - Vector3(face->v[0].pos);
352 Vector3 v2 = Vector3(face->v[2].pos) - Vector3(face->v[0].pos);
353 Vector3 xv1v2 = cross(v1, v2);
355 float area = fabs(dot(xv1v2, norm)) * 0.5;
356 if(area < EPSILON) {
357 return bc;
358 }
360 Vector3 pv0 = face->v[0].pos - pt;
361 Vector3 pv1 = face->v[1].pos - pt;
362 Vector3 pv2 = face->v[2].pos - pt;
364 // calculate the area of each sub-triangle
365 Vector3 x12 = cross(pv1, pv2);
366 Vector3 x20 = cross(pv2, pv0);
367 Vector3 x01 = cross(pv0, pv1);
369 float a0 = fabs(dot(x12, norm)) * 0.5;
370 float a1 = fabs(dot(x20, norm)) * 0.5;
371 float a2 = fabs(dot(x01, norm)) * 0.5;
373 bc.x = a0 / area;
374 bc.y = a1 / area;
375 bc.z = a2 / area;
376 return bc;
378 }
380 static void transform(float *res, const float *v, const float *xform)
381 {
382 float tmp[3];
383 tmp[0] = v[0] * xform[0] + v[1] * xform[4] + v[2] * xform[8] + xform[12];
384 tmp[1] = v[0] * xform[1] + v[1] * xform[5] + v[2] * xform[9] + xform[13];
385 tmp[2] = v[0] * xform[2] + v[1] * xform[6] + v[2] * xform[10] + xform[14];
386 memcpy(res, tmp, sizeof tmp);
387 }
389 static void transform_ray(Ray *ray, const float *xform, const float *invtrans_xform)
390 {
391 transform(ray->origin, ray->origin, xform);
392 transform(ray->dir, ray->dir, invtrans_xform);
393 }