Hacker News front page5 min readintermediate
Divide by depth for instant 3D
Summary
A concise walkthrough of the depth‑division trick for 3D projection, how it maps to the full perspective projection matrix, and GLSL snippets showing both the minimal trick and a proper matrix‑based camera.
- Dividing x and y by z (the “depth division” trick) is just a special case of a perspective projection matrix with focal length = 1 and aspect = 1.
- A full perspective matrix adds focal scale (derived from vertical FOV) and aspect ratio, plus depth mapping using near/far planes.
- World → view → clip → NDC → screen is the standard pipeline; the matrix handles the world‑to‑view transform and the perspective divide.
- GLSL code shows how to build the matrix manually and how to apply it, useful when you need a custom or lightweight camera implementation.
Most game and graphics frameworks hide the math behind a Camera object. Knowing the underlying matrix lets you implement lightweight shaders, debug projection issues, or build non‑standard view setups without pulling in a full engine.
6/10


