Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Ray-Triangle Intersection

This topic is 2925 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to do ray-triangle intersection test like it is done at the lighthouse3d.org site here: http://www.lighthouse3d.com/opengl/maths/index.php?raytriint

I think everything is working, except that I'm getting a t value that is the opposite of what I expect. For example when I test a triangle with vertices v0 [-1, -1, -10], v1 [1, -1, -10], v2 [0, 1, -10], and point (p) [0,0,0], and direction vector (d) [0, 0, -1], I'm getting a value of -10 instead of +10 for t.

So when I solve using t to get the intersection point the z-value is always flipped, I wasn't sure if this was something to do with the coordinate system in opengl or if I have an error in my code somewhere?? I went over my code pretty thoroughly to make sure I was doing the macro operations like vector, crossproduct, innerProduct etc the same as they do it on the site and I can't figure out what else could be wrong!

This is C# code, I guess I was just hoping to get an explanation why my t is wrong sign before I just add a negative sign to my calculations!

public static bool RayTriangleIntersectionTest(Vector3d p, Vector3d d, Vector3d v0, Vector3d v1, Vector3d v2)
Vector3d e1 = VectorFromPoints(v0, v1);
Vector3d e2 = VectorFromPoints(v0, v2);

Vector3d h = CrossProduct(d, e2);
double a = DotProduct(e1, h);

if (a > -0.00001 && a < 0.00001)
return false;

double f = 1.0 / a;

Vector3d s = VectorFromPoints(v0, p);
double u = f * DotProduct(s, h);

if (u < 0.0 || u > 1.0)
return (false);

Vector3d q = CrossProduct(s, e1);
double v = f * DotProduct(p, q);

if (v < 0.0 || u + v > 1.0)
return (false);

// at this stage we can compute t to find out where the intersection point is on the line
double t = f * DotProduct(e2, q);

if (t > 0.00001) // ray intersection
return (true);
else // line intersection but not a ray intersection
return (false);

public static Vector3d CrossProduct(Vector3d v0, Vector3d v1)
Vector3d Vout = new Vector3d();

Vout.x = v1.y * v0.z - v0.y * v1.z;
Vout.y = v1.x * v0.z - v0.x * v1.z;
Vout.z = v1.x * v0.y - v0.x * v1.y;

return Vout;

public static double DotProduct(Vector3d v0, Vector3d v1)
return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;

public static Vector3d VectorFromPoints(Vector3d v0, Vector3d v1)
return new Vector3d(v1.x - v0.x, v1.y - v0.y, v1.z - v0.z);


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!