16
$\begingroup$

Why are Homogeneous Coordinates used in Computer Graphics?

What would be the problem if Homogeneous Coordinates were not used in matrix transformations?

$\endgroup$
14
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ What kind of examples are you looking for? Translation matrices and anything related to perspective projections should be easy enough to look up? $\endgroup$ – Bart Sep 25 '15 at 15:07
  • $\begingroup$ @Bart, Analogy needed. $\endgroup$ – user464 Sep 25 '15 at 15:20
  • 2
    $\begingroup$ I'm sorry @anonymous, but that doesn't really tell me anything. You're going to have to use more words to explain what exactly you are looking for. $\endgroup$ – Bart Sep 25 '15 at 15:21
  • $\begingroup$ I think this answer isn't upvoted as highly because it's too technical for us newbies. Maybe a simple example with simple wording would illustrate the principles better $\endgroup$ – Nathan Mar 26 '19 at 19:20
6
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ "Sony's PS4 can perform massive matrix multiplications." You mean the Cell processor of the PS3, right? The PS4 has a rather ordinary x86 processor. $\endgroup$ – Wumpf Sep 28 '15 at 20:40
  • $\begingroup$ While this is a good answer I don't think it answers the OPs question and kind of suggests that homogenous coords are used because the hardware is optimised for it, rather homogenous coords are more useful and hardware was eventually developed around that. Another argument for vec4s is they are 128bit aligned which makes it more efficient to read on wide memory buses (GPUs) $\endgroup$ – PaulHK Aug 29 '16 at 1:53
4
$\begingroup$

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 ).

| improve this answer | |
$\endgroup$
3
$\begingroup$

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}$$

| improve this answer | |
$\endgroup$
2
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ In principle, data types can be implemented that don't actually store those entries even though they act like they do. $\endgroup$ – user1713 Sep 25 '15 at 8:06
  • 1
    $\begingroup$ @Hurkyl Obviously. This is rarely done, as general-purpose matrix toolboxes are on hand. $\endgroup$ – Yves Daoust Sep 25 '15 at 8:09
  • $\begingroup$ @YvesDaoust Could you provide an example of plain Cartesian formulation or link to a resource that describes its use in 3D graphics? $\endgroup$ – Dan Dec 27 '17 at 23:41
  • $\begingroup$ @Dan: use y = A.x + b where A is a 3x3 matrix and b a 3x1 vector, instead of y' = A.x' where y', x' are augmented vectors and A a 4x4 matrix. $\endgroup$ – Yves Daoust Dec 28 '17 at 8:45
  • $\begingroup$ @YvesDaoust So you're passing a 3x3 matrix and a 3x1 vector to your shaders instead of a 4x4 matrix? Where do you calculate and store w? $\endgroup$ – Dan Dec 28 '17 at 18:51
1
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ "Calculations in affine coordinates often require divisions": I don't see why. In fact you compute exactly the same expressions. $\endgroup$ – Yves Daoust Sep 25 '15 at 7:07
  • $\begingroup$ @Yves: I'm responding to the more general "use in computer graphics" topic, not the specific "computing matrix transformations" question. $\endgroup$ – user1713 Sep 25 '15 at 7:54
  • $\begingroup$ @Hurkyl: so do I. When rendering a scene, you compute exactly the same expressions, with the same amount of divisions (the difference lies in dummy terms with a 0 factor). $\endgroup$ – Yves Daoust Sep 25 '15 at 8:02
  • $\begingroup$ @Yves: Hrm. I'm used to doing calculations where the conversion back to affine can be deferred to some extent; I'll cede to your expertise if you say that doesn't come up often. $\endgroup$ – user1713 Sep 25 '15 at 8:05
-1
$\begingroup$
  • simpler formulas
  • Fewer special cases
  • Unification and
  • Duality
| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ The answer is very unclear. You should elaborate on each point. $\endgroup$ – Rotem Dec 25 '17 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy