# Why does clip space in OpenGL have 4 dimensions?

I will use this as a generic reference, but the more i browser online docs and books, the less i understand about this.

const float vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};


in this online book there is an example about how to draw the first and classic hello world for OpenGL about making a triangle.

The vertex structure for the triangle is declared as stated in the code above.

The book, as all the other sources about this, stress the point that the Clip Space is a 4D structure that is used to basically decide what will be rasterized and rendered to the screen.

Here I have my questions:

• i can't imagine something in 4D, i don't think that a human can do that, what is a 4D for this Clip space ?
• the most human-readable doc that i have read speaks about a camera, which is just an abstraction over the clipping concept, and i get that, the problem is, why not using the concept of a camera in the first place which is a more familiar 3D structure? The only problem with the concept of a camera is that you need to define the prospective in other way and so you basically have to add another statement about what kind of camera you wish to have.
• How i'm supposed to read this 0.75f, 0.75f, 0.0f, 1.0f ? All i get is that they are all float values and i get the meaning of the first 3 values, what does it mean the last one?
• Chapter 4 explains exactly what the fourth component does. Actually, scratch that; Chapter 1 explains the clip-to-NDC transform in the rasterization section halfway down. Aug 26, 2012 at 14:29
• @NicolBolas the author gives his explanation in the first chapter and doesn't put any reference for the next chapters, also he pretends to explain what happens next when commenting some c++ code and the problem is that if he doesn't fully explain everything in the 1 chapter do not makes much sense to put what i'm supposed to know in the first place in the chapter number 4, especially if i need this concepts to decode what's inside the 1 chapter. I'm reading this now, and not just 1 time, now i know that i'm supposed to look further for the answer, i will go trough the various chapters. Aug 26, 2012 at 14:39
• You don't need to decode anything; it says in chapter 1: The W component is divided into the other 3 components.This was stated in the introduction as well. What is put off until chapter 4 is why OpenGL does this. That's held off until later because it is irrelevant to the task at hand. Aug 26, 2012 at 14:42
• It would still be irrelevant information for the issue at hand (ie: rendering a triangle). You're curious about it, but you don't need to understand why it is the way it is to understand that this is how it works. In learning anything, the first step is to understand what it is. Once you understand what's going on, then a discussion can take place about why it is that way. Aug 26, 2012 at 14:52
• This answer may help. Oct 21, 2012 at 7:39

The magic term is "Homogeneous coordinates" which are used in systems where perspecive is a factor. Check the wiki for an overview, but it's a long course of study to really understand it (which I don't).

Read the introduction of books you read, you'll be surprised ;)

http://arcsynthesis.org/gltut/Basics/Intro%20Graphics%20and%20Rendering.html under Rasterization Overview

EDIT: the website is down for some time now but the page I refer to is still on the web archive: https://web.archive.org/web/20130901150530/http://www.arcsynthesis.org/gltut/Basics/Intro%20Graphics%20and%20Rendering.html

The "w" value (where the first 3 values are x, y and z) basically says what the dimensions of the clip space are. Because this is 1 scalar value, all 3 dimensions of the clip space are equal (and that's why the clip space is a cube). Every vertex has it's own clip space in which it exists (and basically needs to "fit" in, otherwise it CLIPS :D), there is not 1 "world" that is the clip space(though all clip spaces are in the same "world" I think, even I'm having trouble with this ;P).

So if your vertex has for example the coordinate [1,1,1], if the clip space is 1 then the vertex is in the top right near corner of the screen (when all is default, I don't know if the directions can be altered). But if the vertex has a clip space of 2, then the coordinate [1,1,1] will be somewhere let's say, 3 quarters across the screen to the right, 3 quarters across the screen to the top, and the third dimension you can guess yourself.

I think having let's say a clip space of 5 would mean the locations within that clip space range from -5 to 5 on every dimension, instead of the cube being 5x5x5. But that's probably because simply put: all x y and z coordinates are divided by the clip space dimension, so basically your vertices undergo this:

x = x / w

y = y / w

z = z / w

And that's what makes it all possible. I think the reason this exists is for easy comparisons. If the coordinates have been divided by the clip space dimension, then the coordinate that has 1 or more components with a value higher than 1, exists outside the clip space. So if your clip space is let's say 1024, but the coordinate is [2000,3,-100] then the x(2000) component is outside the clip space (which only ranges from -1024 to 1024).

computingwise it's easy to tell if something is inside the clipspace if all you have to do is (very crudely put ofc): (x/w)<1 && (x/w)>-1 then render. Also, I suppose having all clip spaces of all vertices the same size (so every clip space cube ranging from -1 to 1 in every dimension) makes it easier for whatever comes after the normalisation process, seeing as from that moment on all coordinates are floats ranging from 0 to 1 (disregarding that which has been clipped off).

• The question was why clip-space is as it is, not what clip-space means. That is, what's the point of the divide-by-W. Jul 21, 2013 at 22:06
• It answers the 3rd question of the 3 bullet points though =) Jul 22, 2013 at 10:32

TL;DR it's not 4D space, it's 3D plus a scaling number which is virtually always 1. If it is 1, you can ignore it and the first three numbers are x,y,z. If not it gets more complicated.

Here's a simple explanation. Vertices in 3D should only have three components

 ⌈x⌉ v = |y| ⌊z⌋ 

If we want to manipulate them (e.g. rotation, scaling etc.) we use a matrix. The most common example of course is the Model-View-Projection (MVP) matrix which transforms world coordinates into clip space. Like this:

 ⌈m11 m12 m13⌉ ⌈x⌉ c = |m21 m22 m23| * |y| ⌊m31 m32 m33⌋ ⌊z⌋ 

However this has a big flaw: you can't do translation. If [x,y,z] is zero, no matter what m is the result will always be zero, so we can't have an MVP which includes translation. Obviously we'd like that. The solution is to add a 1 to the end of our vectors and expand the matrix to 4x4:

 ⌈cx⌉ ⌈m11 m12 m13 tx⌉ ⌈x⌉ |cy| = |m21 m22 m23 ty| * |y| |cz| |m31 m32 m33 tz| |z| ⌊ 1⌋ ⌊ 0 0 0 1⌋ ⌊1⌋ 

(If you look at any orthogonal MVP matrix - e.g. from glOrtho() - you'll find the 4th row is 0 0 0 1. Sometimes it is even left implicit.) If you work through the maths you will see that that is the same as

 ⌈cx⌉ ⌈m11 m12 m13⌉ ⌈x⌉ ⌈tx⌉ |cy| = |m21 m22 m23| * |y| + |ty| ⌊cz⌋ ⌊m31 m32 m33⌋ ⌊z⌋ ⌊tz⌋ 

The 4th component is called w, and while it doesn't have to be 1, it nearly always is (before a transformation anyway; afterwards it is usually re-homogenised by dividing the the whole vector by w so it is 1 again). It's kind of a hack to allow transformation matrices to include translation.

Edit

I believe the original motivation was for perspective projections, which are impossible with 3D coordinates. There are other transformations you can only do with 4D vectors, but translation is the easiest to understand.

• Explain your downvotes. Jan 15, 2015 at 12:00
• +1, assuming the information is correct, this was a good explanation and was helpful to me. thanks
– Luke
Feb 10, 2015 at 0:52

There is also one more reason that I see and that was not mentioned in the previous answers.

Translation matrices are 4x4 so that you can also translate the object around "the world". Because with a 3x3 matrix you can rotate and scale a 3d coordinate, but you can translate a 3d coordinate only with a 4x4 matrix, from here the need to express the 3d coordinates in a 4d vector.

• you only "need" a 4d vector under such definitions. a 4x4 matrix isn't just for the mere purpose of adding like everyone likes to assume and claim to others. if all you wanted was to add translation after rotation (to a 3d point) you'd just define a 4x3 matrix. it's much more efficient if that's all you're after. you don't have to restrict yourself to rules that were made for a whole bundle of other reasons just cause it looks neater. lol Dec 14, 2019 at 16:59