As we said in the last two lectures, the Phong algorithm was developed by Bui-Tong Phong to approximate surface shading. The algorithm takes, as input, the following quantities:
Given the above, the Phong shading algorithm is:
A_{rgb} + ∑ I_{i} ( D_{rgb} max(0, n • L_{i}) + S_{rgb} max(0, R_{i} • E)^{p} )where R_{i} is the mirror reflection angle of the light direction vector L_{i}, and E is the unit direction vector to the camera.
As we showed in class, R_{i} can be computed from L_{i} and n by:
R_{i} = 2 (n • L_{i}) n - L_{i}
Since we are transforming the scene into a coordinate system in which the camera looks into negative z, we can assume that E = (0,0,1).
Transforming surface normals
In class we also went over how to transform surface normals. A surface normal is a special kind of linear equation; it describes how far any point p is in the direction normal (ie: perpendicular) to a surface.
In particular, given a surface containing point s, the distance of any point p from that surface is n • (p - s), which is the same as (n • p) - (n • s), since all the operations are linear.
So when we transform our geometry, clearly we want to preserve the value of equations of the form n • p.
Our transformation matrix M will transform point p to (M • p). Therefore the transformation of n must be (n • M^{-1}). We can prove this by using the associative rule:
You can use the same matrix/vector multiply routine you are already using to transform vertices, if you just transform the inverse matrix, since:
For your convenience, I have placed code to compute the inverse of a 4×4 transformation matrix in class MatrixInverter.
Assignment due Wednesday March 26
For Wednesday March 26 I would like you to put all the pieces together, implementing the complete rendering pipeline for your animated shapes. For each animation frame:
Remember to apply the z-buffer algorithm, so that each pixel retains the (r,g,b) of whatever triangle is nearest to the camera (ie: has the lowest p_{z} value at that pixel).