Some Math
- We will generally think of things as vectors.
- Given a triangle, we can convert it to vectors
- The magnitude of a vector
- v1 = (x1, y1 , z1)
- |v1 = sqrt(x12, y12 , z12)
- Note in MV.js this is called length.
- By the way, there is a function that "normalizes" a vector
- normalize in MV.js
- v' = v/|v|
- This will be very useful (in the next set of notes).
- The dot product of two vectors
- v1 = (x1, y1 , z1)
- v2 = (x2, y2 , z2)
- v1· v2 = x1 x2 + y1 y2 + z1 z2
- This is a scalar.
- It can be interpreted as
- v1· v2 = |v1| |v2| cos (Θ)
- So if |v1| = 1 and |v2| = 1
- v1· v2 = cos(Θ)
- Note in MV.js this is called dot
- So given a normal to a triangle
- Given an eye position
-
- We can compute the angle between the normal and the vector towards the eye.
- If cos(Θ) > 0, we can see the front of the triangle
- If cos(Θ) < 0, we can see the back of the triangle
- Remember cos(Θ)
-
- The book, sanely, derives this the
- The Cross product
- This works in three space, but does not generalize to n space.
- v1 × v2 = |v1||v2 |sin(Θ)
- v1 × v2 = (y1z2-z1y2, z1x2 - x1z2, x1y2 - y1x2)
- Or more useful, v1 × v2 = u, the normal to the v1 × v2 plane.
-
- In MV.js there is a cross function.
- So why talk about this now?
- It is important so we understand backface removal
- For flat shading
- We provide a uniform color across a surface.
- But it depends on the lights in the scene.
-
- For smooth shading
- We start with vertex normals.
- These are interpolated across the surface
-
- Computing vertex normals
- Really just the average of associated surface normals.
-
- So we can calculate these in our conversion from OFF to JS
- Assume all polygons are given in CW or CCW
- Probably provide a command line argument to reverse the normal computation.