Raytrace 3: Ray tracing to second order polynomial shapes:

A second order polynomial shape is defined by a second order polynomial. That's a polynomial where the sum of the powers of any term is not greater than two. Examples are x2 + y2 - 1 and xy + 3.

In contrast, a polynomial like x2z + y2 - 1 is not second order, because the sum of the powers in the first term x2z is too high: 2 + 1 = 3.

The most general possible second order polynomial has ten terms:

ax2 + by2 + cz2 + dxy + eyz + fzx + gx + hy + iz + j
We can define a solid shape as all the places where such a polynomial evaluates to less than or equal to zero. Points on the surface of the shape give a zero value, points inside of the shape give a negative value, and points on the outside of the shape give a positive value.

For points at the surface, you can compute the x,y,z partial derivatives of this polynomial to get the gradient vector (a vector which points perpendicularly out of the surface). If you normalize this gradient vector to unit length, you get the surface normal, which you can use to do shading, reflection and refraction.

As you probably know from your freshman calculus class, you can get the x,y,z components of this gradient vector by taking the derivative of the polynomial with respect to x,y and z, respectively:

( 2ax+dy+fz+g , 2by+dx+ez+h , 2cz+ey+fx+i )

If you want to transform a second order polynomial by a linear transformation matrix, it is useful to describe the evaluation of the polynomial at any point (x y z 1)T by using the notation of linear transformations. As we discussed in class, the following notation (originally devised by Jim Blinn) does the trick, by forming the polynomial coefficients into a 4×4 matrix:

x   y   z   1
a   d   f   g
0   b   e   h
0   0   c   i
0   0   0   j
x
y
z
1

The above expression multiplies out to exactly ax2 + by2 + cz2 + dxy + eyz + fzx + gx + hy + iz + j.

Now that we have the equation in this form, we can transform the original point and see what happens. First we transform (x y z 1)T to (M (x y z 1)T). We also need to transform the point in the other place it appears - on the left side of the coefficients matrix. When you transpose vectors and matrices, you end up reversing the order that they multiply together, so (x y z 1) is transformed to ((x y z 1) MT).

Following the same logic that we used for transforming half-spaces, we need to insert a matrix both to the left and to the right of the coefficients matrix, so that the result of the transformed equation doesn't change:

As we discussed in class, we do this as follows:

x   y   z   1
MT
M-1T
a   d   f   g
0   b   e   h
0   0   c   i
0   0   0   j
M-1
M
x
y
z
1

Of the three terms above, the middle term gives the transformed coefficients matrix. After you do the above matrix multiplication, you will end up with a 4×4 matrix that has non-zero values in all 16 places. In order to turn this into the ten coefficients of the general second order polynomial ax2+ by2+...+j, you need to add the six values on the lower left of the matrix into their corresponding places in the upper right:

a d f g
D b e h
F E c i
G H I j
a d+D f+F g+G
0  b  e+E h+H
0  0   c  i+I
0  0   0   j