Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Coordinate Systems in OpenGL

Stefano Markidis and Sergio Rivas-Gomez


Three Key-Points
• To transform our vertices from the local (to the object) coordinates to
the Normalized Device Coordinates (NDC) requires three major
transformations with associated matrices: model, view and projection
transformations
• The final matrix combining model, view and projection
transformation is called MVP and it is obtained as matrix-matrix
multiplication of the individual transformation matrices
• We can use GLM to form the model, view and projection matrices
and then pass them as mat4 uniform to the vertex shader
Normalized Device Coordinates
• OpenGL expects all the vertices to be in normalized device coordinates:
• That is, the x, y and z coordinates of each vertex should be between -1.0 and 1.0
• In the vertex shader transform these coordinates to NDC.
• Transforming coordinates to NDC is usually accomplished in a step-by-
step fashion where we transform an object’s vertices to several
coordinate systems
• The reason we’re transforming our vertices into all these different spaces
is that some operations make are easier to use in certain coordinate
systems.
The Global Picture
Local Coordinates

• To transform the coordinates in one Model Matrix


space to the next coordinate space we’ll
use several transformation matrices of
which the most important are the World Coordinates
model, view and projection matrix.
• Our vertex coordinates first start in local View Matrix
space as local coordinates and are then
further processed to world coordinates, View/Camera Coordinates
view coordinates, clip coordinates and
eventually end up as screen Projection Matrix
coordinates.
Clip Coordinates
From Local to World Coordinates: Model Matrix
• Local coordinates are the
coordinates of your object relative
to its local origin at (0,0,0)
9.2 Local space
• We want to define a position for
each object to position them inside
a larger world with world
coordinates
• The model matrix transforms a
position in a model to the position
in the world by combining
translation, scale and rotation
• We can use GSM to build and apply
this matrix
From World to View/Camera coordinates
• The
9.2 view
Local space is the
space 86
space as seen from the
camera’s point of view
(also user or eye view).
• In the next lecture, we
will focus on how to
create a combination of
of translations and
rotations stored inside a
view matrix to simulate a
camera.
1. Local coordinates are the coordina
your object begins in.
From View to Clip Space
• At the vertex shader, coordinates have to be
be within a specific range.
• If any coordinate that falls outside this range is
clipped.
• To transform vertex coordinates from view
to clip-space we define a so called
projection (3D to 2D) matrix to transform
coordinates within this specified range to
NDC (-1.0, 1.0).
• This viewing box a projection matrix creates
1. Local coordinates are the coordinates of your object relative to its
is called a frustum and each coordinate thatyour object begins in.
ends up inside this frustum will end up on2. The next step is to transform the local coordinates to world-space c
Frustum
the user’s screen respect of a larger world. These coordinates are relative to a globa
many other objects also placed relative to the world’s origin.
• Two projection matrices: 3. Next we transform the world coordinates to view-space coordinate
• Orthographic projection matrix is as seen from the camera or viewer’s point of view.
• Perspective projection matrix. 4. After the coordinates are in view space we want to project them to
are processed to the -1.0 and 1.0 range and determine which v
5. And lastly we transform the clip coordinates to screen coordin
9.5 Clip space

Orthographic Projection width, height and length of the visible frustum. All the coordinates that end up inside thi
transforming them to clip space with the orthographic projection matrix won’t be clipped. Th
a bit like a container:

• An orthographic projection matrix defines a Frustum


cube-like frustum box.
• Specify the width, height and length of the
visible frustum.
• All the coordinates inside this frustum after
transforming them to clip space won’t be clipped
• To create an orthographic projection matrix we
make use of GLM’s function glm::ortho()The frustum defines the visible coordinates and is specified by a width, a height and a ne
Any coordinate in front of the near plane is clipped and the same applies to coordinates behi
The orthographic frustum directly maps all coordinates inside the frustum to normalized dev
since the w component of each vector is untouched; if the w component is equal to 1.0 pers
Distance
doesn’t change Distance
the coordinates.
Left Right Bottom Top Near plane Far plane
To create an orthographic projection matrix we make use of GLM’s built-in function gl

glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f);

The first two parameters specify the left and right coordinate of the frustum and the t
Perspective Projection 9.5 Clip space
9.5 Clip space 89

• In real life, objects that are farther away


appear much smaller. Frustum
• The perspective projection tries to mimic this
effect using a perspective projection matrix.
• The projection matrix maps a given
frustum range to clip space, but also
manipulates the w value of each vertex
coordinate in such a way that the further
away a vertex coordinate is from the
viewer, the higher this w component As you can see, due to perspective the lines seem to coincide the farther they’re away. This is exactly
the effect perspective projection tries to mimic and it does so using a perspective projection matrix. The
becomes. projection matrix maps a given frustum range to clip space, but also manipulates the w value of each vertex
coordinate in such a way that the further away a vertex coordinate is from the viewer, the higher this w
Its first parameter defines the fov value, that stands for field of view and sets how large t
• This is another reason why the w component
component becomes. Once the coordinates are transformed to clip space they are in the range -w to w
For a realistic
(anything outside this range is clipped). OpenGL requires that theview
visibleitcoordinates
is usuallyfall set to 45
between degrees, but for more doom-style results you could
the range

is important -1.0 and 1.0 as the final vertex shader output, thus once the coordinates are in clip space, perspectiveratio which is calculated by dividing the viewpo
value.
division is applied to the clip space coordinates:
The second parameter sets the aspect
height. The third and fourth parameter set the near and far plane of the frustum. We usu
distance to 0.1f and the far distance to 100.0f. All the vertices between the near and far
FOV(field
0 1
x/w of viewtheangle)
frustum will be rendered. Aspect Ratio
out = @y/wA
z/w
Whenever the near value of your perspective matrix is set a bit too high (like 10.0
Each component of the vertex coordinate is divided by its w component giving smaller vertex coordinates
will clip all coordinates close to the camera (between 0.0f and 10.0f), which giv
the further away a vertex is from the viewer. This is another reason why the w component is important,
visual
since it helps us with perspective projection. The resulting resultareinthen
coordinates videogames in that
in normalized device you can see through certain objects if you move
space.
Near plane Far plane If you’re interested to figure out how the orthographicthem.
and perspective projection matrices are actually
calculated (and aren’t too scared of mathematics) I can recommend this excellent article by Songho.

A perspective projection matrix can be createdWhen


in GLM using orthographic
as follows: projection, each of the vertex coordinates are directly mapp
The MVP: the Model View Projection Matrix
• We create
9.6 Putting a transformation matrix that
it all together Local Coordinates
combines model, view and projection matrices
by multiplying
You can see that withprojection
perspectiveby view andthemodel
projection, vertices farther away appear
Model much smaller, whi
Matrix
matrices
orthographic and obtaining
projection each vertexthe
has Model
the sameView
distance to the user.
Projection Matrix World Coordinates
Putting
• A itvertex
all together
coordinate is then transformed to clip
x View Matrix
We createcoordinates
a transformation matrix for each of the aforementioned
simply applying the MVP to the steps: model, view and projection ma
A vertex vertex
coordinate is then
in local transformed to clip coordinates as follows:
coordinates: View/Camera Coordinates

x Projection Matrix
V _clip = M_pro jection · M_view · M_model ·V _local
Clip Coordinates
Note that the order of matrixMVPmultiplication is reversed (remember that we need to read matrix m
plication from right to left). The resulting vertex should then be assigned to gl_Position in the ve
= MVP Matrix
shader and OpenGL will then automatically perform perspective division and clipping.
Putting Everything Together
• Apply an MVP to our first triangle with the code
MVP_transf_first_triangle.cpp
• Use GSM to form the model, view and projection
matrices on CPU
• Move them to the vertex shader on the GPU
using uniform
• Make the matrix-matrix multiplication in the
vertex shader on the GPU
• On Mac with GLFW: g++
MVP_transf_first_triangle.cpp
glad.c -o
MVP_transf_first_triangle -Wall -
framework Cocoa -framework OpenGL
-framework IOKit -framework
CoreVideo -lglfw
To Summarize
• To transform our vertices from the local coordinates to the NDC
requires model, view and projection transformations/matrices
• The final matrix combining model, view and projection
transformation is called MVP and it is simply obtained as matrix-
matrix multiplication of the individual transformation matrices
• We can use GLM to form the model, view and projection matrices
and then pass them as mat4 uniform to the vertex shader

You might also like