Why are Homogeneous Coordinates used in Computer Graphics?
What would be the problem if Homogeneous Coordinates were not used in matrix transformations?
Computer Graphics Stack Exchange is a question and answer site for computer graphics researchers and programmers. It only takes a minute to sign up.
Sign up to join this communityWhy are Homogeneous Coordinates used in Computer Graphics?
What would be the problem if Homogeneous Coordinates were not used in matrix transformations?
They simplify and unify the mathematics used in graphics:
They allow you to represent translations with matrices.
They allow you to represent the division by depth in perspective projections.
The first one is related to affine geometry. The second one is related to projective geometry.
It's in the name: Homogeneous coordinates are well ... homogeneous. Being homogeneous means a uniform representation of rotation, translation, scaling and other transformations.
A uniform representation allows for optimizations. 3D graphics hardware can be specialized to perform matrix multiplications on 4x4 matrices. It can even be specialized to recognize and save on multiplications by 0 or 1, because those are often used.
Not using homogeneous coordinates may make it hard to use strongly optimized hardware to its fullest. Whatever program recognizes that optimized instructions of the hardware can be used (typically a compiler but things are more complicated sometimes) for homogeneous coordinates will have a hard time with optimizing for other representations. It will choose less optimized instructions and thus not use the potential of the hardware.
As there were calls for examples: Sony's PS4 can perform massive matrix multiplications. It's so good at it that it was sold out for some time, because clusters of them were used instead of more expensive super-computers. Sony subsequently demanded that their hardware may not be used for military purposes. Yes, super-computers are military equipment.
It has become quite usual for researchers to use graphic cards to calculate their matrix multiplications even if no graphic is involved. Simply because they are magnitudes better in it than general purpose CPUs. For comparison modern multi-core CPUs have on the order of 16 pipelines (x0.5 or x2 doesn't matter so much) while GPUs have on the order of 1024 pipelines.
It's not so much the cores than the pipelines that allow for actual parallel processing. Cores work on threads. Threads have to be programmed explicitly. Pipelines work on instruction level. The chip can parallelize instructions more or less on its own.
Imagine you want to represent transformations using matrices. Points could be stored as $$\begin{bmatrix}x\\y\end{bmatrix}$$ and you could represent a rotation as $$\begin{bmatrix}u\\v\end{bmatrix}=\begin{bmatrix}cos(\theta)&-sin(\theta)\\sin(\theta)&cos(\theta)\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}$$ and scaling as $$\begin{bmatrix}u\\v\end{bmatrix}=\begin{bmatrix}k1&0\\0&k2\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}$$ These are known as linear transformations and they allow us to do transformations as matrix multiplications. But notice that you cannot do translations as a matrix multiplication. Instead you have to do $$\begin{bmatrix}u\\v\end{bmatrix}=\begin{bmatrix}x\\y\end{bmatrix}+\begin{bmatrix}s\\t\end{bmatrix}$$ This is known as an affine transformation. However this is undesirable (computationally).
Let R and S be rotation and scaling matrices and T be a translation vector. In computer graphics, you may need to do a series of translations to a point. You could imagine how tricky this could get.
Scale, translate, then rotate and scale, then translate again: $$p'=SR(Sp+T)+T$$ Not too bad but imagine you had do this computation on a million points. What we would like is to represent rotation, scaling, and translation all as matrix multiplications. Then those matrices can be pre multiplied together for a single transformation matrix which is easy to do computations with.
Scale, translate, then rotate and scale, then translate again: $$M=TSRTS$$ $$p'=Mp$$
We can achieve this by adding another coordinate to our points. I'm going to show all this for 2D graphics (3D points) but you could extend all this to 3D graphics (4D points). $$p=\begin{bmatrix}x\\y\\1\end{bmatrix}$$ Rotation matrix: $$R=\begin{bmatrix}cos(\theta)&-sin(\theta)&0\\sin(\theta)&cos(\theta)&0\\0&0&1\end{bmatrix}$$ Scale matrix: $$S=\begin{bmatrix}k1&0&0\\0&k2&0\\0&0&1\end{bmatrix}$$ Translation matrix: $$T=\begin{bmatrix}1&0&t1\\0&1&t2\\0&0&1\end{bmatrix}$$ You should work out some examples to convince yourself that these do in fact give you the desired transformation and that you can compose a series of transformations by multiplying multiple matrices together.
You could go further and allow the extra coordinate to take on any value. $$p=\begin{bmatrix}x\\y\\w\end{bmatrix}$$ and say this this homogeneous (x, y, w) coordinate represents the euclidean (x, y) coordinate at (x/w, y/w). Normally you cannot do division using matrix transformations, however by allowing w to be a divisor, you can set w to some value (through a matrix multiplication) and allow it to represent division. This is useful for doing projection because (in 3D) you will need to divide the x and y coordinates by -z (in a right handed coordinate system). You can achieve this by simply setting w to -z using the following projection matrix: $$Q=\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&-1&0\end{bmatrix}$$
complement:
homogeneous coordinates also allows to represent infinity: $(x,y, z, 0) = \frac{x,y,z}0$ in 3D, i.e., the point at infinity in direction $x,y,z$. Typically, light sources at finite or infinite position can be represented the same way.
About perspective transform, it even allows to interpolate correctly with no perspective distortion (contrary to early graphics hardware on PC ).
As a personal taste I have always abstained (when possible) from using homogeneous coordinates and preferred the plain Cartesian formulation.
Main reason is the fact that homogeneous coordinates uses 4 trivial entries in the transformation matrices (0, 0, 0, 1), involving useless storage and computation (also the overhead of general-purpose matrix computation routines which are "by default" used in this case).
The downside is that you need more care when writing the equations and lose support of matrix theory, but so far I have survived.
plain Cartesian formulation
or link to a resource that describes its use in 3D graphics?
$\endgroup$
w
?
$\endgroup$
Calculations in affine coordinates often require divisions, which are expensive as compared to additions or multiplications. One usually does not need to divide when using projective coordinates.
Using projective coordinates (and more generally, projective geometry) tends to eliminate special cases too, making everything simpler and more uniform.