The Graphics Pipeline and Opengl I:: Transformations!
The Graphics Pipeline and Opengl I:: Transformations!
Transformations!
Gordon Wetzstein!
Stanford University!
!
• geometry + transformations!
• cameras and viewing!
• lighting and shading!
• rasterization!
• texturing!
!
Some History!
GPU = massively!
parallel processor!
• OpenGL is our The Graphics Pipeline!
interface to the
GPU!!
• right: “old-school”
OpenGL state
machine!
• today’s lecture:
vertex transforms!
• higher-level library that provides a lot of useful helper functions, tools, and
abstractions around WebGL – easy and convenient to use!
• https://threejs.org/!
• simple examples: https://threejs.org/examples/!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
The Graphics Pipeline!
1. Vertex Processing: Process and transform individual vertices & normals.!
2. Rasterization: Convert each primitive (connected vertices) into a set of fragments. A fragment can
be interpreted as a pixel with attributes such as position, color, normal and texture.!
3. Fragment Processing: Process individual fragments.!
4. Output Merging: Combine the fragments of all primitives (in 3D space) into 2D color-pixel for the
display.!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
The Graphics Pipeline!
1. Vertex Processing: Process and transform individual vertices & normals.!
2. Rasterization: Convert each primitive (connected vertices) into a set of fragments. A fragment can
be interpreted as a pixel with attributes such as position, color, normal and texture.!
3. Fragment Processing: Process individual fragments.!
4. Output Merging: Combine the fragments of all primitives (in 3D space) into 2D color-pixel for the
display.!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
The Graphics Pipeline!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Coordinate Systems!
• right hand coordinate system!
wikipedia!
Primitives!
• vertex = 3D point v(x,y,z)!
• triangle = 3 vertices!
n3
• normal = 3D vector per vertex describing
n2
surface orientation n=(nx,ny,nz)! v3
v2
n1
v1
Pixels v Fragments!
• fragments have rasterized 2D coordinates on screen but a lot of other attributes
too (texture coordinates, depth value, alpha value, …)!
• pixels appear on screen!
• won’t discuss in more detail today!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
vertex/normal!
transforms!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
2. Position and orientation the camera (View
transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
2. Position and orientation the camera (View
transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
2. Position and orientation the camera (View
transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
2. Position and orientation the camera (View
transform).!
3. Select a camera lens (wide angle, normal or
telescopic), adjust the focus length and zoom
factor to set the camera's field of view
(Projection transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
2. Position and orientation the camera (View
transform).!
3. Select a camera lens (wide angle, normal or
telescopic), adjust the focus length and zoom
factor to set the camera's field of view
(Projection transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
2. Position and orientation the camera (View
transform).!
3. Select a camera lens (wide angle, normal or
telescopic), adjust the focus length and zoom
factor to set the camera's field of view
(Projection transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Vertex Transforms!
1. Arrange the objects (or models, or avatar) in
the world (Model Transform).!
2. Position and orientation the camera (View
transform).!
3. Select a camera lens (wide angle, normal or
telescopic), adjust the focus length and zoom
factor to set the camera's field of view
(Projection transform).!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Model Transform!
⎛ x ⎞
• transform each vertex v = ⎜ y ⎟ from object coordinates to world coordinates!
⎜ ⎟
⎜⎝ z ⎟⎠
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Model Transform - Scaling!
⎛ x ⎞
• transform each vertex v = ⎜ y ⎟ from object coordinates to world coordinates!
⎜ ⎟
⎜⎝ z ⎟⎠
⎛ sx 0 0 ⎞⎛ x ⎞ ⎛ sx x ⎞
⎜ ⎟ ⎟ =⎜ s y ⎟
scaled vertex = matrix-vector product:! Sv = ⎜ 0 sy 0 ⎟⎜ y
⎜ ⎟ ⎜ y ⎟
⎜ 0
⎝ 0 sz ⎠ ⎜⎝ z
⎟ ⎟⎠ ⎜ s z
⎝ z
⎟
⎠
Model Transform - Rotation!
⎛ x ⎞
• transform each vertex v = ⎜ y ⎟ from object coordinates to world coordinates!
⎜ ⎟
⎜⎝ z ⎟⎠
⎛ x ⎞ ⎛ dx ⎞ ⎛ x + dx ⎞
⎜ y ⎟ + ⎜ dy ⎟ = ⎜ y + dy ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜⎝ z ⎟⎠ ⎜ d ⎟ ⎜ z + d ⎟
⎝ z ⎠ ⎝ z ⎠
that’s unfortunate L!
Model Transform - Translation!
⎛ x ⎞
⎜ y ⎟
• solution: use homogeneous coordinates, vertex is! v=⎜ ⎟
⎜ z ⎟
⎜⎝ 1 ⎟⎠
3. translation is 4x4 matrix!
⎛ 1 0 0 dx ⎞ ⎛ 1 0 0 dx ⎞ ⎛ x ⎞ ⎛ x + dx ⎞
⎜ ⎟ ⎜ ⎟ ⎟ ⎜ y+d ⎟
0 1 0 dy ⎟ 0 1 0 dy ⎟ ⎜ y
T (d) = ⎜ Tv = ⎜ ⎜ ⎟ =⎜ y ⎟
⎜ 0 0 1 dz ⎟ ⎜ 0 0 1 dz ⎟ ⎜ z ⎟ ⎜ z + dz ⎟
⎜ ⎟ ⎜ ⎟⎜ ⎟⎠ ⎜⎜ ⎟
⎜⎝ 0 0 0 1 ⎟⎠ ⎜⎝ 0 0 0 1 ⎟⎠ ⎝ 1 ⎝ 1 ⎟⎠
better J!
Summary of Homogeneous Matrix Transforms!
⎛ 1 0 0 dx ⎞
⎜ ⎟
0 1 0 dy ⎟
• translation! T (d) = ⎜
⎜ 0 0 1 dz ⎟
⎜ ⎟
⎜⎝ 0 0 0 1 ⎟⎠
! ⎛ sx 0 0 0 ⎞
⎜ ⎟
0 sy 0 0 ⎟
• scale! S(s) = ⎜
⎜ 0 0 sz 0 ⎟
⎜ ⎟
! ⎜⎝ 0 0 0 1 ⎟⎠
! ⎛ sx 0 0 0 ⎞ ⎛ 1 / sx 0 0 0 ⎞
⎜ ⎟ ⎜ ⎟
0 sy 0 0 ⎟ ⎛ 1⎞ 0 1 / sy 0 0 ⎟
• scale S(s) = ⎜ inverse scale! S −1 (s) = S ⎜ ⎟ =⎜
⎜ 0 0 sz 0 ⎟ ⎝ s⎠ ⎜ 0 0 1 / sz 0 ⎟
⎜ ⎟ ⎜ ⎟
! ⎜⎝ 0 0 0 1 ⎟⎠ ⎜⎝ 0 0 0 1 ⎟⎠
v2
M
t t'
v1 v'1
Vector and Normal Transforms!
⎛ nx ⎞
⎜ ⎟
ny ⎟
• homogeneous representation of a normal n=⎜
⎜ nz ⎟
(unit length, perpendicular to surface)! ⎜ ⎟
⎜⎝ 0 ⎟⎠
n' = ( M −1 ) ⋅ n
T
• need to use normal matrix = transpose of
inverse for transformation! ! v'2
!
n'
v2
n
M
t t'
v1 v'1
Vector and Normal Transforms!
⎛ nx ⎞
⎜ ⎟
ny ⎟
• homogeneous representation of a normal n=⎜
⎜ nz ⎟
(unit length, perpendicular to surface)! ⎜ ⎟
⎜⎝ 0 ⎟⎠
n' = ( M −1 ) ⋅ n
T
• need to use normal matrix = transpose of
inverse for transformation!!
• fine print: only use upper left 3x3 part of modelview matrix for inverse transpose
(no homogeneous normal representation) OR drop w component from n’ after
multiplying 4x4 inverse transpose (i.e. don’t use w for normalization of n’!)!
Attention!!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
View Transform!
• so far we discussed model transforms, e.g. going from object or model space to
world space !
• one simple 4x4 transform matrix is sufficient to go from world space to camera
or view space!!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
View Transform!
specify camera by!
! ⎛ eyex ⎞
⎜ ⎟
• eye position! eye = ⎜ eyey ⎟
! ⎜ eye ⎟
⎝ z ⎠
⎛ centerx ⎞
⎜ ⎟
• reference position! center = ⎜ centery ⎟
⎜ center ⎟
! ⎝ z ⎠
! ⎛ upx ⎞
⎜ ⎟
• up vector! up = ⎜ upy ⎟
⎜ upz ⎟⎠
⎝
View Transform!
specify camera by! compute 3 vectors:!
! ⎛ eyex ⎞
⎜ ⎟ eye − center
• eye position! eye = ⎜ eyey ⎟ zc =
eye − center
! ⎜ eye ⎟
⎝ z ⎠
up × z c
⎛ centerx ⎞ x =
c
⎜ ⎟ up × z c
• reference position! center = ⎜ centery ⎟
⎜ center ⎟ yc = z c × x c
! ⎝ z ⎠
! ⎛ upx ⎞
⎜ ⎟
• up vector! up = ⎜ upy ⎟
⎜ upz ⎟⎠
⎝
View Transform!
view transform M is translation into eye position, ! compute 3 vectors:!
followed by rotation!
eye − center
! zc =
eye − center
!
up × z c
x =
c
up × z c
yc = z c × x c
View Transform!
view transform M is translation into eye position, ! compute 3 vectors:!
followed by rotation!
eye − center
! zc =
eye − center
!
up × z c
x =
c
up × z c
⎛ x xc x yc xzc 0 ⎞⎛ 1 0 0 −eyex ⎞
⎜ ⎟⎜ yc = z c × x c
⎟
⎜ yxc yyc yzc 0 ⎟⎜ 0 1 0 −eyey ⎟
M = R ⋅T (−e) = ⎜ ⎟⎜
⎜ zxc zyc zzc 0 ⎟⎜ 0 0 1 −eyez ⎟
⎟
⎜ ⎟⎜ 1 ⎟⎠
⎝ 0 0 0 1 ⎠⎝ 0 0 0
View Transform!
view transform M is translation into eye position, !
followed by rotation!
!
!
⎛
⎜
x xc x yc xzc ( )
− x xc eyex + x yc eyey + xzc eyez ⎞
⎟
⎜ yxc yyc yzc − ( y eye + y eye + y eye )
c c c ⎟
M = R ⋅T (−e) = ⎜ x x y y z z
⎟
⎜ zxc zyc zzc − ( z eye + z eye + z eye )
c c c ⎟
⎜ x x y y z z
⎟
⎜⎝ 0 0 0 1 ⎟⎠
View Transform – Attention!!
• many graphics APIs have a function called lookat that automatically computes
the view matrix for you!
• Three.js also has such a function, but that only computes the rotation, not the
translation, of the view matrix. So best implement the view matrix yourself!!
!
!
View Transform!
• in camera/view space, the camera is at the origin, looking into negative z!
• modelview matrix is combined model (rotations, translations, scaling) and view
matrix!!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
View Transform!
• in camera/view space, the camera is at the origin, looking into negative z!
up
x
z
x
z
vodacek.zvb.cz!
Projection Transform!
• similar to choosing lens and sensor of camera – specify field of view and aspect!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Projection Transform - Perspective Projection!
• have symmetric view frustum!
• fovy: vertical angle in degrees!
• aspect: ratio of width/height!
• zNear: near clipping plane (relative from cam)!
• zFar: far clipping plane (relative from cam)!
f = cot( fovy / 2)
⎛ f ⎞
⎜ aspect 0 0 0 ⎟
⎜ ⎟
⎜ 0 f 0 0 ⎟
M proj =⎜ ⎟ projection matrix!
⎜ zFar + zNear 2 ⋅ zFar ⋅ zNear ⎟
0 0 − −
⎜ zFar − zNear zFar − zNear ⎟ (symmetric frustum)!
⎜⎝ 0 0 −1 0 ⎟⎠
Projection Transform - Perspective Projection!
more general: a perspective “frustum” (truncated, possibly
sheared pyramid)!
• left (l), right (r), bottom (b), top (t): corner coordinates
on near clipping plane (at zNear)!
perspective frustum!
⎛ 2 ⋅ zNear r+l ⎞
⎜ 0 0 ⎟
r−l r−l
⎜ ⎟
⎜ 2 ⋅ zNear t +b ⎟
0 0
M proj =⎜ t −b t −b ⎟ projection matrix!
⎜ ⎟
⎜ zFar + zNear 2 ⋅ zFar ⋅ zNear ⎟
0 0 − − (asymmetric frustum)!
⎜ zFar − zNear zFar − zNear ⎟
⎜⎝ 0 0 −1 0 ⎟⎠
Projection Transform - Orthographic Projection!
more general: a “box frustum” (no perspective, objects
don’t get smaller when farther away)!
• left (l), right (r), bottom (b), top (t): corner coordinates
on near clipping plane!
⎛ 2 r+l ⎞
⎜ r−l 0 0 − ⎟
r−l
⎜ ⎟
⎜ 0 2 t +b ⎟
0 −
M proj =⎜ t −b t −b ⎟ projection matrix!
⎜ ⎟
⎜ 0 −2 f +n ⎟
0 − (orthographic)!
⎜ f −n f −n ⎟
⎜ 0 ⎟
⎝ 0 0 1 ⎠
Perspective! Orthographic!
[Song Ho Ahn]!
Projection Transform!
• possible source of confusion for zNear and zFar:!
!
• Marschner & Shirley define it as absolute z coordinates, thus zNear>zFar and
both values are always negative !
• OpenGL and we define it as positive values, i.e. the distances of the near and
far clipping plane from the camera (zFar > zNear)!
Modelview Projection Matrix!
• put it all together with 4x4 matrix multiplications!!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Normalized Device Coordinates (NDC)!
from: OpenGL Programming Guide!
width
xwindow =
2
( xNDC + 1) + x
⎞ ∈( 0, width )
height
ywindow =
2
( yNDC + 1) + y ⎛ xclip / wclip ⎞ ⎛ x
window
⎜ ⎟ ⎜ ⎟
1 1 y /w ⎜ y ⎟ ∈( 0,height )
zwindow = zNDC +
vNDC = ⎜ clip clip ⎟ vwindow = ⎜ window ⎟
2 2 ⎜ z /w ⎟
⎜ clip clip ⎟ ⎜ z window ⎟ ∈( 0,1)
⎜⎝ 1 ⎟⎠ ⎜⎝ 1 ⎟⎠
all vertex
transforms
from today!!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
The Graphics Pipeline!
• assign fixed color (e.g. red) to each vertex in window coordinates (fragment)!
• interpolate (i.e. rasterize) lines between vertices (as defined by user)!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
… and we can almost do this …!
https://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html!
Further Reading!
• course notes on transforms (see course website)!