Main content

## Mathematics of rendering

Current time:0:00Total duration:4:11

# 5. 3D ray tracing part 1

## Video transcript

- Now that we can retrace in 2D, we can finally go back to the problem we really wanna solve: retracing in 3D. In particular, we'll need
to retrace flat planes, houses, and, ultimately, characters. A character like Carl is a complex shape but, as we discussed in the
character modeling lesson, he can be broken down into lots of small, four-sided quadrilaterals,
that is, four-sided polygons. And each quadrilateral can be
converted into two triangles by adding an edge that
connects diagonal points. That leads to the question: How do you intersect
a ray with a triangle? It turns out, that that's
one of the most fundamental calculations that a ray tracer performs. Here's a scene consisting
of just one triangle. Our real scenes contain
millions of triangles. But, once we know how to
intersect a single triangle, our ray tracer just keeps doing
that, over and over again. Now, I don't know about
you, but I don't wanna do the same thing over and over again. So, it's a good thing we
have computers to help us out and that they don't get tired. As in 2D, we start by setting
up a coordinate system. But, this time, there are
three directions: X, Y, and Z. As we explained earlier,
we pick a camera position, call it C, and a viewing direction. And we construct an image plane perpendicular to the viewing direction. This is where our image will be formed. Let's pick a pixel, P, on the image plane and construct the
parametric representation of the ray, CP, as R(t) = (1-t)C + tP. This is the same equation we saw in 2D, but now it represents
three separate equations. One for X coordinates,
one for Y coordinates, and one for Z coordinates. Remember that, in the previous video, we saw that, in two dimensions, every line can be written in implicit
form as ax + by + c = 0. Very similar to this is
the equation for a plane. And every triangle lies in a plane. The equation for a plane can
be written in implicit form as ax + by + cz + d = 0. The intersection point,
I, we're looking for, is in the plane of the triangle, meaning that aIx + bIy + cIz + d = 0, where Ix, Iy, and Iz are
the coordinates of I. I is also on the ray, meaning
that there's a value of t, again, let's call it
t*, such that I = R(t*) which equals (1-t*)c + t*P which is really the three
equations shown here. One for X, one for Y, and one for Z. Now we have four equations
and four unknowns. To solve this system of equations, we can follow the recipe from 2D and substitute the last three
equations into the first one. This gives us one equation
with only one unknown, t*. But, it turns out, when you
put all these substitutions in, it looks pretty scary. But, remember, it's not that bad. We're just plugging one value from one equation into another. Solve this for t*, then substitute back into the ray equations
to get Ix, Iy, and Iz. Now, I know we've gone kind of fast, but the next exercise
will let you practice computing intersection
points for yourself.