Perspective Projection in OpenGL: Mapping 3D to Screen Coordinates Explained
When you render a 3D scene with OpenGL, the vertices you submit do not land directly on pixels. They travel through a chain of coordinate spaces, and perspective projection OpenGL screen coordinates are the final result of that journey. The projection matrix creates the illusion of depth, but the conversion to actual window coordinates depends on clipping, the perspective divide, the viewport transform, and the depth range. If you have ever tried to align a mouse click with a 3D object, place a 2D label, or build a ray from the camera, you have already brushed against this pipeline.
This article breaks down how OpenGL maps a point from clip space to normalized device coordinates and then to screen coordinates. It also covers practical formulas, common coordinate conventions, and the pitfalls that cause off-by-one errors or upside-down input. The goal is to make the transformation predictable enough that you can debug it with a few print statements and a simple triangle.
The OpenGL Coordinate Transformation Pipeline
A vertex typically starts in object space, where its coordinates are defined relative to a model. The model matrix places it in world space, the view matrix moves it into camera space, and the projection matrix pushes it into clip space. In a shader, the usual expression is a chain of matrix multiplications: clip position equals projection times view times model times object position. With perspective projection, the resulting clip position has a w component that is not equal to one.
Clipping happens in clip space before the perspective divide. Primitives are tested against a viewing volume defined by the projection matrix. In OpenGL, a point is inside the frustum when its x, y, and z clip coordinates are between negative w and positive w. After clipping, the GPU performs the perspective divide, which turns clip coordinates into normalized device coordinates, or NDC.
From Object Space to Clip Space
The perspective projection matrix is responsible for the narrowing shape of the view frustum. It encodes the field of view, the aspect ratio, and the near and far planes. In a right-handed OpenGL camera, the view direction is usually negative Z, and the projection matrix flips that into a clip space w that is positive for visible points. That w value is what enables the perspective divide to shrink distant geometry.
For example, a call such as GLM perspective with a 45 degree field of view, a 16 by 9 aspect ratio, and near and far planes of 0.1 and 100 produces a standard perspective matrix. If the aspect ratio is wrong, circles become ovals and screen coordinates drift away from what you expect. The projection matrix does not know about your window size; it only defines the shape of the camera frustum.
The Perspective Divide and Normalized Device Coordinates
Once clipping is complete, the GPU divides the clip space x, y, and z values by the clip space w value. This is the perspective divide. The result is NDC, a cube where x and y range from minus one to one, and z also ranges from minus one to one in standard OpenGL. NDC is the last coordinate space before the hardware applies the viewport transform.
Points with a w value at or below zero are behind the camera or at a singularity, so they must be clipped or handled carefully. The divide also causes depth precision to become nonlinear, which is why near planes that are too close can create z-fighting. NDC is convenient for math, but it is not yet a pixel coordinate. That final step belongs to the viewport and depth range.
Mapping NDC to OpenGL Screen Coordinates
OpenGL window coordinates, often called screen coordinates, are measured in pixels. By default, the origin is the lower-left corner of the viewport, x increases to the right, and y increases upward. This differs from many windowing systems and image formats that place the origin at the top-left. The viewport transform maps the NDC cube into this pixel rectangle.
The standard mapping uses the viewport width and height along with an offset. For x, the formula is x window equals x NDC plus one, multiplied by half the viewport width, plus the viewport x offset. For y, the same pattern applies with the viewport height and y offset. For z, the mapping depends on the depth range, which defaults to zero through one.
The Viewport Transform and glViewport
The function glViewport sets the pixel rectangle that receives the NDC cube. Its parameters are x, y, width, and height. The x and y values define the lower-left corner of the viewport in window coordinates. If you render to a framebuffer with a different size, you must set the viewport to match, or the projection will stretch incorrectly.
Consider a window that is 1920 pixels wide and 1080 pixels tall, with a viewport at zero, zero. NDC point zero, zero maps to pixel 960, 540. NDC point minus one, minus one maps to pixel 0, 0. NDC point one, one maps to pixel 1920, 1080. If the viewport starts at 100, 50 and is 800 by 600, the same NDC points shift accordingly. This offset is a common source of bugs when UI and 3D rendering share the same window.
Depth Range, Window Y, and Practical Conversions
Depth is also transformed. With the default depth range, NDC z of minus one maps to window z of zero, and NDC z of one maps to window z of one. The depth buffer stores these window z values, and depth testing compares them. If you call glDepthRange with different values, the mapping changes, but the principle remains linear in window z.
Mouse input usually arrives with a top-left origin, while OpenGL window coordinates use a bottom-left origin. To convert, subtract the mouse y from the window height and then subtract one if your input system uses inclusive pixel rows. For a viewport with an offset, first convert the mouse position into viewport-relative coordinates, then apply the NDC formula in reverse. This same reverse mapping is the basis of unproject, which takes a screen point and a depth value and reconstructs a world-space ray.
The next time a pick ray misses its target or an overlay sits a few pixels too high, trace the numbers from clip space to NDC to window space. Print the viewport, the projection matrix, and the mouse coordinates, then verify each conversion step. That habit turns a confusing pipeline into a reliable tool you can reuse across renderers, editors, and interactive simulations.