1. bool intersectTree(const Vec3& start, const Vec3& dir,
  2. Triangle** tri, float *best, BIHNode* node, int *tris_tested, float tmin, float tmax) {
  3.  
  4. if(node->isLeaf) {
  5. float u,v,t;
  6. bool hit = node->tri->intersect(start,dir,&u,&v,&t);
  7. (*tris_tested)++;
  8. if(hit && t < *best) {
  9. *best = t;
  10. *tri = node->tri;
  11. return true;
  12. }
  13. }
  14. bool lr=false, rr=false;
  15. int axis = node->axis;
  16.  
  17. float tsplit_l = (node->split[0] - start[axis]) / dir[axis];
  18. float tsplit_r = (node->split[1] - start[axis]) / dir[axis];
  19.  
  20. // (Early Termination possible?)
  21.  
  22. BIHNode *child1 = node->left, *child2 = node->right;
  23. if(dir[axis] <= 0.f) {
  24. std::swap(child1,child2);
  25. std::swap(tsplit_l,tsplit_r);
  26. }
  27.  
  28. if(tmin > tsplit_l && tmax < tsplit_r)
  29. return false;
  30.  
  31. if(child2 != NULL)
  32. if(tsplit_r < tmax)
  33. rr = intersectTree(start,dir,tri,best,child2,tris_tested,tsplit_r,tmax);
  34.  
  35. if(child1 != NULL)
  36. if(tmin < tsplit_l)
  37. lr = intersectTree(start,dir,tri,best,child1,tris_tested,tmin,tsplit_l);
  38.  
  39. return lr | rr;
  40. }