Introduction
Graphics
Raster (bitmap) graphics have a fixed array of pixels with values, and a fixed resolution. Vector graphics are driven by display commands. It has a scalable resolution, which makes it ideal for typography and animation.
There are three main disciplines of graphics: modeling shape and appearance, animation, and rendering.
Images and Pixels
An image is a 2D grid of cells called pixels, where each pixel holds some set of values. A pixel (picture + element) is a unit with some kind of value. A pixel value can represent intensity (grayscale), color (RGB), opacity, or depth.
What are the advantages and disadvantages of floating-point vs. integer format?
- Floating point takes a lot more storage than integer
- 24 bits is usually enough for the human eye
- Floating point is usually used as the intermediate format for calculations
How do we store images in memory?
- Array of structs (AoS), where each struct represents an RGB value
- Struct of arrays (SoA), where each array represents a R, G, or B channel
Different memory organizations affect cache locality.
Vector Graphics
Vector Graphics Pipeline
To create a 2D vector graphic:
- Define object geometry
- Transform object (translate, rotate, scale)
- Define "window" on the world
- Render into raster graphics
In each of the steps of this pipeline, there are different coordinate systems:
- Object geometry is defined in terms of object space
- Objects are placed into world space
- The window is defined in terms of view space
- The rendered graphic is defined in terms of normalized device coordinates, which are resolution-specific
- The application may only have a subregion of the OS window (viewport), so we might also need to transform into screen space
Why use multiple coordinate systems?
At higher levels of abstraction, we don't want to think about specific device coordinates to draw to. We also want to keep different layers modular, so we can modify each independently.
Scenes
Graphics applications typically represent scenes with a DAG which stores:
- Objects
- Attributes (color, texture)
- Transformations

To get the cumulative transformation matrix of an object, simply walk the node up to the root.
Appendix
Vectors
A vector transforms a scalar into another scalar. Below, we transform the scalar by shifting it units right and units up.
The dot product is an operation that takes in two vectors and produces a scalar:
Geometric interpretations of the dot product:
- is the squared length of the vector
- when the vectors are perpendicular
- , where is the angle between the arrows
The cross product is an operation that takes in two vectors and produces a vector that is mutually perpendicular to both vectors. By convention, the direction that points in is determined by the right-hand rule.
Geometric interpretations of the cross-product:
- also gives the area of the parallelogram with and as edges. Half of this area gives us the triangle with and as edges.
Matrices
The identity matrix is a matrix such that for all matrices . The 2x2 identity matrix is:
If we have two matrices and such that , then we say that is the inverse of . Not all matrices have an inverse. For a 2x2 matrix, we have:
Transformations
A transformation is a function that acts on space.
- A linear transformation respects the property
- A matrix transformation is any transformation that can be written in terms of multiplying a matrix and a vector
- Every linear transformation is a matrix transformation
Note that matrix transformations can compose. For example, applying then to vector is equivalent to .
We can build matrix transformations using basis vectors. To find a matrix that sends:
We need to find a matrix that satisfies:
Arithmetically, this matrix is
To reverse the transformation, we can simply calculate the inverse of .
Thus, to send
We can simply calculate the matrix that sends
And the matrix that sends
And then calculate .
Scale, Rotate, and Translate
Both scale and rotate are linear transformations and can be represented by matrix transformations.
-
To scale by in the x-axis and scale by in the y-axis, apply the matrix:
-
To rotate by counterclockwise, apply the matrix:
Translation is not a linear transformation; it is an affine transformation. In order to represent it as a matrix transformation, we add a third coordinate .
- is if the value is a scalars and if the value is a vector
- does not affect subtraction of two scalars (which produces a vector), addition of a vector and a scalar (which produces a scalar), or dot product (which is mostly between two vectors)
- also does not affect the linearity of scaling and rotation
To translate in the x-axis and in the y-axis, apply the matrix:
Algorithmically, this works to out be:
Non-Axis Aligned 3D Rotation
Scaling and translation in 3D is similar to that in 2D. Rotations are more complicated because we can now rotate around an arbitrary axis. There are several ways to do this: Euler angles, axis-angles, and quaternions.
Euler angles are a composition of rotations around the x, y, and z axes. One problem with Euler angles is that they don't trace the shortest rotational path because rotation composition doesn't commute. To fix that, we can linearly interpolate each angle independently with some correction factor. However, this does not get an even angular velocity. Finally, when two rotation axes align, we lose a degree of freedom, causing sudden jumps (gimbal lock).
Axis-angle rotations involve decomposing a point into two parts relative to the axis :
- is the part parallel to ; rotation does not change it
- is the part perpendicular to ; it rotates like a 2D point in a plain perpendicular to
To calculate it, we need:
Which can be expanded to:
The formula for the cross product can be represented as a matrix, so this entire equation can be rewritten in matrix form. Unfortunately, axis-angle rotations also do not sweep the shortest path between two points.
Quaternions are the only rotation method that can correctly do linear interpolation. A quaternion represents a rotation as a point on the surface of a 4D hypersphere. Because we are moving along the sphere, we get shortest path and constant angular velocity.
References: Visualizing quaternions