Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. int iLeafIndex = -1;
  2. Contact contact; // Pass this to the CheckOverlap function and overwrite a_Contact when the collision time of contact is lower.
  3. contact.m_fCollisionTime = std::numeric_limits<float>::max();
  4.  
  5. // Allocate memory..
  6. std::stack<Node*> queueNodes;
  7.  
  8. // Traversing, start with the root node.
  9. Node* currentNode = *m_tree;
  10.  
  11. // There is only one object.
  12. if (currentNode->m_Type == LEAF) {
  13. if (CheckOverlap(a_Ray, currentNode, iLeafIndex, contact)) {
  14. a_Contact = contact;
  15. a_Shape = currentNode->m_Shapes[iLeafIndex];
  16. }
  17. return;
  18. }
  19.  
  20. do
  21. {
  22. // Get the node childrens.
  23. Node* childNodeL = (currentNode->m_NodeLeft != NULL) ? currentNode->m_NodeLeft : NULL;
  24. Node* childNodeR = (currentNode->m_NodeRight != NULL) ? currentNode->m_NodeRight : NULL;
  25.  
  26. // Check for intersection with the left node or leaf.
  27. bool bOverlapNodeL = CheckOverlap(a_Ray, childNodeL, iLeafIndex, contact);
  28.  
  29. // Overwrite the shape and contact data if the left node is a leaf and the collision time is closer by.
  30. if (bOverlapNodeL && childNodeL->m_Type == LEAF && a_Contact.m_fCollisionTime > contact.m_fCollisionTime) {
  31. a_Contact = contact;
  32. a_Shape = childNodeL->m_Shapes[iLeafIndex];
  33. }
  34.  
  35. // Check for intersection with the right node or leaf.
  36. bool bOverlapNodeR = CheckOverlap(a_Ray, childNodeR, iLeafIndex, contact);
  37.  
  38. // Overwrite the shape and contact data if the right node is a leaf and the collision time is closer by.
  39. if (bOverlapNodeR && childNodeR->m_Type == LEAF && a_Contact.m_fCollisionTime > contact.m_fCollisionTime) {
  40. a_Contact = contact;
  41. a_Shape = childNodeR->m_Shapes[iLeafIndex];
  42. }
  43.  
  44. // Traverse further into the left or right node when there is an overlap and it is not a leaf.
  45. bool bTraverseL = (bOverlapNodeL && childNodeL->m_Type != LEAF);
  46. bool bTraverseR = (bOverlapNodeR && childNodeR->m_Type != LEAF);
  47.  
  48. // We cannot traverse further into this node. Look in the stack if there is another option we haven't traversed into.
  49. if (!bTraverseL && !bTraverseR) {
  50. if (!queueNodes.empty()) {
  51. currentNode = queueNodes.top();
  52. queueNodes.pop();
  53. }
  54. else {
  55. currentNode = NULL;
  56. }
  57. }
  58. else
  59. {
  60. // First, go through the left node and when we cannot traverse there, look into the right node.
  61. currentNode = (bTraverseL) ? childNodeL : childNodeR;
  62.  
  63. // Add the right child node to the stack, because there is an overlap with both the nodes and we are going through the left node first.
  64. if (bTraverseL && bTraverseR) {
  65. queueNodes.push(childNodeR);
  66. }
  67. }
  68. } while (currentNode != NULL);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement