Skip to content

Instantly share code, notes, and snippets.

@fospathi
Last active March 14, 2023 15:21
Show Gist options
  • Save fospathi/8227bb28e79d27379051cad82364c40c to your computer and use it in GitHub Desktop.
Save fospathi/8227bb28e79d27379051cad82364c40c to your computer and use it in GitHub Desktop.
Aspect distortion of relative vectors composed in NDC space

Aspect distortion of relative vectors composed in NDC space

The distortion affects relative vectors calculated in NDC (OpenGL Normalised Device Coordinates) space at the point at which they are mapped to a rectangular (i.e. non-square) screen.

Let the metric used to measure distance be the proportion of the height of the (NDC cube)/screen. Let's say we wish to calculate a position Q in NDC which is a desired distance from P on the screen.

For a square screen, nothing needs to be done, just calculate PQ as normal. For a screen aspect ratio ≠ 1, e.g. when the screen width is greater than the screen height, the relative vector needs to be scaled in the X-axis direction.

                        Q          Q'
                  |    /        /
                  |   /      /
                  |  /    /
                  | /  /
              P/P'_/_ _ _ _ _ _ _ _ _ _ 
                  |
            
            For a square aspect ratio, Q = Q'.

The process that stops the distortion:

  1. calculate the relative vector PQ' with the desired length;
  2. scale PQ' in the X-axis direction by 1/(aspect ratio) giving PQ;
  3. add PQ to P to get Q.

The X-axis scaling from step 2 is ultimately undone by the NDC to screen space transformation applied by OpenGL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment