Loading [MathJax]/jax/output/CommonHTML/jax.js

The Notes of Computer Graphics Ⅲ

 

2D/3D Transformation, Viewing Transformation

Linear Transformations

Scale

[xy]=[sx00sy][xy]

Reflection

[xy]=[1001][xy]

Shear

[xy]=[1a01][xy]

Rotation

cg-3-1

Rθ=[cosθsinθsinθcosˆθ]

Tip: For the rotation matrix M, which is always normalized in orthogonal form. Thus the transposed matrix MT is exactly inversed matrix M1, that is: MT=M1, where M is orthogonal matrix

Linear Transformations

  • Use the same dimension matrix
[xy]=[abcd][xy]x=Mx

Affine Transformations

Homogeneous Coordinate

  • Translation cannot be represented in matrix form
[xy]=[abcd][xy]+[txty]

Hence, translation is Not linear transform

  • But we don’t want translation to be a special case, so is there a unified way to represent all transformations?

  • Add a third coordinate, and use 3D matrix to represent translations:

    • 2D point =(x,y,1)
    • 2D vector =(x,y,0)
(xyw)=(10tx01ty001)(xy1)=(x+txy+ty1)
  • Valid operation if w-coordinate of result is 1 or 0
    • vector + vector = vector
    • point - point = vector
    • point + vector = point
    • point + point = ?

Info: In homogenous coordinates,
(xyw) is the 2D point (x/wy/w1),w0
Thus, point + point = midpoint of two points

Affine Transformations

  • Affine map = linear map + translation
(xy)=(abcd)(xy)+(txty)
  • Using homogenous coordinates:
(xy1)=(abtxcdty001)(xy1)

Note: This means linear transform first, and then translation.

Inverse Transform

M1 is the inverse of transform M in both a matrix and geometric sense.

Composite transform

Composing Transforms

  • Matrix multiplication is not commutative
  • For a sequence of affine transforms A1,A2,A3,
    • Compose by matrix multiplication to speed up: An(A2(A1(x)))=AnA2A1(xy1)
    • Pre-multiply n matrices to obtain a single matrix AnA2A1 representing combined transform

Decomposing Complex Transforms

How to rotate around a given point c?

  1. Translate center to origin
  2. Rotate
  3. Translate Back

cg-3-2

3D transformations

Rotation around x, y, z-axis

cg-3-3

Rx(α)=(10000cosαsinα00sinαcosα00001)
Ry(α)=(cosα0sinα00100sinα0cosα00001)
Rz(α)=(cosαsinα00sinαcosα0000100001)

Caveat: In Ry, where we use right-hand rule z×x get y, so the α is opposite.

Euler angles

  • To represent any 3D rotation from Rx,Ry,Rz,

Rxyz(α,β,γ)=Rx(α)Ry(β)Rz(γ)

  • (α,β,γ) is Euler angles

  • Often used in flight simulator:

cg-3-4

  • Eluer angles may cause Gimbal lock: The loss of one degree of freedom.
    • This suitation occurs when the axes of two of the three gimbals are driven into a parallel configuration as the below shows.
    • Use Unit quaternions to solve this problem

cg-3-5

Rodrigues’ Rotation Formula

  • Rotation by angle α around axis n (axis n goes through the origin)
R(n,α)=cos(α)I+(1cos(α))nnT+sin(α)(0nznynz0nxnynx0)N
  • Formula derivation

  • If the rotation axis doesn’t go through the origin, but at p

    • Translate the axis to origin. T(p)
    • Rotate. R(n,α)
    • Translate the axis back: T(p)
    • Thus: T(p)R(n,α)T(p)

Viewing transformation

  • View / Camera Transformation
  • Projection transformation
    • Orthographics projection
    • Perspective projection

A vivid analogy

  • Analogous to taking a photo:
    • Find a good place and arrage people (model transformation)
    • Find a good “angle” to put the camera (view transformation)
    • Cheese! (projection transformation)

View / Camera Transformation

Define the camera

  • Define the camera first (suppose every objects has been arranged properly)
    • Position e
    • Look-at / gaze direction ˆg
    • up direction ˆt

cg-3-6

  • Key observation: If the camera and all objects move together, the photo will be the same. Thus we transform the camera to the fixed origin point.
    • Position: the origin
    • Look-at direction: -Z
    • up direction: Y
    • Then transform the objects along with the fixed camera

cg-3-7

Transform the camera

  • Transform the camera by Mview
    • To locate it at the origin, up at Y, look at Z
      • Translates e to origin
      • Rotates t to Y
      • Rotates g to Z
      • If t to Y and g to Z, then ˆg׈t will be to X

cg-3-8

  • Mview=RviewTview
    • Translate e to origin
      Tview=[100xe010ye001ze0001]
    • Difficult to directly get Rview, so consider its inverse rotation:
      R1view=[xˆg׈txtxg0yˆg׈tytyg0zˆg׈tztzg00001]Rview=(R1view)T=[xˆg׈tyˆg׈tzˆg׈t0xtytzt0xgygzg00001]

Tip: We can validate the result of the multiplication of R1view and the vector (1000) which represents X axis, the vector (0100) which represents Y axis, and the vector (0010) which represents Z axis. We could find X axis rotates to ˆg׈t, Y rotates to t and Z rotates to g.

Note: Each column of the Rview except for the last column is the unit vector describing the camera, so the transposed matrix of it is its inversed matrix itself.

Projection transformation

Projection in Computer Graphics

  • In orthogonal projection, the camera can be supposed to be placed infinite way from the viewing frustum (now the frustum becomes cuboid).

cg-3-9

Orthographic Projection

  • Camera located at origin, looking at -Z, up at Y
  • Drop Z coordinate
  • Translate and scale the resulting rectangle to [1,1]2

cg-3-10

  • More in general: map a cuboid [l,r]×[b,t]×[f,n] to the canonical cube [1,1]3. (left, right, bottom, top, far, near)
    • Translate the center to origin first, then scale each edge to 2.
Mortho=[2rl00002tb00002nf00001][100r+l2010t+b2001n+f20001]

Note: Objects may be stretched out of the original scale, so the next viewport transform will deal with this and draw it back to the original scale.

Caveat: Looking along Z makes n > f (near > far) and this is the reason why OpenGL uses left hand coords.

Perspective Projection

  • Most common in Computer Graphics, art, visual system
  • Further objects are smaller
  • Parallel lines not parallel, converge to single point

Note: We only consider Euclidean Geometry (Not Riemannian Geometry)

The step to do perspective projection

  1. First squish the frustum into a cuboid Mpersportho
  2. Do orthographic projection Mortho

cg-3-11

  • Denote vertical field-of-view (fovY) and aspect ratio (assume symmetry i.e. l=r b=t)

cg-3-13

Aspect Ratio=widthheight
  • Convert fovY and aspect to l, r, b, t

cg-3-14

tanfovY2=t|n| aspect =rt
  • Find the relationship between transformed points (x,y,z) and original points (x,y,z))

cg-3-12

y=nzy

Similar to x:

x=nzx
  • Use homogenous coordinates to represent this relationship:
(xyz1)(nx/zny/z unknown 1)==(nxny still unknown z)

Tip: according to the points defined in homogenous coordinates. reference

  • So the operation squish should be the following form:
M(4×4)persportho(xyz1)=(nxny unknown z)
  • Three known parameters could get a good reference of three rows of Mpersportho:
Mpersportho=(n0000n00????0010)
  • Two important observations, which is responsible for z:

1. Any point on the near plane will not change

M(4×4)persportho(xyz1)=(nxny unknown z)

Replace z with n:

(xyn1)(xyn1)==(nxnyn2n)

Thus, the third row must be of the form (0 0 A B):

(00AB)(xyn1)=n2 An+B=n2

Tip: n is only the known paramter, and has nothing to do with z, so A and B can not be determined yet.

2. Any z of points on the far plane will not change

(00f1)(00f1)==(00f2f) Af+B=f2
  • From the two above equations we get, we could solve A and B:
{An+B=n2Af+B=f2{A=n+fB=nf
  • Now, every entry of Mpersportho is known:
Mpersportho=(n0000n0000n+fnf0010)
  • Finally, do orthographic projection Mortho to finish

  • In summary: Mpersp=MorthoMpersportho

Reference

Gitalking ...