Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Windowing and the viewing transformation

How to select a 2D/3D viewing window, build the viewing (camera) transform, and map the canonical view to a display viewport (including the math and examples).

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Windowing and the viewing transformation are the steps that let a graphics program convert a part of the world you want to show into pixels on a screen. Engineers use these ideas when implementing scene cameras, viewports, user-zoom/pan, 2D GUI clipping, and the camera stage before projection (so it links to Parallel and Perspective projection topics).

Key ideas

  • Window (2D): the rectangular region in world coordinates selected for display; defined by xwmin, ywmin, xwmax, ywmax (in world units).
  • Viewing transformation (3D): the change of coordinates that moves points from world coordinates into a camera (view) coordinate system. This uses a View Reference Point (VRP), a View Plane Normal (VPN) and a View Up Vector (VUP) to build an orthonormal camera basis u, v, n (u is right, v is up, n is view direction). After this transform the camera is at origin and looks along a principal axis.
  • Canonical view volume: a standard cube (for parallel) or frustum / canonical cube (after perspective divide) to which we map all view coordinates to simplify clipping and rasterization. Typical NDC ranges: OpenGL uses x,y,z in [−1, +1]; Direct3D/Vulkan use z in [0, 1] (x,y in [−1, +1]). State which you assume.
  • Perspective divide: in perspective projection, after multiplying by the projection matrix we divide by the homogeneous w to get normalized device coordinates (NDC). This is called the perspective divide and moves the frustum to a canonical cube.
  • Exact steps (2D windowing): translate the window minimum (xwmin,ywmin) to the origin, scale the window to the viewport size, translate to the viewport origin. Universities expect the derivation of the affine map from these steps.

Derivation (2D window-to-viewport mapping) — show on answer sheet:

  1. Draw two rectangles: one labelled Window with corners (xwmin,ywmin) and (xwmax,ywmax), and one labelled Viewport with corners (xvmin,yvmin) and (xvmax,yvmax). Draw a point P(xw,yw) inside the window and an arrow to its image P'(xv,yv) inside the viewport.
  2. Compute scale factors: sx = (xvmax - xvmin) / (xwmax - xwmin), sy = (yvmax - yvmin) / (ywmax - ywmin).
  3. Affine mapping (no axis inversion): xv = xvmin + (xw - xwmin) * sx, yv = yvmin + (yw - ywmin) * sy.
  4. Matrix form (homogeneous 3x3): represent as composition T_vp · S · T_win where T_win translates window min to origin, S scales by sx,sy, and T_vp translates to viewport origin. Write each 3x3 matrix and multiply if required by exam.

Derivation (3D viewing transform) — steps to show on answer sheet:

  1. Draw world axes and a camera at VRP, with a view direction given by VPN and an up direction VUP. Label VRP, VPN, VUP.
  2. Form the orthonormal basis: n = normalize(VPN) (pointing from view toward scene or vice-versa — state your convention); u = normalize(cross(VUP, n)); v = cross(n, u).
  3. Translate the world by −VRP so VRP maps to origin.
  4. Rotate so the u, v, n axes align with the camera coordinate axes (x,y,z). The combined 4×4 view matrix is R · T where R has rows u, v, n and T is translation by −VRP.
  5. For perspective: apply projection matrix that maps camera frustum to canonical cube, then do perspective divide (divide by w) to get NDC. After NDC, map to viewport as a 2D window-to-viewport step.

State assumptions clearly in exam: coordinate conventions (right-handed or left-handed), whether VPN points toward the scene or toward the camera, whether device y increases downwards (screen pixel rows), and that VUP is not colinear with VPN (otherwise u cannot be formed).

Formulas

sx = (xvmax - xvmin) / (xwmax - xwmin) where sx is scale in x (unitless); applies for linear mapping from window to viewport in x.

sy = (yvmax - yvmin) / (ywmax - ywmin) where sy is scale in y (unitless); applies for linear mapping from window to viewport in y.

xv = xvmin + (xw - xwmin) * sx xv in pixels; xw in world units (e.g., metres).

yv = yvmin + (yw - ywmin) * sy yv in pixels, yw in metres.

(3×3 homogeneous matrix for 2D mapping) M = [ sx 0 xvmin - sx * xwmin ; 0 sy yvmin - sy * ywmin ; 0 0 1 ] M maps [xw yw 1]^T to [xv yv 1]^T; units follow xw/yw and xv/yv.

(Unit vectors and camera basis) n = normalize(VPN) unitless vector; normalize means divide by length (length in metres cancels, result unitless).

u = normalize(cross(VUP, n)), v = cross(n, u) all unitless orthonormal vectors.

(View matrix 4×4: translate then rotate (V = R · T)) V = [ ux uy uz -dot(u,VRP) ; vx vy vz -dot(v,VRP) ; nx ny nz -dot(n,VRP) ; 0 0 0 1 ] V maps world homogeneous coordinate to camera coordinates; dot in metres.

(Perspective projection to view plane at z = -d with camera looking along -z — camera coordinates) x_proj = -d * x_cam / z_cam meters; d in metres, x_cam,z_cam in metres; valid when z_cam < 0 (point in front of camera under this sign convention).

y_proj = -d * y_cam / z_cam meters; same units and sign condition.

(After perspective projection, use window-to-viewport mapping formulas to go to pixels.)

Worked examples

Example 1 — 2D window-to-viewport numeric mapping (standard exam question) Given: Window corners xwmin=100 m, ywmin=100 m, xwmax=500 m, ywmax=400 m. Viewport corners xvmin=0 px, yvmin=0 px, xvmax=800 px, yvmax=600 px. Map point P(xw,yw)=(300 m, 250 m). Steps:

  1. Compute widths: window width = xwmax − xwmin = 500 − 100 = 400 m; window height = ywmax − ywmin = 400 − 100 = 300 m.
  2. Compute scales: sx = (800 − 0)/400 = 2.0 (unitless), sy = (600 − 0)/300 = 2.0.
  3. Apply mapping formulas: xv = 0 + (300 − 100) * 2.0 = 400 px yv = 0 + (250 − 100) * 2.0 = 300 px Answer: The mapped point is (400 px, 300 px).

Example 2 — 3D camera to viewport via perspective (simple, camera axes aligned) Assumptions stated on answer paper: camera VRP = (0,0,0), camera looks along negative z (so view plane at z = -d), camera axes aligned with world axes (no rotation). This simplifies demonstration of perspective math — always state such assumptions. Assume viewport y increases upward; if device y increases downward, invert y in the mapping (use sy < 0 and an extra translation). Given: A world point P = (1 m, 2 m, −5 m) (so it is in front of camera), view plane distance d = 2.0 m (plane z = −2.0 m). The view-window (on the view plane) is x in [−1 m, +1 m], y in [−1 m, +1 m]. Viewport is xv in [0,800] px and yv in [0,600] px. Map P to pixel coordinates. Steps:

  1. Because camera axes are aligned and VRP at origin, camera coordinates = world coordinates: x_cam=1, y_cam=2, z_cam=−5 (m).
  2. Perspective projection onto view plane z=−d using x' = -d * x_cam / z_cam, y' = -d * y_cam / z_cam. x' = -2 * 1 / (-5) = 0.4 m y' = -2 * 2 / (-5) = 0.8 m
  3. Map x',y' from view-window [−1,1] to viewport [0,800] and [0,600]: normalized x = (x' − (−1)) / (1 − (−1)) = (0.4+1)/2 = 0.7; normalized y = (0.8+1)/2 = 0.9. xv = 0 + 0.7 * 800 = 560 px yv = 0 + 0.9 * 600 = 540 px Answer: The pixel position is (560 px, 540 px).

Example 3 — describe on paper: derive the 3×3 homogeneous matrix for mapping a general 2D window to a viewport and show it multiplies [x y 1]^T. In an exam write the three matrices T_win, S, T_vp and multiply to get the final form shown earlier in Formulas.

Common mistakes

  • Not handling y-axis inversion for display devices where screen y increases downwards; this changes sy sign and requires an extra translation.
  • Ignoring window/viewport aspect ratio differences; to avoid distortion, use a uniform scale s = min(sx, sy) and center the image (letterbox/pillarbox).
  • Using the wrong cross-product order when building u, v, n (e.g., cross(n, VUP) instead of cross(VUP, n)), causing a handedness flip.
  • In 3D: failing to normalize u, v, n before building rotation matrix — leads to shear and scale errors.
  • Sign errors in perspective projection: if you assume view plane at z = +d but camera looks along −z you will have wrong signs. State and use consistent convention.
  • Forgetting the perspective divide (w) after multiplying a 4×4 projection matrix.

For your exam

Types of questions you will get from this topic:

  • Definitions: define Window, Viewport, Viewing transformation, Canonical view volume, VRP, VPN, VUP. A full-marks answer defines the term and gives one sentence about its role.
  • Short notes: list steps to build the view matrix; explain the purpose of the canonical view volume; explain perspective divide.
  • Derivations: derive the 2D affine transform from window to viewport and produce the homogeneous matrix; derive the view matrix from VRP, VPN, VUP.
  • Clipping: explain Cohen–Sutherland region codes and Liang–Barsky for line clipping, and Sutherland–Hodgman for polygon clipping; perform a simple line/polygon clip by hand.
  • Numericals: compute the mapping of a point from world to viewport (2D and 3D). Show units, intermediate arithmetic and final pixel values.
  • Diagrams: draw world axes, camera with VRP, indicate VPN and VUP, draw window and viewport rectangles and arrows showing mapping. Label all points and vectors.

A full-marks answer always: states conventions (handedness, axis directions), lists steps, shows intermediate values, and labels diagrams.

Quick check

  1. For a 2D window x in [10, 30] and viewport x in [0, 800], what is sx?
  2. If a camera has VRP at (1,2,3) and a world point at (4,6,3), what is the translated point after applying −VRP translation (no rotation)?
  3. For perspective with view plane at z = −2 m and a camera point with z_cam = −4 m and x_cam = 2 m, what is x_proj? Answers: 1) sx = 800/(30−10) = 40. 2) (3,4,0). 3) x_proj = −2 * 2 / (−4) = 1.0 m.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?