graphics - How to get the intersection point? Ray Triangle Intersection C++ -
i intersection point on triangle when ray go through it. followed on line tutorial , made function, not correct coordinates of intersection point.
for example, should intersection point (0, 3, -1), if 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, have got intersection point (0, 7, -1), not correct.
i appreciate time , 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 normalize v3f n = crossproduct(p0p1, p0p2); // n // step 1: finding p // check if ray , plane parallel ? float ndotraydirection = dotproduct(n, dir); // if result 0, function return value false (no intersection). if (fabs(ndotraydirection) < kepsilon){ // 0 return false; // parallel 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 , direction r) float t = (dotproduct(n, orig) + d) / ndotraydirection; // check if triangle in behind ray //if (t < 0){ return false; } // triangle behind // compute intersection point using equation v3f p; //this part should work, 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 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 on 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 on 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 on right side; return true; // ray hits triangle }
thanks help.
you got t=5 , got (0,7,-1) correct number t 1 , reason have float t = (dotproduct(n, orig) + d) / ndotraydirection;
while correct code float t = -((dotproduct(n, orig) - d) / ndotraydirection);
solve problem hopefully.
Comments
Post a Comment