I didn't write a blog on assignment 2 because it's wasn't overly interesting--just making a set of rotating cubes. However, assignment 3 was fairly fun and interesting and I think deserves its own blog post. First, you can visit this page to get a demo of what it is: https://fu5ha.github.io/asu-cse470/assignment3
The same concepts talked about in this and the next blog post are also the basis for this more complete scene renderer that I made which is based on a simplified version of THREE.js' structure: https://fu5ha.github.io/asu-cse470/assignment4
The main interesting things that are happening here are the creation of the surface(s) of revolution and the lighting calculations.
The surfaces of revolution are built by first creatng a parameterized curve like so:
where is a "generator function" which has range . We then sweep this function around the Y axis by applying a counter-clockwise rotation matrix to it with an angle . This gives us a final surface definition:
Next we discretize this continuous surface into a set of vertices. To do this we define a number of tesselations in the direction of , as well as in the direction of , . These represent the number of quads that we want in each direction, up/down and around the surface. We then step in increments of and and add each resulting point to a list of points.
The next thing we need to do is calculate normal vectors at each of these vertices for use in the shading algorithm later. The normal vector is a vector pointing perpendicular to a tangent plane to the surface at that point. In this case, we want a vector that is perpendicular to both the tangent line in the direction of and the tangent line in the direction of . In three dimensions, w can calculate a vector perpendicular to two other ones by taking their cross product. Therefore, if we can calculate a vector in the direction of the tangent line of , , and in the direction , , at point , we can take the cross product and get a vector in the correct direction. However, we need it to also have length 1, so we'll say our final normal vector
The order of the cross product in relation to the direction of the vectors is important; following the right hand rule, we want to be counter-clockwise from so that the resulting vector of points in the correct direction (out of the surface of revolution rather than into it). Since our previously defined rotation matrix rotates in a counter-clockwise direction, we know that increasing will take us further counter-clockwise. Therefore we know that our vector will be counter-clockwise of which has its origin at the same . If we used a counter clockwise rotation matrix, we would do the cross product in the other order to obtain the same result.
To calculate these vectors we can simply take the partial derivatives of with respect to and respectively:
Now we can compute the cross product of these vectors:
However, since we don't care about the length of this vector since we are going to be normalizing it anyway, we can drop the since multiplying a vector by a scalar does not change its direction.
Yay! Now we just do that calculation for each and when we discretize the surface and put the new normal vectors in their own array buffer to be used later as well.
Continued in part 2 here, which talks about the actual lighting calculations