4 int ray_triangle(cgm_ray *ray, struct triangle *tri, float tmax, struct rayhit *hit)
9 if(fabs(ndotdir = cgm_vdot(&ray->dir, &tri->norm)) <= 1e-6) {
14 cgm_vsub(&vdir, &ray->origin);
16 if((t = cgm_vdot(&ray->dir, &vdir) / ndotdir) <= 1e-6 || t > tmax) {
25 cgm_raypos(&hit->v.pos, ray, t);
26 cgm_bary(&bc, &tri->v[0].pos, &tri->v[1].pos, &tri->v[2].pos, &hit->v.pos);
28 hit->v.norm.x = tri->v[0].norm.x * bc.x + tri->v[1].norm.x * bc.y + tri->v[2].norm.x * bc.z;
29 hit->v.norm.y = tri->v[0].norm.y * bc.x + tri->v[1].norm.y * bc.y + tri->v[2].norm.y * bc.z;
30 hit->v.norm.z = tri->v[0].norm.z * bc.x + tri->v[1].norm.z * bc.y + tri->v[2].norm.z * bc.z;
32 hit->v.tex.x = tri->v[0].tex.x * bc.x + tri->v[1].tex.x * bc.y + tri->v[2].tex.x * bc.z;
33 hit->v.tex.y = tri->v[0].tex.y * bc.x + tri->v[1].tex.y * bc.y + tri->v[2].tex.y * bc.z;
38 #define SLABCHECK(dim) \
40 min = box->p.dim - box->r.dim; \
41 max = box->p.dim + box->r.dim; \
42 invdir = 1.0f / ray->dir.dim; \
43 t0 = (min - ray->origin.dim) * invdir; \
44 t1 = (max - ray->origin.dim) * invdir; \
50 tmin = t0 > tmin ? t0 : tmin; \
51 tmax = t1 < tmax ? t1 : tmax; \
52 if(tmax <= tmin) return 0; \
55 int ray_aabox_any(cgm_ray *ray, struct aabox *box, float tmax)
57 float invdir, t0, t1, tmp;
58 float min, max, tmin = 0.0f;
67 int ray_bvhnode(cgm_ray *ray, struct bvhnode *bn, float tmax, struct rayhit *hit)
72 if(!ray_aabox_any(ray, &bn->aabb, tmax)) {
77 for(i=0; i<bn->num_faces; i++) {
78 if(ray_triangle(ray, bn->faces[i], tmax, 0)) {
86 for(i=0; i<bn->num_faces; i++) {
87 if(ray_triangle(ray, bn->faces[i], tmax, hit) && hit->t < hit0.t) {