Coordinate Systems in OpenGL

There are multiple coordinate systems involved in 3D Graphics. In this article i will try to explain how they interact with each other and what’s their purpose in a friendly and easy way.

What always has helped me in understanding 3D Math, Coordinate Systems and Transformations – Hands and Objects! Use them, play around with your (empty) Coffee Cup, draw axis and objects … it’s hard to grasp the basics but believe me: At some point it makes Bang and you understand it.

Part 1: Coordinate Systems in General and in OpenGL especially

Most 3D Systems use the so called “Cartesian Coordinate System” – you probably know this in from School where it’s heavily used in Geometry to draw different things. One property of a Cartesian coordinate System is that the cardinal axis are perpendicular. Take a look at the 2D Cartesian Coordinate System below:

The X and Y axis are perpendicular and range from -infinity to +infinity on each axis. Let’s expand it to the 3rd Dimension. We simply add ad new axis which goes through the origin and is perpendicular to X and Y – we call this new axis Z:

One might ask why the positive X is to the right, the positive Y goes up and the positive Z goes forward in our direction. That’s because of the Handiness of the coordinate System. There are two different Systems: Right-Handed and Left-Handed coordinate systems. OpenGL uses a Right-Handed System (same as the one shown above).  As a reminder, do the following:

  • Stretch your Right Arm and form a 90° angle with your Elbow
  • Point with your Thumb to the right side
  • Point with your Pointing Finger up
  • Point with your Middle Finger in your direction

Your Hand should now form a right-handed coordinate System.

Part 2: The different Coordinate Systems in OpenGL

There are multiple coordinate Systems involved in 3D Graphics:

  • Object Space
  • World Space (aka Model Space)
  • Camera Space (aka Eye Space or View Space)
  • Screen Space (aka Clip Space)

So, what’s the difference between them?

Object Space

This is the local coordinate System of your Geometrical Objects. Imagine a Cube which consists of 8 vertices:

When modelling this cube you do this in Object Space – the Object stands alone for itself, no other things in here.

World Space

This is the Space where everything is positioned. Imagine the Cube created above – it has been modelled in it’s own absolute Object Space. But you probably want the Cube at some other location in your World – so we need to transform the Object Coordinate System to the desired World coordinate System position. We do this using a Transformation Matrix – every vertex in the Cube (which is in Object Space) is multiplied by the World Matrix which transforms the vertex to it’s new position and orientation.

Let’s explain this with a example:

  1. We have the Cube shown above
  2. We want to position it in World coordinates at position X=5, Y=0, Z=0 (simply move it 5 units to the right)

What we need to do, is to multiply each vertex with the corresponding Model Matrix for the actual positioning of this object (we have different Model Matrices for differently located objects). The resulting Vertex has the new position in World Space.

Even if the math for doing the matrix multiplication is out of the scope i want to show you an example:

The Vector V2 is [-1,-1,1]. We have to use a 4 Component Vector (x,y,z,w) so we set w simply at 1 (-1,-1,1,1)
The Matrix M1 is:

[1,0,0,5,
0,1,0,0,
0,0,1,0,
0,0,0,1]

When we now multiply those two (M1 . V2) we get a new vector: [4,-1,1,1] – which is V2 moved 5 units to the left. This calculation is done for every vertex in the cube. If you don’t know how to actually do the matrix multiplication, just read one of the books mentioned at the bottom of this article (or the corresponding wikipedia article, your old math book, whatever, … :-))

Camera Space

Now that we have positioned everything in our World we want to look at it. First, there is no such “real” thing as a Camera in OpenGL. Think about the following for a short time:

  • Moving a Video Camera Backward is the same as moving the filmed object forward

That’s exactly what we are going to do to get our view on the scene – multiply every vertex in the World Space with our View Matrix. Each Vertex is then in Camera Space – and the scene looks like we are looking at it through the camera.

Imagine the camera as a abstract thing which is on the positive Z-Axis and looks down the negative end of the Z-Axis. Imagine also a rotation and translation of the World around you so that you can see what you want to see – this rotation and translation is the View Matrix.

As for the World Space, the Mathematics for doing this is out of Scope for this Overview.

Projection Space

So, we have every vertex in a position that we see the scene the way we want it to see. The last Coordinate System transformation is from Camera to Screen Space – which is essentially nothing more than going from the 3D Coordinate System to the 2D Screen in front of us. This transformation is necessary as long as we don’t have real 3D Holographic Screens. In the process of transforming from Camera to Screen Space you can either choose a Orthographic or Perspective Projection.

The difference between those two is that the Orthographic Projection does not apply a perspective distortion and the perspective one does. Perspective Projection is the natural one as we Humans see in a perspective Way – things farther away from us appear smaller.

Graphical overview of the Transformation

If you never worked with 3D graphics before it’s probably more intuitive to take a look at the graphic below to get an idea on how the transformations actually modify the coordinates of the vertices:

From top left to bottom right, the following actions occur:

  1. Vertices of the Object to draw are in Object space (as modelled in your 3D Modeller)
  2. … get transformed into World space by multiplying it with the Model Matrix
  3. Vertices are now in World space (used to position the all the objects in your scene)
  4. … get transformed into Camera space by multiplying it with the View Matrix
  5. Vertices are now in View Space – think of it as if you were looking at the scene throught “the camera”
  6. … get transformed into Screen space by multiplying it with the Projection Matrix
  7. Vertex is now in Screen Space – This is actually what you see on your Display.

Part 3: Further Reading

There are many good books out which explain the Mathematics behind those Transformations in a good and easy to understanding way – i can absolutely recommend you the following two books:

OpenGL Superbible 5th Edition:

It’s all about OpenGL Programming – and completly relies on the Programmable Pipeline present in OpenGL 3.x and higher. Definitively worth a read if you want to learn OpenGL

3D Math Primer for Graphics and Game Development

It’s a complete Book on only one subject: 3D (and also 2D) Computer Graphics. Read this one if you really want to understand what’s going on under the hood.

If you can read German, i also recommend you the Book “XNA Spieleprogrammierung” published by MITP. Even if it’s for XNA the Math Chapter is excellent and is freely available as a sample chapter here

Please keep in mind that you should have at least a good and solid understanding of Basic Mathematics before reading those two books – doing multiplications, divisions, solving parenthesis and solving simple equations should make you not running away. Dig out your old School Books ! :-)

One last thing i need to mention is that most books talk about a MODELVIEW Matrix. Because moving an Object around and “positioning the camera” is actually the same they use just one matrix. For the sake of simplicity i like to keep them seperate – should be no problem for actual hardware to make a few more matrix multiplications … (first from object to world and then from world to view)