If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

5. 3D ray tracing part 1

Now we are ready to ray trace in 3D! We'll look at the problem of ray triangle intersection. We can break down complex shapes, like characters, into smaller four-sided polygons, which we can then convert into triangles. Click here for more detail on the plane equation.

Want to join the conversation?

  • mr pants teal style avatar for user Phil Fulks
    Shouldn't there be a " +d " in the last equation?
    (19 votes)
    Default Khan Academy avatar avatar for user
  • winston default style avatar for user ☺☻☺Natth4545☺☻☺
    How would you write the big equation in terms of t?
    (3 votes)
    Default Khan Academy avatar avatar for user
    • leafers sapling style avatar for user Peter Collingridge
      So the equation is (using t in place of t*):
      a(1-t)Cx + atPx + b(1-t)Cy + btPy + c(1-t)Cz + ctPz + d = 0
      aCx - atCx + atPx + bCy - btCy + btPy + cCz - ctCz + ctPz + d = 0
      aCx + bCy + cCz + d + t(aPx + bPy + cPz - aCx - bCy - cCz) = 0
      t(aCx + bCy + cCz - aPx - bPy - cPz) = aCx + bCy + cCz + d
      t = (aCx + bCy + cCz + d) / (aCx + bCy + cCz - aPx - bPy - cPz)

      I think, though it's possible I made a mistake.
      (4 votes)
  • spunky sam blue style avatar for user randy tjandra
    How do you check if a ray actually intersect with plane? (Plane parallel with ray never have intersection)
    (3 votes)
    Default Khan Academy avatar avatar for user
    • leafers ultimate style avatar for user Davide Pesare
      if a plane is infinite it is easy:
      a ray can be described by a vector and a point. Take the vector and do a dot product with the normal of the plane. If the result is 0, the plane and the ray are parallel. If the result is negative, the ray points towards the plane. If it is positive, the ray points away from the plane.

      If instead of an infinite plane you are looking for the intersection with a triangle, you need to do a few more tests but that's the idea.
      (4 votes)
  • leaf red style avatar for user Blaze
    So if I were in, say, 7D, would the equation be:
    at + bu + cv + dw + fx + gy + hz + j = 0?

    (Note: I removed e and i to prevent confusion.)
    (3 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user crystealneonlion
    what is the value of A,B,C and D?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • ohnoes default style avatar for user HSstudent16
    In the beginning of the video, the subtitles say "retracing" instead of "ray-tracing". Is this intentional?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • aqualine ultimate style avatar for user Ruohuai Pan
    Oh, it eventually matches some points in linear algebra.
    (1 vote)
    Default Khan Academy avatar avatar for user
  • winston baby style avatar for user Destroyer
    how do you ray trace a sphere or ellipsoid?
    a sphere can be defined by xx + yy + zz = rr
    so t should equal sqrt((sq(r)-sq(C.x)-sq(C.y)-sq(C.z)+sq(O.x)+sq(O.y)+sq(O.z))/(sq(P.x)+sq(P.y)+sq(P.z)-sq(C.x)-sq(C.y)-sq(C.z)))
    and an ellipsoid can be described by
    xx/aa + yy/bb + zz/cc = 1
    so t should equal sqrt((w*w*h*h*d*d - C.x*C.x*h*h*d*d + O.x*O.x*h*h*d*d - C.y*C.y*w*w*d*d + O.y*O.y*w*w*d*d - C.z*C.z*w*w*h*h + O.z*O.z*w*w*h*h)/(C.x*C.x*h*h*d*d + P.x*P.x*h*h*d*d - C.y*C.y*w*w*d*d + P.y*P.y*w*w*d*d - C.z*C.z*w*w*h*h + P.z*P.z*w*w*h*h))
    but the weight equation does not always work
    (1 vote)
    Default Khan Academy avatar avatar for user
  • old spice man green style avatar for user laiviethai
    In fact, where are the linear and plane equations coming from?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • spunky sam orange style avatar for user Elder Fauth
    It is talking about tris, but in 3d programs, you should always have as few tris as possible, but have mainly quads, right?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • hopper happy style avatar for user Immensity
      I'm a little late, but hopefully I can answer your question.

      Yes, in most situations it is better to have fewer polygons (triangles). When the program needs to render something, it has to render every single triangle on screen. Your computer has to compute something, like ray intersections and such in ray tracing, for every triangle on screen. Most scenes in animated films have millions of triangles on screen at a time, and your computer has to run computations on every single one of them, which can take quite a bit of time. So the fewer triangles you have, the better.

      In most, if not all 3D programs, you also have the option to use quads (four sided polygons). But in the video, they are talking about triangles. So what gives? Well, your 3D software is actually lying to you! gasp! Quads definitely make things way easier for artists, but triangles are much simpler to compute and are thus the fundamental building block of computer graphics. When your computer actually renders those quads, they get turned into triangles (each quad can be simplified to two triangles). In some 3D programs, you can technically also work with polygons with more that four sides (although it isn't recommended), and even these polygons are secretly turned into triangles when you aren't looking.

      TLDR; The less polygons you have, the faster you can render. Quads are secretly turned into triangles, but quads are useful for human artists.
      (1 vote)

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.