GAMES101 Notes_Lec03~04_Transform

1 Why study transformation Why study transformation

1.1 Model transformation Modeling

  • Scene display - describe the position movement of the camera
  • Robot Dancing - Rotation of Objects
  • Pixar opening animation Desk lamp squashes the letter I - Scaling of objects

1.2 View transformation Viewing

  • Rasterized imaging involves a large number of view transformations
    insert image description here

2 Two-dimensional transformation 2D Transformations

The essence of 3B1B line generation (visual understanding of linear transformation): https://www.bilibili.com/video/BV1ns41167b9?spm_id_from=333.999.0.0

2.1 Scale transformation matrix Scale Matrix

  • Non-Uniform without locking ratio
    insert image description here

( x ′ y ′ ) = ( s x 0 0 s y ) ( x y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { s _ { x } } & { 0 } \\ { 0 } & { s _ { y } } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} (xy)=(sx00sy)(xy)

2.2 Symmetric transformation matrix Reflection Matrix

  • Flip Horizontal reflection horizontally:
    insert image description here

( x ′ y ′ ) = ( − 1 0 0 1 ) ( x y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { - 1 } & { 0 } \\ { 0 } & { 1 } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} (xy)=(1001)(xy)

2.3 Shear Matrix

  • The horizontal displacement is 0 at y=0, a at y=1, and the vertical displacement is always 0
    insert image description here

( x ′ y ′ ) = ( 1 a 0 1 ) ( x y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { 1 } & { a } \\ { 0 } & { 1 } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} (xy)=(10a1)(xy)

2.4 Rotation transformation matrix (default around the origin, counterclockwise direction) Rotation Matrix

  • The angle of rotation is θ
    insert image description here

R θ = ( cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ) R _ { \theta } = \begin{pmatrix} { } { \cos \theta } & { -\sin \theta } \\ { \ sin \theta } & { \cos \theta } \end{pmatrix}Ri=(cosisinisinicosi)

2.5 Linear Transforms Linear Transforms

  • Use matrix multiplication of the same dimension, in the form of multiplying coordinates and matrices to get new coordinates
    ( x ′ y ′ ) = ( abcd ) ( xy ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } \\ { c } & { d } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix}(xy)=(acbd)(xy)

3 Homogeneous Coordinates

3.1 Why learn homogeneous coordinates Why homogeneous coordinates

  • Translation transformation is not a linear transformation and cannot be directly expressed in matrix form
    insert image description here

( x ′ y ′ ) = ( a b c d ) ( x y ) + ( t x t y ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } \\ { c } & { d } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} + \begin{pmatrix} { } { t _ { x } } \\ { t y } \end{pmatrix} (xy)=(acbd)(xy)+(txty)

  • We don't want translation to be a special case
  • Is there a uniform way to represent all transformations, and what is its cost? Is there a unified way to represent all transformations? (and what's the cost?)

3.2 Solve the problem through homogeneous coordinates Solution: Homogenous Coordinates

3.2.1 Add a third coordinate Add a third coordinate (w-coordinate)

2 D   p o i n t = ( x , y , 1 ) T , 2 D   v e c t o r = ( x , y , 0 ) T \rm{2D\,point}=(x,y,1)^T,\quad\rm{2D\,vector}=(x,y,0)^T 2 Dpoint=(x,y,1)T,2 Dvector=(x,y,0)T

3.2.2 Matrix representation of translations

( x ′ y ′ w ′ ) = ( 1 0 t x 0 1 t y 0 0 1 ) ⋅ ( x y 1 ) = ( x + t x y + t y 1 ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \\ { w ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { 1 } & { 0 } & { t_x } \\ { 0 } & { 1 } & { t_y } \\ { 0 } & { 0 } & { 1 } \end{pmatrix}\cdot\begin{pmatrix} { } { x } \\ { y } \\ { 1 } \end{pmatrix} = \begin{pmatrix} { } { x + t_x } \\ { y + t_y } \\ { 1 } \end{pmatrix} xyw=100010txty1xy1=x+txy+ty1

3.2.3 What if the translation is a vector? What if you translate a vector?

  • The vector has translation invariance, after the translation transformation, it is still the original vector
    ( x ′ y ′ w ′ ) = ( 1 0 tx 0 1 ty 0 0 1 ) ⋅ ( xy 0 ) = ( xy 0 ) \begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \\ { w ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { 1 } & { 0 } & { t_x } \\ { 0 } & { 1 } & { t_y } \\ { 0 } & { 0 } & { 1 } \end{pmatrix}\cdot\begin{pmatrix} { } { x } \\ { y } \\ { 0 } \end{pmatrix} = \begin{pmatrix} { } { x } \\ { y } \\ { 0 } \end{pmatrix}xyw=100010txty1xy0=xy0

3.2.4 The beauty of homogeneous coordinates

  • If the result of w-coordinate is 1 or 0, it is a valid operation, which not only makes the form of translation transformation uniform, but also retains the operability of vector and point addition and subtraction operations
  • When using homogeneous coordinates, (x, y, w) means a point in the plane (x/w, y/w, 1), w≠0
    • vector + vector = vector 0+0=0
    • point – point = vector 1-1=0
    • point + vector = point 1+0=1
    • point + point = ??

The result obtained by point+point is the midpoint of the line connecting two points (w-coordinate is changed to 1)

3.2.5 Affine Transformations

  • Affine transformation = linear transformation + translation transformation Affine map = linear map + translation
    ( x ′ y ′ ) = ( abcd ) ⋅ ( xy ) + ( txty ) \begin{pmatrix} { } { x ^ { \prime } } \ \ { y ^ { \prime } } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } \\ { c } & { d } \end{pmatrix} \cdot \begin{pmatrix} { } { x } \\ { y } \end{pmatrix} + \begin{pmatrix} { } { t_x } \\ { t_y } \end{pmatrix}(xy)=(acbd)(xy)+(txty)
  • 使用齐次坐标 Using homogenous coordinates:
    A f f i n e    T r a n s f o r m a t i o n s : ( x ′ y ′ 1 ) = ( a b t x c d t y 0 0 1 ) ⋅ ( x y 1 ) \rm{Affine\;Transformations:}\begin{pmatrix} { } { x ^ { \prime } } \\ { y ^ { \prime } } \\ { 1 } \end{pmatrix} = \begin{pmatrix} { } { a } & { b } & { t _ { x } } \\ { c } & { d } & { t _ { y } } \\ { 0 } & { 0 } & { 1 } \end{pmatrix} \cdot \begin{pmatrix} { } { x } \\ { y }\\{1} \end{pmatrix} AffineTransformationsxy1=ac0bd0txty1xy1

3.2.6 2D Transformations using homogeneous coordinates

S c a l e : S ( s x , s y ) = ( s x 0 0 0 s y 0 0 0 1 ) \rm{Scale:}S (s _ { x , } s _ { y } ) = \begin{pmatrix} {} { s _ { x } } & { 0 } & { 0 } \\ { 0 } & { s _ { y } } & { 0 } \\ { 0 } & { 0 } & { 1 } \end{pmatrix} ScaleS(sx,sy)=sx000sy0001

R otation : R ( α ) = ( cos ⁡ α − sin ⁡ α 0 sin ⁡ α cos ⁡ α 0 0 0 1 ) \rm{Rotation:}R ( \alpha ) = \begin{pmatrix} {} { \cos \soft } & { - \sin \soft } & { 0 } \\ { \sin \soft } & { \cos \soft } & { 0 } \\ { 0 } & { 0 } & { 1 } \end{ pmatrix}R o t a t i o n : R ( a )=cosasina0sinacosa0001

T r a n s l a t i o n : T ( t x , t y ) = ( 1 0 t x 0 1 t y 0 0 1 ) \rm{Translation:}T ( t _ { x , } t _ { y } ) = \begin{pmatrix} { } { 1 } & { 0 } & { t x } \\ { 0 } & { 1 } & { t _ { y } } \\ { 0 } & { 0 } & { 1 } \end{pmatrix} TranslationT(tx,ty)=100010txty1

The cost of homogeneous coordinates is the introduction of additional numbers involved in the calculation

4 Inverse Transform Inverse Transform

  • M − 1 M^{-1} M1 is not only the inverse of the M matrix, but also the geometric inverse of the matrix
    insert image description here

5 Composite Transform

5.1 The order of transformations affects the result! Transform Ordering Matters!

  • Translate and then rotate vs. rotate and translate
    insert image description here

  • From the perspective of matrix operations, matrix multiplication does not satisfy the commutative law Matrix multiplication is not commutative
    R 45 ⋅ T ( 1 , 0 ) ≠ T ( 1 , 0 ) ⋅ R 45 R _ { 45 } \cdot T _ { ( 1 , 0 ) } \neq T _ { ( 1 , 0 ) } \cdot R _ { 45 }R45T(1,0)=T(1,0)R45

  • Note that matrices are applied right to left
    T ( 1 , 0 ) ⋅ R 45 ( xy 1 ) = ( 1 0 1 0 1 0 0 0 1 ) ( cos ⁡ 4 5 ∘ − sin ⁡ 4 5 ∘ 0 sin ⁡ 4 5 ∘ cos ⁡ 4 5 ∘ 0 0 0 1 ) ( xy 1 ) T ( 1 , 0 ) \cdot R _ { 45 } \begin{pmatrix} { } { x } \ \ { y } \\ { 1 } \end{pmatrix} = \begin{pmatrix} {} { 1 } & { 0 } & { 1 } \\ { 0 } & { 1 }&{0} \\ { 0 } & { 0 } & { 1 } \end{pmatrix}\begin{pmatrix} { } { \cos 45 ^ { \circ } } & { - \sin 45 ^ { \circ } } & { 0 } \\ { \sin 45 ^ { \circ } } & { \cos 45 ^ { \circ } } & { 0 } \\ { 0 } & { 0 } & { 1 } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \\ { 1 } \end{pmatrix}T(1,0)R45xy1=100010101cos45sin450sin45cos450001xy1

5.2 Composite Transform

  • For the sequence of affine transformations A 1 , A 2 , A 3 , ..., the associative law can be used first to obtain a single matrix representing the combined transformation, which is very important for performance consumption!
    A n ( ⋯ A 2 ( A 1 ( X ) ) ) = A n ⋯ A 2 ⋅ A 1 ⏟ P re − multiply ⋅ ( xy 1 ) A _ { n } ( \cdots A _ { 2 } ( A _ { 1 } ( X ) ) ) =\underbrace{ A _ { n } \cdots A _ { 2 } \cdot A _ { 1 } }_{\rm{Pre-multiply}}\cdot \begin{pmatrix} {} { x } \\ { y } \\ { 1 } \end{pmatrix}An(A2(A1(X)))=Premultiply AnA2A1xy1

6 Decomposing Complex Transforms Decomposing Complex Transforms

  • How to rotate around a given point c?
    • First translate the center of rotation to the origin Translate center to origin
    • Rotate
    • Translate to original position Translate back
      insert image description here

7 3D Transformations 3D Transformations

7.1 Using homogeneous coordinates to describe points and vectors in three-dimensional space

  • Use homogeneous coordinates again:
    3 D point = ( x , y , z , 1 ) T , 3 D vector = ( x , y , z , 0 ) T \rm{3D\,point}=( x,y,z,1)^T,\quad\rm{3D\,vector}=(x,y,z,0)^T3D _point=(x,y,z,1)T,3D _vector=(x,y,z,0)T
  • When using homogeneous coordinates, (x, y, z, w) represents a point (x/w, y/w, z/w) in a three-dimensional space, w≠0
  • Use 4×4 matrices for affine transformation
    ( x ′ y ′ z ′ 1 ) = ( abctxdeftyghitz 0 0 0 1 ) ⋅ ( xyz 1 ) \begin{pmatrix} {} { x ^ { \prime } } \ \ { y ^ { \ prime } } \ \ { z ^ { \ prime } } \\ { 1 } \end{pmatrix} =\begin{pmatrix} { } { a } & { b } &{c}&{t_{x}}\\{d}&{e}&{f}&{t_{y}}\\{g}&{h}&{i}&{t_ { z } } \\ { 0 } & { 0 } & { 0 } & { 1 } \ end{pmatrix}\cdot\begin{pmatrix} { } { x } \\ { y } \\ { z } \\ { 1 } \ end { pmatrix }xyz1=adg0beh0cfi0txtytz1xyz1
  • The order of affine transformation is linear transformation first, then translation

7.2 Scale and Translation in 3D space

  • 缩放 Scale
    S(sx,sy,sz) = (sx 0000 sy00000 sz000001) S(s_{x},s_{y},s_{z}) = \begin{pmatrix}{}{s_{x}}&{0}&{0}&{0}\\{0}&{s_{y}}&{0}&{0}\\{ 0} & {0}&{s_z}&{0}\\{0}&{0}&{0}&{1}\end{pmatrix}S(sx,sy,sz)=sx0000sy0000sz00001
  • 平移 Translation
    T ( t x , t y , t z ) = ( 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ) T ( t _ { x }, t _ { y }, t _ { z }) = \begin{pmatrix} { } { 1 } & { 0 } & { 0 } & { t _ { x } } \\ { 0 } & { 1 } & { 0 } & { t _ { y } } \\ { 0 } & { 0 } & { 1 } & { t _ { z } } \\ { 0 } & { 0 } & { 0 } & { 1}\end{pmatrix} T(tx,ty,tz)=100001000010txtytz1

7.3 3D Rotations in 3D space

7.3.1 Rotation around x-, y-, or z-axis

insert image description here

R x ( α ) = ( 1 0 0 0 0 cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 1 ) R _ { x } ( \alpha ) = \begin{pmatrix} { } { 1 } & { 0 } & { 0 } & { 0 } \\ { 0 } & { \cos \soft } & { - \sin \soft } & { 0 } \\ { 0 } & { \sin \soft } & { \cos \alpha } & { 0 } \ \ { 0 } & { 0 } & { 0 } & { 1 } \ end{pmatrix}Rx( a )=10000cosasina00sinacosa00001
R y ( α ) = ( cos ⁡ α 0 sin ⁡ α 0 0 1 0 0 − sin ⁡ α 0 cos ⁡ α 0 0 0 0 1 ) R _ { y } ( \alpha ) = \begin{pmatrix} {} { \cos \soft } & { 0 } & { \sin \soft } & { 0 } \\ { 0 } & { 1 } & { 0 } & { 0 } \\ { - \sin \soft } & { } & { \cos \alpha } & { 0 } \ \ { 0 } & { 0 } & { 0 } & { 1 } \ end{pmatrix}Ry( a )=cosa0sina00100sina0cosa00001
R z ( α ) = ( cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 0 1 0 0 0 1 0 ) R _ { z } ( \alpha ) = \begin{pmatrix} { } { \cos \soft } & { - \sin \soft } & { 0 } & { 0 } \\ { \sin \soft } & { \cos \soft } & { 0 } & { 0 } \\ { 0 } &{0}&{1}&{0}\\{0}&{0}&{1}&{0}\end{pmatrix}Rz( a )=cosasina00sinacosa0000110000

Note: The matrix is ​​slightly different when rotating along the y-axis (z is multiplied by x to get the y-axis, rather than x is multiplied by z).
Bullet chat explanation: xyz, yzx, zxy, there is no essential difference between these three, understand this, and the front is easy to solve

7.3.2 Any rotation can be regarded as a combination of several rotations around the axis Compose any 3D rotation from Rx, Ry, Rz

  • A set of three parameters describing the rotation of an object is called the Euler angle Euler angles

  • Often used in flight simulators: roll, pitch, yaw
    insert image description here

  • Rodrigues' Rotation FormulaRodrigues' Rotation Formula
    rotates α around the n axis: ( n , α ) = cos ⁡ ( α ) I + ( 1 − cos ⁡ ( α ) ) nn T + sin ⁡ ( α ) ( 0 − nznynz 0 − nx − nynx 0 ) ⏟ N \rm{rotate \alpha angle around n axis:} ( n , \alpha ) = \cos ( \alpha ) I + ( 1 - \cos ( \alpha ) ) nn ^ { T } +\sin ( \alpha ) \underbrace{\begin{pmatrix} { } { 0 } & { - n _ { z } } & { n _ { y } } \\ { n _ { z } } & { 0 } & { - n _ { x } } \\ { - n _ { y } } & { n _ { x } } & { 0 } \end{pmatrix}}_{\rm{N}}Rotate α angle around n axis : ( n ,a )=cos ( α ) I+(1cos ( a ) ) n nT+sin ( α )N 0nznynz0nxnynx0

  • If you want to rotate along any axis, similar to 2D, you need to move the starting point of the rotation to the axis, then rotate, and finally move back to the original position

  • The introduction of quaternion is mainly to solve the interpolation problem between rotation angles, which is not covered in this lesson

8 Observation transformation Viewing transformation

8.1 View transformation View/Camera transformation

8.1.1 What is view transformation What is view transformation?

  • Think about how to take a photo
    1. Find a good place and arrange people (model transformation )
    2. Find a good "angle" to put the camera (view transformation )
    3. eggplant! (projection transformation) Cheese! ( projection transformation)

8.1.2 How to determine the view transformation How to perform view transformation?

  • define a camera
    • Determine the position Position e ⃗ \vec{e}e
    • Determine the direction to look Look-at / gaze direction g ^ \hat{g}g^
    • Determine the upward direction (to determine the left and right tilt direction of the camera) Up direction t ^ \hat{t}t^
      insert image description here

8.1.3 Standard location Key observation

  • If the camera and all objects move together, the “photo” will be the same
    insert image description here

  • We usually move the position of the camera to the origin, the upward direction is the Y-axis direction, and the -Z-axis direction is the upward direction. The object and the camera are transformed at the same time. We always transform the camera to the origin, up at Y, look at -Z, and transform the objects along with the camera
    insert image description here

  • Through the matrix M view M_{view}MviewTo transform the camera Transform the camera by M view M_{view}Mview
    M v i e w = R v i e w T v i e w M_{view}=R_{view}T_{view} Mview=RviewTview

    1. Translate e to origin
      T view = ( 1 0 0 − xc 0 1 0 − yc 0 0 1 − zc 0 0 0 1 ) T _ {view } = \begin{pmatrix} { } { 1 } & { 0 }&{0}&{-x_{c}}\\{0}&{1}&{0}&{-y_{c}}\\{0}&{0}&{1}& {-z_c}\\{0}&{0}&{0}&{1}\end{pmatrix}Tview=100001000010xcyczc1
    2. Rotate g to -Z, t to Y, (gxt) To X
      R v i e w − 1 = ( x g ^ × t ^ x t x − g 0 y g ^ × t ^ y t y − g 0 z g ^ × t ^ z t z − g 0 0 0 0 1 )    ⟹    R v i e w = ( x g ^ × t ^ y g ^ × t ^ z g ^ × t ^ 0 x t y t z t 0 x − g y − g z − g 0 0 0 0 1 ) R^{-1}_{view}=\begin{pmatrix} {} { x _ { \hat{g} \times \hat{t}} } & { x _ { t } } & { x _ { -g } } & { 0 } \\ { y _ { \hat{g} \times \hat{t}} } & { y _ { t } } & { y _ { - g } } & { 0 } \\ { z _ {\hat{g} \times \hat{t}}} & { z _ { t } } & { z _ { - g } } & { 0 } \\ { 0 } & { 0}&{0}&{1}\end{pmatrix}\implies R_{view}=\begin{pmatrix} {} { x _ { \hat{g} \times \hat{t}} } & { y _ { \hat{g} \times \hat{t}} } & { z _ { \hat{g} \times \hat{t}} } & { 0 } \\ { x _ { t } } & { y _ { t } } & { z _ { t } } & { 0 } \\ { x _ { - g } } & { y _ { - g } } & { z _ { - g } } & { 0 } \\ { 0 } & { 0 }&{ 0 }&{ 1 }\end{pmatrix} Rview1=xg^×t^yg^×t^zg^×t^0xtytzt0xgygzg00001Rview=xg^×t^xtxg0yg^×t^ytyg0zg^×t^ztzg00001

The rotation matrix is ​​an orthogonal matrix (Orthogonal Matrix). Here, because the inverse matrix is ​​relatively easy to write, the rotation matrix is ​​obtained by using the property that the inverse of the orthogonal matrix is ​​equal to its transpose.

8.2 Projection transformation Projection transformation

8.2.1 Perspective projection vs. orthographic projection

  • The human eye is closer to perspective projection, orthographic projection is often used in industrial graphics
  • "Near big, far small", "One leaf blinds the eye", "I understand the truth, but why is the pigeon so big" all refer to perspective projection
    insert image description here

insert image description here

8.2.2 Orthographic projection

A simple way of understanding

  1. Camera located at origin, looking at -Z, up at Y
  2. Remove the Z-axis coordinate Drop Z coordinate
  3. Translate and scale the resulting rectangle to [ − 1 , 1 ] 2 [-1, 1]^2[1,1]2 Translate and scale the resulting rectangle to [ − 1 , 1 ] 2 [-1, 1]^2 [1,1]2

Formal understanding In general

  • We want a cubic space [ l , r ] × [ b , t ] × [ f , n ] [l, r] × [b, t] × [f, n][l,r]×[b,t]×[f,n ] is mapped to a standard cube[ − 1 , 1 ] 3 [-1, 1]^3[1,1]3 We want to map a cuboid [ l , r ] × [ b , t ] × [ f , n ] [l, r] × [b, t] × [f, n] [l,r]×[b,t]×[f,n] to the “canonical cube [ − 1 , 1 ] 3 [-1, 1]^3 [1,1]3

  • When transforming, first translate (the center moves to the origin), and then scale (the length, width and height become 2) Translate (center to origin) first, then scale (length/width/height to 2) M ortho = ( 2 r − l
    0 0 0 0 2 t − b 0 0 0 0 2 n − f 0 0 0 0 1 ) ( 1 0 0 − r + l 2 0 1 0 − t + b 2 0 0 1 − n + f 2 0 0 0 1 ) M_{ortho}=\begin{pmatrix} { } { \frac { 2 } { r - l } } & { 0 } & { 0 } & { 0 } \\ { 0 } & { \frac { 2 } { t - b } } & { 0 } & { 0 } \\ { 0 } & { 0 } & { \frac { 2 } { n - f } } & { 0 } \\ { 0 } & { 0 } & { 0 } & { 1 } \end{pmatrix}\begin{pmatrix} {} { 1 } & { 0 } & { 0 } & { - \frac { r + l } { 2 } } \\ { 0 } & { 1 } & { 0 } & { - \frac { t + b } { 2 } } \\ { 0 } & { 0 } & { 1 } & { - \frac { n + f } { 2 } } \\ { 0 } & { 0 } & { 0 } & { 1 } \end{pmatrix}Mortho=rl20000tb20000nf2000011000010000102r+l2t+b2n+f1
    insert image description here

  • Precautions

    • When the camera looks in the -Z direction, the near coordinates will be greater than the far coordinates Looking at / along -Z is making near and far not intuitive (n > f)
    • That's why OpenGL (a Graphics API) uses left hand coords

8.2.3 Perspective projection

How to do perspective projection?

  1. First “squish” the frustum into a cuboid (n -> n, f -> f)( M persp → ortho M_{persp\to ortho}Mpersportho)
  2. Do orthographic projection Do orthographic projection ( M ortho M_{ortho}Mortho, already known!)
    insert image description here

Matrix derivation process of "squeeze" transformation

  1. The relationship before and after coordinate transformation can be obtained from similar triangles Find the relationship between transformed points (x', y', z') and the original points (x, y, z)
    insert image description here

y ′ = n z y , x ′ = n z x y\prime=\frac{n}{z}y\rm{,}x\prime=\frac{n}{z}x y=znyx=znx
2. At this time, the transformed coordinates containing an unknown can be written using homogeneous coordinates. In homogeneous coordinates
( xyz 1 ) ⇒ ​​( nx / zny / zuknown 1 ) = = ( nynystill unknownz ) \begin{pmatrix} { } { x } \\ { y } \\ { z } \\ { 1 } \end{pmatrix} \Rightarrow \begin{pmatrix} { } { nx / z } \\ { ny / z } \\ { un know wn} \\ { 1 } \end{pmatrix} = = \begin{pmatrix} { } { ny } \\ { ny} \\ { still\;unk now }\\{z} \end{pmatrix}xyz1nx/zn y / zunknown1==n yn ystillunknownz
3. So the "squish" transformation matrix has the following relationship So the "squish" (persp to ortho) projection does this
M persp → ortho ( 4 × 4 ) ( xyz 1 ) = ( nxnyunknownz ) M^{(4×4 )}_{persp\to ortho}\begin{pmatrix} { } { x } \\ { y } \\ { z } \\ { 1 } \end{pmatrix} = \begin{pmatrix} { } { nx } \\ { ny } \\ {unknown} \\{ z } \end{pmatrix}Mpersportho(4×4)xyz1=nxn yunknownz
4. We can already fill in part of the above information M persp → ortho M_{persp\to ortho}Mpersportho Already good enough to figure out part of M p e r s p → o r t h o M_{persp\to ortho} Mpersportho(Next, just find out what the number on the third row of the matrix is)
M persp → ortho = ( n 0 0 0 0 n 0 0 ? ? ? 0 0 1 0 ) M_{persp\to ortho }=\begin{pmatrix} { } { n } & { 0 } & { 0 } & { 0 } \\ { 0 } & { n } & { 0 } & { 0 } \\ { ? } & { ? } & { ? } & { ? } \\ { 0 } & { 0 } & { 1 } & { 0 } \end{pmatrix}Mpersportho=n0?00n?000?100?0
5. 任何在近的平面上的点坐标都不变 Any point on the near plane will not change
M p e r s p → o r t h o ( 4 × 4 ) ( x y z 1 ) = ( n x n y u n k n o w n z ) r e p l a c e    z    w i t h    n → M p e r s p → o r t h o ( 4 × 4 ) ( x y n 1 ) = ( x y n 1 ) = ( n x n y n 2 n ) = ( n x n y u n k n o w n z ) M^{(4×4)}_{persp\to ortho}\begin{pmatrix} {} { x } \\ { y } \\ { z } \\ { 1 } \end{pmatrix} = \begin{pmatrix} {} { n x } \\ { n y } \\ {unknown} \\{ z } \end{pmatrix}\quad\underrightarrow{\rm{replace\;z\;with\;n}}\quad M^{(4×4)}_{persp\to ortho}\begin{pmatrix} {} { x } \\ { y } \\ { n } \\ { 1 } \end{pmatrix} =\begin{pmatrix} {} { x } \\ { y } \\ { n } \\ { 1 } \end{pmatrix}= \begin{pmatrix} {} { n x } \\ { n y } \\ {n^2} \\{ n } \end{pmatrix}=\begin{pmatrix} {} { n x } \\ { n y } \\ {unknown} \\{ z } \end{pmatrix} Mpersportho(4×4)xyz1=nxn yunknownz replacezwithnMpersportho(4×4)xyn1=xyn1=nxn yn2n=nxn yunknownz
n 2 n^2 n2 has nothing to do with x, y, soM persp → ortho M_{persp\to ortho}MpersporthoThe row must be of the form (0 0 AB)
( n 0 0 0 0 n 0 0 0 0 AB 0 0 1 0 ) ( xyn 1 ) = ( nxnyn 2 n ) ⟹ ( 0 0 AB ) ( xyn 1 ) = n 2 ⟹ A n + B = n 2 \begin{pmatrix} { } { n } & { 0 } & { 0 } & { 0} \\{0}&{n}&{0}&{0}\\{0}&{0}&{A}&{B}\\{0}&{0}&{1}& { 0 } \end{pmatrix} \begin{pmatrix} { } { x } \\ { y } \\{n}\\ { 1 } \end{pmatrix} = \begin{pmatrix} {} { nx } \ \{new}\\{n^2}\\{n}\end{pmatrix}\implies\begin{pmatrix}{}{0}&{0}&{A}&{B}\end{pmatrix} \begin{pmatrix}{}{x}\\{y}\\{n}\\{1}\end{pmatrix}=n^{2}\implies An+B=n^2n0000n0000A100B0xyn1=nxn yn2n(00AB)xyn1=n2An+B=n2
6. The Z-axis coordinates of any point on the far plane will not change, and the center point of the far plane will be (0, 0, f, 1) before and after transformation. Any point's z on the far plane will not change M p e r s p → o r t h o ( 4 × 4 ) ( x y z 1 ) = ( n x n y u n k n o w n z ) ( x , y , z , 1 ) t o ( 0 , 0 , f , 1 ) → M p e r s p → o r t h o ( 4 × 4 ) ( 0 0 f 1 ) = ( 0 0 f 1 ) = ( 0 0 f 2 f ) = ( n x n y u n k n o w n z ) M^{(4×4)}_{persp\to ortho}\begin{pmatrix} {} { x } \\ { y } \\ { z } \\ { 1 } \end{pmatrix} = \begin{pmatrix} {} { n x } \\ { n y } \\ {unknown} \\{ z } \end{pmatrix}\quad\underrightarrow{\rm{(x,y,z,1)to(0,0,f,1)}}\quad M^{(4×4)}_{persp\to ortho}\begin{pmatrix} {} { 0 } \\ { 0 } \\ { f } \\ { 1 } \end{pmatrix} =\begin{pmatrix} {} { 0 } \\ { 0 } \\ { f } \\ { 1 } \end{pmatrix}= \begin{pmatrix} {} {0} \\ {0 } \\ {f^2} \\{ f } \end{pmatrix}=\begin{pmatrix} {} { n x } \\ { n y } \\ {unknown} \\{ z } \end{pmatrix} Mpersportho(4×4)xyz1=nxn yunknownz (x,y,z,1)to(0,0,f,1)Mpersportho(4×4)00f1=00f1=00f2f=nxn yunknownz
When the third line is (0 0 AB), the relational expression
( n 0 0 0 0 n 0 0 0 0 AB 0 0 1 0 ) ( 0 0 f 1 ) = ( 0 0 f 2 f ) ⟹ ( ll 0 0 AB ) ( 0 0 f 1 ) = f 2 ⟹ A f + B = f 2 \begin{pmatrix} { } { n } & { 0 } & { 0 } & { 0 } \\ { 0 } & { n } & { 0 } & { 0 } \\ { 0 } & { 0 } & { A } & { B } \\ { 0 } & { 0 } & { 1 } & { 0 } \end{pmatrix} \begin{pmatrix } { } { 0 } \\ { 0 } \\{f}\\ { 1 } \end{pmatrix} = \begin{pmatrix} {} { 0 } \\ { 0 } \\ {f^2} \ \{ f } \end{pmatrix}\implies \begin{pmatrix} { ll } { 0 } & { 0 } & { A } & { B } \end{pmatrix} \begin{pmatrix} { } { 0 } \ \ { 0 } \\{f}\\ { 1 } \end{pmatrix} = f ^ { 2 } \implies Af+B=f^2n0000n0000A100B000f1=00f2f(l l 00AB)00f1=f2A f+B=f2
7. 解出A和B的值 Solve for A and B
{ A n + B = n 2 A f + B = f 2    ⟹    { A = n + f B = − n f \begin{cases}An+B=n^2\\Af+B=f^2\end{cases}\implies \begin{cases}A=n+f\\B=-nf\end{cases} { An+B=n2A f+B=f2{ A=n+fB=nf

  1. Finally, we already know that M persp → ortho M_{persp\to ortho}Mpersportho的每一项 Finally, every entry in Mpersp->ortho is known!
    M persp → ortho = ( n 0 0 0 0 n 0 0 0 0 n + f − nf 0 0 1 0 ) M_{persp\to ortho}=\begin{pmatrix} { } { n } & { 0 } & { . 0} & {0}\\{0}&{n}&{0}&{0}\\{0}&{0}&{n+f}&{-nf}\\{0}&{ 0 } & { 1 } & { 0 } \end{pmatrix}Mpersportho=n0000n0000n+f100nf0

In the process of extrusion, a point in the middle of the far and near planes will be pushed to the distance after the change.
My guess: this may be related to the fact that the middle of the near, large, and far is pushed to the distance when drawing (that is, a <b)
insert image description here

  • Complete perspective projection transformation
    M persp = M ortho M persp → ortho M_{persp}=M_{ortho}M_{persp\to ortho}Mpersp=MorthoMpersportho
  • How to define what's near plane's l, r, b, t in the frustum
    • Vertical viewing angle Vertical Field-of-View (fovY)

    • Aspect ratio
      insert image description here

    • How to convert from fovY and aspect to l, r, b, t?
      insert image description here

tan ⁡ f o v Y 2 = t ∣ n ∣ , a s p e c t = r t \tan \frac { f o v Y } { 2 } = \frac { t } { | n | },\quad aspect=\frac{r}{t} tan2f o v Y=nt,aspect=tr

Guess you like

Origin blog.csdn.net/Ziiur/article/details/122782287