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?
You could make the code a lot shorter and more readable. For example, change
int tNear = -2147000000
toint tNear = INT_MIN
and changeto
or better
and change
to
Then one section of your code becomes:
And this reveals one problem.
tNear
andtFar
define an interval oft
values within which the line intersects the cube. Each coordinate you test (x, y and z) further constrains the interval. However the codetFar = max(tFar, t1)
is expanding the interval. Change it totFar = min(tFar, t1)
.More fundamentally, this limits you to axis-aligned cuboids, though this code might be useful later as a quick hit-test for more complex shapes. Anyway, once this is working you might like to make it more general.
You can define any convex polygon as a set of infinite planes with the normals facing outwards. A point is inside the polygon if it is "inside" all of the planes.
A plane splits space into two halves. Define the half to which the normal points as "outside" and the other half as "inside". Then a point is outside a plane if the plane equation at that point is positive, inside the plane if the value is negative and on the plane if the value is zero.
To ray-trace this you determine the ray/plane intersections and choose the nearest one. To determine if that point is within the face (remember, the plane is infinite) you check if the point is inside all the other planes. If not, test the next nearest intersection, and so on.
Once this is working it is quite easy to extend it to general intersections and differences of shapes (e.g. a cube with a hemispherical indent in one of the faces).