SHARE
TWEET

Ray Triangle Intersection - SSCCE

a guest Nov 13th, 2013 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <glm.hpp>
  3. #include <gtx\intersect.hpp>
  4.  
  5. const glm::vec3 tri1[] = {glm::vec3(19.0000000, 34.0000000, 2.70000005), glm::vec3(19.0000000, 33.0000000, 2.70000005), glm::vec3(18.0000000, 33.0000000, 2.70000005)};
  6. const glm::vec3 tri2[] = {glm::vec3(18.0000000, 33.0000000, 2.70000005), glm::vec3(18.0000000, 33.0000000, 2.70000005), glm::vec3(19.0000000, 34.0000000, 2.70000005)};
  7.  
  8. const glm::vec3 ray_origin              = glm::vec3(18.5000000, 33.1871414, 10.0000000);
  9. const glm::vec3 ray_direction   = glm::vec3(18.5000000, 33.1871414, -10.0000000);
  10.  
  11. bool normalizedIntersectRayTriangle(glm::vec3 orig, glm::vec3 dir, glm::vec3 vert0, glm::vec3 vert1, glm::vec3 vert2, glm::vec3 &position)
  12. {
  13.         return glm::intersectRayTriangle(glm::normalize(orig), glm::normalize(dir), glm::normalize(vert0), glm::normalize(vert1), glm::normalize(vert2), position);
  14. }
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.         glm::vec3 o;
  19.         if (glm::intersectRayTriangle(ray_origin, ray_direction, tri2[0], tri2[1], tri2[2], o))
  20.         {
  21.                 printf("Result: (%f, %f, %f)\n", o.x, o.y, o.z);
  22.         }
  23.         else if(normalizedIntersectRayTriangle(ray_origin, ray_direction, tri2[0], tri2[1], tri2[2], o))
  24.         {
  25.                 printf("Result: (%f, %f, %f)\n", o.x, o.y, o.z);
  26.         }
  27.         else
  28.         {
  29.                 printf("No intersection found!\n");
  30.         }
  31.         printf("Press any key to exit...\n");
  32.         getchar();
  33.         return 0;
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top