Hi! I'm trying to traverse a 3D KD-Tree in my raytracer. The Tree is correct, but there seems to be something wrong with my traversal algorithm since I'm getting some errors compared to using a brute-force approach (some small surface areas seem to get ignored).
Note: none of the rays in question is parallel to any axis.
This is my traversal algorithm:
IntersectionData* intersectKDTree(const Ray &ray, KDTreeNode* node, double tMin, double tMax) const{
if (node->GetObjectCount()==0) return 0;
IntersectionData* current = 0;
bool intersected = false;
if (node->m_isLeaf){
...test all primitives in the leaf...
}
else{
int axis = node->m_splitAxis;
double splitPos = node->m_splitPos;
double tSplit = (splitPos-ray.point[axis])/ray.direction[axis];
KDTreeNode* nearNode = ray.point[axis]<splitPos?node->m_leftnode:node->m_rightnode;
KDTreeNode* farNode = ray.point[axis]<splitPos?node->m_rightnode:node->m_leftnode;
if (tSplit > tMax)
return intersectKDTree(ray, nearNode , tMin, tMax);//case A
else if (tSplit < tMin){
if(tSplit>0)
return intersectKDTree(ray, farNode, tMin, tMax);//case B
else if(tSplit<0)
return intersectKDTree(ray, nearNode, tMin,tMax);//case C
else{//tSplit==0
if(ray.direction[axis]<0)
return intersectKDTree(ray, farNode, tMin, tMax);//case D
else
return intersectKDTree(ray, nearNode, tMin, tMax);//case E
}
}
else{
if(tSplit>0){//case F
current = intersectKDTree(ray, nearNode, tMin, tSplit);
if (current != 0)
return current;
else
return intersectKDTree(ray, farNode, tSplit, tMax);
}
else{
return intersectKDTree(ray,nearNode,tSplit, tMax);//case G
}
}
}
}
I created a graphic with all different cases:
Am I missing a case?
thank you for the help!