3751286d390ac6d3d5e6cac5a364c430d0330b11
[cyberay] / src / geom.c
1 #include <float.h>
2 #include "geom.h"
3
4 void free_bvh_tree(struct bvhnode *tree)
5 {
6         struct bvhnode *node, *tmp;
7
8         free(tree->faces);
9
10         node = tree->sub;
11         while(node) {
12                 tmp = node;
13                 node = node->next;
14                 free_bvh_tree(tmp);
15         }
16
17         free(tree);
18 }
19
20 int ray_triangle(cgm_ray *ray, struct triangle *tri, float tmax, struct rayhit *hit)
21 {
22         float t, ndotdir;
23         cgm_vec3 vdir, bc, pos;
24
25         if(fabs(ndotdir = cgm_vdot(&ray->dir, &tri->norm)) <= 1e-6) {
26                 return 0;
27         }
28
29         vdir = tri->v[0].pos;
30         cgm_vsub(&vdir, &ray->origin);
31
32         if((t = cgm_vdot(&tri->norm, &vdir) / ndotdir) <= 1e-6 || t > tmax) {
33                 return 0;
34         }
35
36         cgm_raypos(&pos, ray, t);
37         cgm_bary(&bc, &tri->v[0].pos, &tri->v[1].pos, &tri->v[2].pos, &pos);
38
39         if(bc.x < 0.0f || bc.x > 1.0f) return 0;
40         if(bc.y < 0.0f || bc.y > 1.0f) return 0;
41         if(bc.z < 0.0f || bc.z > 1.0f) return 0;
42
43         if(hit) {
44                 hit->t = t;
45                 hit->ray = *ray;
46                 hit->mtl = tri->mtl;
47
48                 hit->v.pos = pos;
49
50                 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;
51                 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;
52                 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;
53
54                 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;
55                 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;
56         }
57         return 1;
58 }
59
60 #define SLABCHECK(dim)  \
61         do { \
62                 invdir = 1.0f / ray->dir.dim;   \
63                 t0 = (box->vmin.dim - ray->origin.dim) * invdir;        \
64                 t1 = (box->vmax.dim - ray->origin.dim) * invdir;        \
65                 if(invdir < 0.0f) {     \
66                         tmp = t0;       \
67                         t0 = t1;        \
68                         t1 = tmp;       \
69                 }       \
70                 tmin = t0 > tmin ? t0 : tmin;   \
71                 tmax = t1 < tmax ? t1 : tmax;   \
72                 if(tmax <= tmin) return 0; \
73         } while(0)
74
75 int ray_aabox_any(cgm_ray *ray, struct aabox *box, float tmax)
76 {
77         float invdir, t0, t1, tmp;
78         float tmin = 0.0f;
79
80         SLABCHECK(x);
81         SLABCHECK(y);
82         SLABCHECK(z);
83
84         return 1;
85 }
86
87 int ray_bvhnode(cgm_ray *ray, struct bvhnode *bn, float tmax, struct rayhit *hit)
88 {
89         int i, res = 0;
90         struct rayhit hit0;
91
92         if(!ray_aabox_any(ray, &bn->aabb, tmax)) {
93                 return 0;
94         }
95
96         if(!hit) {
97                 for(i=0; i<bn->num_faces; i++) {
98                         if(ray_triangle(ray, bn->faces + i, tmax, 0)) {
99                                 return 1;
100                         }
101                 }
102                 return 0;
103         }
104
105         hit0.t = FLT_MAX;
106         for(i=0; i<bn->num_faces; i++) {
107                 if(ray_triangle(ray, bn->faces + i, tmax, hit) && hit->t < hit0.t) {
108                         hit0 = *hit;
109                         res = 1;
110                 }
111         }
112         *hit = hit0;
113         return res;
114 }