Jump to content
  • Advertisement
Sign in to follow this  
Speedster

Problem with Ingo Wald's Ray/Triangle Intersection Test

This topic is 3248 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

Hi guys. I'm new to this forum. I'm hoping that someone here could assist me in solving a problem I've been having with Ingo Wald's ray/triangle intersection test (found pg 91-92 on his PHD thesis). I'm currently building a ray tracer that only uses triangles as primitives. The triangle meshes are loaded from *.obj files which are exported from 3ds Max 2009. At the moment I'm only testing intersections between the primary ray and triangles in the scene. There is no shading. When a primary ray intersects a triangle I simply set the color of the appropriate pixel in the output image to be equal to the triangle's color. One primary ray is sent straight through each pixel in the negative Z direction (0,0,-1). The problem with Wald's intersection test is that if I choose a projection dimension for a triangle to be X (the triangle's absolute maximum normal component is the X component), then I would get incorrect intersections. Only if I choose Z to be the triangles projection dimension would I get correct intersections. Likewise if I instead cast rays into the negative X direction (-1,0,0) then all triangles must have their projection dimensions set to X for correct intersections. I've provided some example outputs from my ray tracer to illustrate the problem that I'm experiencing. Primary rays are cast through every pixel in the negative Z direction (0,0,-1). There is only one object in the scene: A sphere. The first image shows the incorrect intersections that occur when the projection dimensions for triangles are chosen according to their maximum absolute normal component. The second image shows the correct intersections that occur only when the projection dimensions for all triangles are explicitly set to Z. Image1: http://i942.photobucket.com/albums/ad269/ArcadeSpeedster/Image2.jpg Image2: http://i942.photobucket.com/albums/ad269/ArcadeSpeedster/Image3.jpg Any help with my problem would be much appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Sorry about that biki_. The intersection code:

public bool Intersect(ref Ray ray, ref float beta, ref float gamma)
{
//v0 = triangle vertex 0
//v1 = triangle vertex 1
//v2 = triangle vertex 2

edge1 = v1 - v0;
edge2 = v2 - v0;

normal = edge2.CrossProduct(edge1);
normal.Normalize();

if (Math.Abs(normal.x) > Math.Abs(normal.y))
{
if (Math.Abs(normal.x) > Math.Abs(normal.z))
k = 0; //Projection dimension = X
else
k = 2; //Projection dimension = Z
}
else
{
if (Math.Abs(normal.y) > Math.Abs(normal.z))
k = 1; //Projection dimension = Y
else
k = 2; //Projection dimension = Z
}

ku = ((k + 1) % 3);
kv = ((k + 2) % 3);

//Note: tPlane is always correct, irrespective of the projection dimension of the triangle.
float tPlane = (-1) * (((ray.origin - v0) * normal) / (ray.direction * (normal)));
if (tPlane < 0 || tPlane >= ray.distance)
return false;

float invDet = 1.0f / (edge2[ku] * edge1[kv] - edge2[kv] * edge1[ku]);

float[] h = new float[3];
h[ku] = ray.origin[ku] + tPlane * ray.direction[ku] - v0[ku]; //Only change to Wald's algorithm: -v0.[ku]
h[kv] = ray.origin[kv] + tPlane * ray.direction[kv] - v0[kv]; //Only change to Wald's algorithm: -v0.[kv]

beta = (edge2[ku] * h[kv] - edge2[kv] * h[ku]) * invDet;
if (beta < 0)
return false;

gamma = (edge1[kv] * h[ku] - edge1[ku] * h[kv]) * invDet;
if (gamma < 0)
return false;

if (beta + gamma > 1)
return false;

ray.distance = tPlane;
return true;
}

EDIT: Sorry about the bad formatting - I'm not sure how to post the code with its original indentation.

[Edited by - Speedster on October 29, 2009 2:57:35 AM]

Share this post


Link to post
Share on other sites
my first guess would be.. maybe ku,kv shoulde be swapped for some projections
instead calculating them using expensive % set them inside of your 'if' statement, and play with the order.

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!