Computer Graphics
Basics of Computer Graphics
Homogeneous Coordinates
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
Points and vectors are represented using homogeneous coordinates in computer graphics.
This allows affine transformations in 3D (i.e. rotation and translation) to be represented as a matrix multiplication.
While rotations can typically be represented in a 3x3 matrix multiplication, a translation requires a shear in 4D.
Points are
The last coordinate in points allow for translations to be represented as matrix multiplications.
- Notes
- The point
is equivalent to .
Affine transformations consist of translations, rotations, and scaling
Translation Matrix
Rotation Matrix
Rotations can be about the X, Y, and Z axis.
Below is a rotation about the Z axis by angle
To formulate a rotation about a specific axis, we use Wikipedia:Rodrigues' rotation formula.
Suppose we want to rotate by angle
Let
Then the rotation matrix is
Here the 4x4 form is:
Scaling Matrix
MVP Matrices
To convert from model coordinates
- The model matrix
applies the transform of your object. This includes the position and rotation. is in world coordinates. - The view matrix
applies the transform of your camera. is in camera or view coordinates. - The projection matrix
applies the projection of your camera, typically an orthographic or a perspective camera. The perspective camera shrinks objects in the distance.
Model Matrix
Order of matrices
The model matrix is the product of the element's scale, rotation, and translation matrices.
View Matrix
Reference
Lookat function
The view matrix is a 4x4 matrix which encodes the position and rotation of the camera.
Given a camera at position
We can calculate the forward vector (from target to position) as
We can calculate the right vector as
Then the view matrix is written as:
r_x r_y r_z 0 u_x u_y u_z 0 f_x f_y f_z 0 p_x p_y p_z 1
Matrix lookAt(camera_pos, target, up) { forward = normalize(camera - target) up_normalized = normalize(up) right = normalize(cross(up, forward) // Make sure up is perpendicular to forward up = normalize(cross(forward, right) m = stack([right, up, forward, camera], 0) return m }
Perspective Projection Matrix
Notes: In computer vision, this is called the calibration matrix
Inverting the projection
If you have the depth (either z-depth or euclidean depth), you can invert the projection operation.
The idea is to construct a ray from the camera to the pixel on a plane of the viewing frustrum and scale the distance accordingly.
See stackexchange.
Shading
Flat Shading
Gourard Shading
Phong Shading
More Terms
- Diffuse reflection - reflection scattered in many directions (i.e. matte)
- Specular reflection - mirror reflection
- Refraction - change in direction of light as it passes through a material