Professional Documents
Culture Documents
3.7 Coordinate Systems in OpenGL
3.7 Coordinate Systems in OpenGL
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:
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
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.
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