Ray tracing box intersections

Chris picture Chris · Jan 18, 2012 · Viewed 16.5k times · Source

So I'm back with another ray tracing question. My code renders spheres all fine and dandy, but cubes aren't really working. I'm using this code to test for intersections: http://pastebin.com/qgm6vpdx (It's a recursive function, t is the distance to the intersection point) The bounding box is defined as:

Cube* c1 = new Cube;      
c1->Corner1 = Vec3(100, 100, 100);      
c1->Corner2 = Vec3(200, 200, 200);

I've confirmed that the camera is not inside the cube. Now, the only problem is that the entire screen shows up as green (the color designated to the cube)

I don't think I'm doing the cube intersections right, can anyone proof read my code?

Answer

Tavian Barnes picture Tavian Barnes · Jan 19, 2012

One of the best algorithms for calculating ray-box intersections is the slab method. I outlined my optimized implementation of it here, as well.