Lecture_006_Perspective_Projection_and_Texture_Mapping

Transformations

Transformation of Coordinate System

Transformation of Coordinate System

Transformation on camera is the same as transformation on all triangles

Perspective Projection

Notice that all previous transformation from local, world, to view space is all done using matrix. The transformation from view space to clip space can also be done by matrix.

Our 4x4 perspective transform matrix can be constructed like follow: Notice the matrix does not do the perspective projection (division by z).

/// Compute perspective projection matrix with
///  fov: vertical field of view (in degrees),
///   ar: aspect ratio (x/y)
///    n: near plane.
///
/// The camera is located at the origin looking down the -z axis
///  with y up and x right.
/// The far plane is at infinity.
///
/// This projection maps (x,y,z,1) to (x',y',z',w') such that:
///  - all visible points have w'>0 and (x',y',z')/w' in [-1,1]^2
///  - points on the near plane (z=-n) map to points with z'/w'= -1.0
///  - points on the far 'plane' (z=-inf) map to points with z'/w'= 1.0
///  - objects are closer if their mapped depth is lower
///  - w' must be z for perspective-correct interpolation
static Mat4 perspective(float fov, float ar, float n);
\begin{bmatrix} c_x & 0 & 0 & 0\\ 0 & c_y & 0 & 0\\ 0 & 0 & m_1 & m_2\\ 0 & 0 & 1 & 0\\ \end{bmatrix} \cdot \begin{bmatrix} x\\ y\\ z\\ 1\\ \end{bmatrix} = \begin{bmatrix} c_x x\\ c_y y\\ m_1 z + m_2\\ z\\ \end{bmatrix}

Note that this matrix is fixed as a constant after we constructed for the entire duration of the rendering. This is so that we can speed up the transformation in GPU (the same matrix will be used for all geometry coordinates). Therefore we can't just copy our z into the matrix to save a z value, since z is geometry-dependent.

Explanation:

CAREFUL! In above formulas, some z must be flipped since camera looks down to -z axis instead of positive z. Also make sure the view port aspect ratio is the same as view plane aspect ratio.

Focal Length: If we assume 1 unit focal length, then the angle would be 30 degree.

Focal Length: If we assume 1 unit focal length, then the angle would be 30 degree.

Instead of dividing by -z (assuming we are facing -z axis), if we need angle \theta as view angle we need to divide by -z \cdot \tan \theta. If we are dividing by -z, we assume view angle to be 45^\circ.

Perspective Correct Interpolation

Vec3 barycentric = barycentric_coordinate(coord, va, vb, vc);
float z = 1.0f/barycentric_interpolate_2D(barycentric, a.inv_w, b.inv_w, c.inv_w);
frag.attributes[i] = z * barycentric_interpolate_2D(barycentric, a.attributes[i] * a.inv_w, b.attributes[i] * b.inv_w, c.attributes[i] * c.inv_w);

Watch this video

Clipping

View Frustum: a near and a far plane

View Frustum: a near and a far plane

Clipping: eliminating triangles not in view frustum to save rasterizing primitives

Clipping Half Overlapped Triangles by Splitting Shape to Triangles

Clipping Half Overlapped Triangles by Splitting Shape to Triangles

Why near/far clipping planes?

Z-Fighting Effect is Larger with no Enough Precision in Depth Buffer (don't set near, far plane too big)

Z-Fighting Effect is Larger with no Enough Precision in Depth Buffer (don't set near, far plane too big)

Non-Perspective Frustum Transformation: it is not perspective because it warpped space that aligns to camera's perspective

Non-Perspective Frustum Transformation: it is not perspective because it warpped space that aligns to camera's perspective

To again get perspective: copy z to w in homogeneous coordinate.

Full Perspective Matrix: takes view frustum and projection into account

Full Perspective Matrix: takes view frustum and projection into account

Screen Transformation

Translate 2D viewing plane to pixel coordinates

  1. reflect about x-axis
  2. translate by (1, 1)
  3. scale by (W / 2, H / 2)

Color Interpolation

Color Interpolation: we want to use vertex color to interpolate color inside triangle

Color Interpolation: we want to use vertex color to interpolate color inside triangle

Linear interpolation in 1D

Linear interpolation in 1D

Linear interpolation in 1D is Linear Combination of Two Functions

Linear interpolation in 1D is Linear Combination of Two Functions

In 1D: since we have equation \hat{f}(t) = (1 - t)f_i + tf_j, we can think of this as a linear combination of two functions.

Linear interpolation in 2D

Linear interpolation in 2D

Linear Interpolation in 2D:

\begin{bmatrix} a\\ b\\ c\\ \end{bmatrix} = \frac{1}{(x_iy_i - x_iy_j)+(x_ky_j - x_jy_k)+(x_iy_k - x_ky_i)} \begin{bmatrix} f_i(y_k - y_j) + f_j(y_i - y_k) + f_k(y_j - y_i)\\ f_i(x_j - x_k) + f_j(x_k - x_i) + f_k(x_i - x_j)\\ f_i(x_ky_j - x_jy_k) + f_j(x_iy_k - x_ky_i) + f_k(x_jy_i - x_iy_j)\\ \end{bmatrix}

Linear interpolation in 2D is Linear Combination of Three Functions

Linear interpolation in 2D is Linear Combination of Three Functions

These picture describes the same as above complicated function.

Interpolate based on the three triangular area creased by a point in triangle

Interpolate based on the three triangular area creased by a point in triangle

Barycentric Coordinates: \phi_i(x), \phi_j(x), \phi_k(x)

Triangular Half-plane Test

Triangular Half-plane Test

We should not interpolate in screen space, but in 3D space. How to solve this problem?

We should not interpolate in screen space, but in 3D space. How to solve this problem?

Perspective Incorrect Interpolation: compute barycentric coordinates using 2D coordinates leads to derivative discontinuity

Perspective Incorrect Interpolation: compute barycentric coordinates using 2D coordinates leads to derivative discontinuity

Perspective Correct Interpolation: we interpolate attribute \phi

  1. compute depth z at each vertex
  2. interpolate \frac{1}{z} and \frac{\phi}{z} using 2D barycentric coordinates to give perspective
  3. divide interpolated \frac{\phi}{z} by interpolated \frac{1}{z}

normal map and displacement map

normal map and displacement map

Texture Mapping

Texture Mapping

Texture Coordinates

Texture Coordinates

Periodic Texture Coordinates

Periodic Texture Coordinates

Given a model with UV:

Sampled Texture Space might be warpped, it is hard to avoid aliasing

Sampled Texture Space might be warpped, it is hard to avoid aliasing

Solution to Magnification

Solution to Magnification

Magnification: camera too close to object

Aliasing due to minification

Aliasing due to minification

Ideal minification result

Ideal minification result

Minification: camera too far to object

Mip Map

Since averaging in large texture area is very costly, we tend to pre-compute mip map. Mip map is only smaller than original image by a factor of 2^2 because this way we can ensure we only need the value of 4 pixels to compute the value of 1 pixel

Mip Map: a specific prefiltering technique

MIP map (L.Williams 83) store prefiltered image at every possible scale

MIP map (L.Williams 83) store prefiltered image at every possible scale

MIP map storage cost is low

MIP map storage cost is low

Calculating MIP Map Level by estimating region cover using partial derivative

Calculating MIP Map Level by estimating region cover using partial derivative

The result in above picture expresses u, v in pixel coordinates [0, W] \times [0, H], not texture coordinate [0, 1] \times [0, 1]. Imagine you have a very small base picture, but you look at it from far away, you would still show un-minified texture.

Different MIP Map Levels

Different MIP Map Levels

MIP Map all level 1

MIP Map all level 1

MIP Map all level 4

MIP Map all level 4

But we don't want jumps between MIP Map levels

But we don't want jumps between MIP Map levels

MIP Map blending

MIP Map blending

Tri-linear Interpolation: interpolation after interpolation

Tri-linear Interpolation: interpolation after interpolation

Isotropic Filtering (Trilinear) vs. Anisotropic Filtering

Isotropic Filtering (Trilinear) vs. Anisotropic Filtering

Pipeline:

  1. from screen space (x, y) to barycentric coordinates
  2. using barycentric coordinates to interpolate (u, v) stored in vertices
  3. approximate \frac{du}{dx}, \frac{du}{dy}, \frac{dv}{dx}, \frac{dv}{dy} by taking differences of screen-adjacent samples and compute mip map level d
  4. convert normalized texture coordinate (u, v) \in [0, 1] to pixel locations in texture image (U, V) \in [W, H].
  5. determine address of pixels for filter (8 neighbors for trilinear)
  6. Load texels into local registers
  7. tri-linear interpolation according to (U, V, d)
  8. maybe anisotropic filtering

Table of Content