First, let's look at a special case: ray tracing to an unit radius cylindrical tube.
The implicit function for a unit diameter infinitely long cylindrical tube along the z axis is: x2 + y2 - 1 ≤ 0We can solve the ray equation manually, like we did for the sphere, as follows:
But this doesn't give us the ability to transform the cylinder, nor would it generalize to other quadratic shapes, including cones, paraboloids and hyperboloids. So instead, let's work on the general case of being able to transform any implicit quadratic function by a 4×4 matrix, and then ray trace to the transformed surface described by that transformed function. The general second order implicit surface: Any quadratic implicit surface can be expressed as follows: ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + j ≤ 0The ten coefficients of this equation can be stored as a vector: [a, b, c, d, e, f, g, h, i, j]The unit sphere, the infinite unit cylinder and the infinite unit cone, respectively, can all be expressed in such a form: Raytracing to a general second order implicit surface:
Ray tracing to a general second order surface just requires substituting the ray into this ten-coefficient equation to get a quadratic equation in t, exactly as we did above in the case of the cylinder:
|
Applying a linear transform to a second order surface
Let us first review the simpler case of linear surfaces: Half space P = (a, b, c, d) contains point p = (x,y,z), if and only if ax + by + cz + d ≤ 0. This can also be expressed as the product:
or, in other words, L pT ≤ 0. To find the plane that contains transformed points MpT, you need to replace L with LM-1. Similarly, we can express quadratic equation ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + j ≤ 0as the following double product:
or in other words, p Q pT = 0, where Q denotes the 10 quadratic coefficients arranged into the above 4×4 matrix. This means that if you want to find the three-variable quadratic equation that contains transformed points MpT, you need to replace Q by (M-1)T Q M-1 because that will give you:
(M pT)T (M-1)T Q M-1 (M pT) = when you create the product of (M-1)T P M-1you can end up with non-zero values in all sixteen slots of your 4×4 matrix. This is because in addition to x*y, x*z, x*1, y*z, y*1, z*1 terms, you also end up with y*x, z*y, 1*z, z*y, 1*z, 1*z terms, as follows: Since multiplication of numbers is commutative, you can just add the six numbers in the lower left (shown in magenta above) into their mirror locations on the upper right, as follows:x*x x*y x*z x*1 y*x y*y y*z y*1 z*x z*y z*z z*1 1*x 1*y 1*z 1*1 This will leave you with just the ten unique coefficients of the quadratic in three variables, in the upper right of your transformed matrix. Computing the surface normal: Once you have computed your transformed values for [a,b,c,d,e,f,g,h,i,j], you can use high school differential calculus to compute its vector valued derivative: f(x,y,z) = ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + jJust normalize this derivative to get the normal at the surface point: N = normalize(f'(x,y,z)) |