Sign in to follow this  
Icebraker

Triangle intersection by Moeller and trumbore

Recommended Posts

Hi, I'm implementing a triangle-based raytracer using the intersection code by Moeller and Trumbore from the paper "Fast Minimal Storage Ray/Triangle intersection. One thing I am not quite sure about is how to compute the actual intersection point from the returned values u,v,t. As t is the distance to the plane the triangle lies in, I can't just say intersect = origin + t * direction as t will only give me the distance to the point on the plane closest to my camera. Does anybody know how to compute the exact intersection point in 3D? Thanks, Icebraker

Share this post


Link to post
Share on other sites
How does it only give distance? origin is a point, direction is a vector, t is the distance from the ray.origin along ray.direction to intersection point on triangle.
u and v help determine if there was an intersection on the triangle, t is a valid distance if there is an intersection. so if u and v say there was an intersection, the point is ray.origin + (ray.dir * t).
Origin doesnt have to have anything to do with the camera, its the ray origin, it can be a point in space anywhere.

[Edited by - NumberXaero on July 25, 2008 7:17:25 PM]

Share this post


Link to post
Share on other sites
Well, he paper says that t is the distance to the closest point on the plane in which the triangle lies. This distance is (I think) not the distance the ray travels till it reaches the triangle. So if I take that formula, I will end up somewhere in front of the triangle...

Share this post


Link to post
Share on other sites
No, NumberXaero is right: if you reach the end of the function, this means that the intersection point is isnside the triangle, and t is actually the distance from the ray origin.
origin + t * direction actually returns the world-space intersection point...

Share this post


Link to post
Share on other sites
Ok, I have computed the point of intersection as eye+t*rayDir, and then I'm clling this method which gets recursively called if the surface is reflective until maxNumRaflects has been reached, then it goes in the else-part and performs a simple phong illumination which works fine.. Does anybody spot a mistake?

Vec4f ray::phongLighting(vector<triangle>::iterator it,double u, double v,Vec3f pointOfIntersection,Vec3f origin,Vec3f dir,vector<triangle> *triangleList,uint refractCounter) {
Vec4f result=Vec4f(0.0,0.0,0.0,1.0);
dir.Normalize();

if(refractCounter>maxNumOfRefracts) return result;
else if(it->reflectiveSurface()==true) {
Vec4f tempResult;
Vec3f normal = (1-u-v)*it->getNormals(0)+u*it->getNormals(1)+v*it->getNormals(2);
normal.Normalize();

//! Add reflection
Vec3f reflect = -2.0*(dir.Dot3(normal))*normal+dir;
reflect.Normalize();

double distance=1000000.0;
for(vector<triangle>::iterator itReflect = triangleList->begin(); itReflect != triangleList->end(); itReflect++) {
Vec3f vertex0(itReflect->getVertices(0,0),itReflect->getVertices(0,1),itReflect->getVertices(0,2));
Vec3f vertex1(itReflect->getVertices(1,0),itReflect->getVertices(1,1),itReflect->getVertices(1,2));
Vec3f vertex2(itReflect->getVertices(2,0),itReflect->getVertices(2,1),itReflect->getVertices(2,2));

double t,u,v;
if(intersect_triangle(pointOfIntersection+0.001*normal,reflect,vertex0,vertex1,vertex2,&t,&u,&v)==1) {
if(distance>t) {
distance=t;
int newRefractCounter=refractCounter+1;
Vec3f newPointOfIntersection=pointOfIntersection+t*reflect;
result=phongLighting(itReflect,u,v,newPointOfIntersection,pointOfIntersection,reflect,triangleList,newRefractCounter);
}
}
}
if(distance>999999.0) result=Vec4f(0.0,0.0,0.0,1.0);
else {
//! Do normal phong lighting
}

[Edited by - Icebraker on July 28, 2008 12:24:21 AM]

Share this post


Link to post
Share on other sites
You should go over all the triangles and find the minimum t amongst all the intersections, and, assuming you hit anything at all, THEN do the recursion.

Also, it's smart to separate the code that finds the closest intersection and the code that does the lighting.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this