KatInterview

Project a Vertex and Its Normal Through a Render Pipeline

Math & Bit ManipulationAuto-graded · 18 test cases
MediumAuto-gradedMath & Bit Manipulationlinear-algebramatriceshomogeneous-coordinates

Last updated

Apply model, view and projection matrices to a point, do the perspective divide and viewport mapping, and transform a normal with the inverse-transpose of the model's 3x3 block.

You are given a simplified graphics pipeline. Implement it for a single vertex and its surface normal.

Inputs

  • point: a list of 3 numbers [x, y, z], a position in object space.
  • model: a 4x4 matrix (list of 4 rows) that takes object space to world space.
  • view: a 4x4 matrix that takes world space to eye space.
  • projection: a 4x4 matrix that takes eye space to clip space.
  • viewport: a list [width, height] giving the screen size in pixels.
  • normal: a list of 3 numbers, the surface normal in object space.

All matrices are applied to column vectors, so a matrix M times a vector v gives entry i as the sum over j of M[i][j] * v[j].

Position

  1. Extend point to the homogeneous vector (x, y, z, 1).
  2. Compute clip = projection * (view * (model * p)).
  3. Perform the perspective divide: ndc = (clip[0] / clip[3], clip[1] / clip[3], clip[2] / clip[3]).
  4. Compute the screen position as a 3-element list:
    • screen_x = (ndc_x + 1) * width / 2
    • screen_y = (ndc_y + 1) * height / 2
    • screen_z = ndc_z, copied unchanged with no remapping.

Do not clip or reject any point, even if it lies outside the view volume or clip[3] is negative. Just apply the formulas above.

Normal

Let A be the upper-left 3x3 block of model. The transformed normal is (A^-1)^T * normal, scaled to unit length (Euclidean norm 1). The translation part of model, and the view and projection matrices, do not affect the normal.

Output

Return a dictionary with three keys, each mapped to a list of 3 floats:

  • "ndc": the normalized device coordinates.
  • "screen": the screen-space values as defined above.
  • "normal": the unit-length transformed normal.

Results are compared numerically, so ordinary floating-point rounding is acceptable.

Constraints

  • point and normal each have exactly 3 numbers; normal is non-zero.
  • All matrices are 4x4 with finite entries.
  • The upper-left 3x3 block of model is invertible.
  • clip[3] (the w component) is never zero for the given inputs.
  • viewport contains two positive numbers.
  • No clipping, rounding, or rearranging of the output is required.

Examples

Example 1

Input: project_vertex_and_normal([0, 0, -2], [[2, 0, 0, 1], [0, 1, 0, -2], [0, 0, 0.5, 3], [0, 0, 0, 1]], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, -1], [0, 0, -1, 0]], [800, 600], [1, 1, 1])

Output: {'ndc': [-0.5, 1, 1.5], 'screen': [200, 600, 1.5], 'normal': [0.2182178902359924, 0.4364357804719848, 0.8728715609439696]}

Example 2

Input: project_vertex_and_normal([1, 2, 3], [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], [100, 200], [0, 0, 1])

Output: {'ndc': [1, 4, 3], 'screen': [100, 500, 3], 'normal': [0, 0, 1]}

Example 3

Input: project_vertex_and_normal([2, -1, -4], [[0, -1, 0, 3], [1, 0, 0, -2], [0, 0, 1, 1], [0, 0, 0, 1]], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, -1], [0, 0, -1, 0]], [1920, 1080], [1, 0, 0])

Output: {'ndc': [1.3333333333333333, 0, 0.6666666666666666], 'screen': [2239.9999999999995, 540, 0.6666666666666666], 'normal': [0, 1, 0]}

Hints

Hint 1

Build the homogeneous vector with w = 1 and write a small helper that multiplies a 4x4 matrix by a 4-vector. Apply model, then view, then projection, in that order.

Hint 2

After the projection, divide the first three components by the fourth. Then map x and y with (v + 1) * size / 2, and leave z as the NDC value.

Hint 3

For the normal, take only the 3x3 block of the model matrix, invert it (the adjugate divided by the determinant works for 3x3), and multiply the transpose of that inverse by the normal. Normalize at the end.