perspective projection opengl screen coordinates

Perspective Projection in OpenGL: Mapping 3D to Screen Coordinates Explained

By Joshua Taylor
AD

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.

AD
OpenGL Projection Matrix
OpenGL Projection Matrix
Perspective Projection Explained
Perspective Projection Explained
a drawing of a room with squares and lines
a drawing of a room with squares and lines

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.

5-five-vanishing-points
5-five-vanishing-points
two different views of a building with blue and green lines
two different views of a building with blue and green lines
8.3 - Perspective Projections β€” LearnWebGL
8.3 - Perspective Projections β€” LearnWebGL

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.

AD

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.

Make pixel art online - Pixilart
Make pixel art online - Pixilart
an image of a room with light coming from the ceiling and some red lines on the floor
an image of a room with light coming from the ceiling and some red lines on the floor
πŸ’ΎTΓ”N HOΓ€NG ANH NHΓƒπŸ’Ή πŸ‘‘:🧐FBIπŸ“ΈπŸͺπŸŒNASAπŸŽ’πŸ”πŸš₯πŸ“¬πŸ“‹πŸ—ƒοΈπŸ”―β˜ΈοΈπŸ–¨πŸ“šπŸŒŽ β„οΈπŸ›οΈπŸŒ†πŸœπŸ«†πŸ“‡πŸŒŸπŸ“œβ˜’οΈ β›©οΈπŸ–ΌοΈπŸ”=}πŸ“–πŸ•ŒπŸ†˜.
πŸ’ΎTΓ”N HOΓ€NG ANH NHΓƒπŸ’Ή πŸ‘‘:🧐FBIπŸ“ΈπŸͺπŸŒNASAπŸŽ’πŸ”πŸš₯πŸ“¬πŸ“‹πŸ—ƒοΈπŸ”―β˜ΈοΈπŸ–¨πŸ“šπŸŒŽ β„οΈπŸ›οΈπŸŒ†πŸœπŸ«†πŸ“‡πŸŒŸπŸ“œβ˜’οΈ β›©οΈπŸ–ΌοΈπŸ”=}πŸ“–πŸ•ŒπŸ†˜.
An intro to modern OpenGL. Chapter 3: 3D transformation and projection
An intro to modern OpenGL. Chapter 3: 3D transformation and projection
Perspective Grids
Perspective Grids
a blueprint drawing of a man standing in front of a mirror
a blueprint drawing of a man standing in front of a mirror

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.

AD

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.

Parallel Projection and Perspective Drawings – poradora
Parallel Projection and Perspective Drawings – poradora
Perspective grid background empty futuristic digital box room | Premium Vector
Perspective grid background empty futuristic digital box room | Premium Vector
an image of two different lines with the words, line angle and intersection in them
an image of two different lines with the words, line angle and intersection in them
an empty room with grids on the wall and floor in perspective, as well as lines
an empty room with grids on the wall and floor in perspective, as well as lines

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.

AD

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.

AD