0
votes

I would like to get the intersection point on a triangle when a ray go through it. I followed the on line tutorial and made a function, but I could not get the correct coordinates of intersection point.

For example, I should get the intersection point (0, 3, -1), if I use ray origin (0, 2, -1), ray direction (0, 1, 0), triangle vertices p0 (0, 3, 0), p1 (-0.5, 3, -1), p2 (0.5, 3, -1). However, I have got the intersection point (0, 7, -1), which was not correct.

I appreciate your time and help. :-)

float kEpsilon = 0.000001;

V3f crossProduct(V3f point1, V3f point2){

  V3f vector; 

  vector.x = point1.y * point2.z - point2.y * point1.z; 
  vector.y = point2.x * point1.z - point1.x * point2.z; 
  vector.z = point1.x * point2.y - point1.y * point2.x; 

  return vector;
}

float dotProduct(V3f dot1, V3f dot2){

  float dot = dot1.x * dot2.x + dot1.y * dot2.y + dot1.z * dot2.z; 

  return dot;
}

//orig: ray origin, dir: ray direction, Triangle vertices: p0, p1, p2.  
bool rayTriangleIntersect(V3f orig, V3f dir, V3f p0, V3f p1, V3f p2){ 

// compute plane's normal

  V3f p0p1, p0p2;

  p0p1.x = p1.x - p0.x; 
  p0p1.y = p1.y - p0.y; 
  p0p1.z = p1.z - p0.z; 

  p0p2.x = p2.x - p0.x;
  p0p2.y = p2.y - p0.y; 
  p0p2.z = p2.z - p0.z;

  // no need to normalize
  V3f N = crossProduct(p0p1, p0p2); // N 

  // Step 1: finding P

  // check if ray and plane are parallel ?
  float NdotRayDirection = dotProduct(N, dir); // if the result is 0, the function will return the value false (no intersection).

  if (fabs(NdotRayDirection) < kEpsilon){ // almost 0 

      return false; // they are parallel so they don't intersect ! 
  }

  // compute d parameter using equation 2
  float d = dotProduct(N, p0); 

  // compute t (equation P=O+tR P intersection point ray origin O and its direction R)

  float t = (dotProduct(N, orig) + d) / NdotRayDirection; 

  // check if the triangle is in behind the ray
  //if (t < 0){ return false; } // the triangle is behind 

  // compute the intersection point using equation
  V3f P; 

  //this part should do the work, but it does not work.
  P.x = orig.x + t * dir.x; 
  P.y = orig.y + t * dir.y; 
  P.z = orig.z + t * dir.z; 


  // Step 2: inside-outside test
  V3f C; // vector perpendicular to triangle's plane 

  // edge 0
  V3f edge0; 

  edge0.x = p1.x - p0.x;
  edge0.y = p1.y - p0.y;
  edge0.z = p1.z - p0.z;

  V3f vp0; 

  vp0.x = P.x - p0.x;
  vp0.y = P.y - p0.y; 
  vp0.z = P.z - p0.z; 

  C = crossProduct(edge0, vp0); 

  if (dotProduct(N, C) < 0) { return false; }// P is on the right side 

  // edge 1
  V3f edge1;

  edge1.x = p2.x - p1.x;
  edge1.y = p2.y - p1.y;
  edge1.z = p2.z - p1.z;

  V3f vp1; 

  vp1.x = P.x - p1.x; 
  vp1.y = P.y - p1.y; 
  vp1.z = P.z - p1.z; 

  C = crossProduct(edge1, vp1); 

  if (dotProduct(N, C) < 0) { return false; } // P is on the right side 

  // edge 2
  V3f edge2;

  edge2.x = p0.x - p2.x;    
  edge2.y = p0.y - p2.y;
  edge2.z = p0.z - p2.z;

  V3f vp2; 

  vp2.x = P.x - p2.x;
  vp2.y = P.y - p2.y;
  vp2.z = P.z - p2.z;

  C = crossProduct(edge2, vp2);

  if (dotProduct(N, C) < 0) { return false; } // P is on the right side; 

  return true; // this ray hits the triangle 
} 

Thanks for your help.

1
If you don't want to re-invent the wheel, look at Geometric Tools.Angew is no longer proud of SO
which online tutorial did you use?463035818_is_not_a_number
Hi Angew and tobi303, Thanks for your replies. Angew, I am very new to computer graphics and I could not find the related code on that website. tobi303, this is the link (scratchapixel.com/lessons/3d-basic-rendering/…). Thanks for your help.Ming
Try normalising both N and dir, and see if it helps. I have a suspicion that t is scaled wrong.Angew is no longer proud of SO
Site nav: Browse source > Mathematics > Intersection > Linear-Planar > IntrRay3Triangle3.hAngew is no longer proud of SO

1 Answers

3
votes

You got t=5 and got (0,7,-1) but the correct number for t is 1 and the reason is you have float t = (dotProduct(N, orig) + d) / NdotRayDirection; while the correct code is float t = -((dotProduct(N, orig) - d) / NdotRayDirection); This would solve your problem hopefully.