Project a Vertex and Its Normal Through a Render Pipeline
Math & Bit ManipulationAuto-graded · 18 test casesLast 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
- Extend
pointto the homogeneous vector(x, y, z, 1). - Compute
clip = projection * (view * (model * p)). - Perform the perspective divide:
ndc = (clip[0] / clip[3], clip[1] / clip[3], clip[2] / clip[3]). - Compute the screen position as a 3-element list:
screen_x = (ndc_x + 1) * width / 2screen_y = (ndc_y + 1) * height / 2screen_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
pointandnormaleach have exactly 3 numbers;normalis non-zero.- All matrices are 4x4 with finite entries.
- The upper-left 3x3 block of
modelis invertible. clip[3](the w component) is never zero for the given inputs.viewportcontains 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The task has two independent parts.
Position. Treat the point as a homogeneous vector (x, y, z, 1) and multiply it by the model, view and projection matrices in that order. This gives clip coordinates. Dividing the first three components by the fourth (the perspective divide) gives normalized device coordinates. The screen x and y then come from the linear map (v + 1) * size / 2, and the z value is passed through as the NDC z.
Normal. A normal must stay perpendicular to the surface after the surface is transformed. If A is the linear part of the model matrix, tangent vectors transform as t' = A t. We look for G such that (G n)^T (A t) = n^T G^T A t is still zero whenever n^T t = 0. Choosing G^T A = I, that is G = (A^-1)^T, guarantees this. Translation does not matter for directions, so only the 3x3 block is used. The result is rescaled to unit length because non-rotation transforms change the length.
For the 3x3 inverse I use the adjugate divided by the determinant. Multiplying by the transpose of the inverse is done by indexing the inverse as inv[col][row].
Why it is correct
Every step is a direct application of the definitions: matrix-vector products, the perspective divide, a linear viewport map, and the inverse-transpose rule, which preserves orthogonality between normal and tangents. No clipping or special cases are applied, as the statement requires.
Complexity
Everything operates on fixed-size 4x4 and 3x3 matrices, so the time and space complexity are both O(1).
Reference solution
import math
from typing import Dict, List
def project_vertex_and_normal(point: List[float], model: List[List[float]], view: List[List[float]], projection: List[List[float]], viewport: List[float], normal: List[float]) -> Dict[str, List[float]]:
def mat_vec(matrix, vector):
return [sum(matrix[r][c] * vector[c] for c in range(4)) for r in range(4)]
p = [point[0], point[1], point[2], 1.0]
eye = mat_vec(view, mat_vec(model, p))
clip = mat_vec(projection, eye)
ndc = [clip[i] / clip[3] for i in range(3)]
width, height = viewport
screen = [
(ndc[0] + 1.0) * width / 2.0,
(ndc[1] + 1.0) * height / 2.0,
ndc[2],
]
a, b, c = model[0][0], model[0][1], model[0][2]
d, e, f = model[1][0], model[1][1], model[1][2]
g, h, i = model[2][0], model[2][1], model[2][2]
det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)
inv = [
[(e * i - f * h) / det, (c * h - b * i) / det, (b * f - c * e) / det],
[(f * g - d * i) / det, (a * i - c * g) / det, (c * d - a * f) / det],
[(d * h - e * g) / det, (b * g - a * h) / det, (a * e - b * d) / det],
]
# inverse-transpose applied to the normal: result[r] = sum_c inv[c][r] * normal[c]
n = [sum(inv[col][row] * normal[col] for col in range(3)) for row in range(3)]
length = math.sqrt(sum(v * v for v in n))
n = [v / length for v in n]
return {"ndc": ndc, "screen": screen, "normal": n}