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 x^{2} + y^{2}  1 and xy + 3.
In contrast, a polynomial like x^{2}z + y^{2}  1 is not second order, because the sum of the powers in the first term x^{2}z is too high: 2 + 1 = 3.
The most general possible second order polynomial has ten terms:
ax^{2} + by^{2} + cz^{2} + dxy + eyz + fzx + gx + hy + iz + jWe 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:



The above expression multiplies out to exactly ax^{2} + by^{2} + cz^{2} + 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) M^{T}).
Following the same logic that we used for transforming halfspaces, 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:



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 nonzero values in all 16 places. In order to turn this into the ten coefficients of the general second order polynomial ax^{2}+ by^{2}+...+j, you need to add the six values on the lower left of the matrix into their corresponding places in the upper right:

→ 

