Skip to content

Instantly share code, notes, and snippets.

@fospathi
Last active March 14, 2023 15:21
Show Gist options
  • Select an option

  • Save fospathi/8227bb28e79d27379051cad82364c40c to your computer and use it in GitHub Desktop.

Select an option

Save fospathi/8227bb28e79d27379051cad82364c40c to your computer and use it in GitHub Desktop.

Revisions

  1. fospathi revised this gist Mar 14, 2023. 1 changed file with 18 additions and 14 deletions.
    32 changes: 18 additions & 14 deletions ndc_aspect_distortion.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,16 @@
    (:pushpin: NDC means OpenGL Normalised Device Coordinates.)

    # Aspect distortion of relative vectors composed in NDC space

    The distortion affects relative vectors calculated in NDC space when the NDC to screen space transformation is
    applied. The distortion occurs because a square/NDC cube is being mapped to a screen rectangle.
    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.

    Consider a point P in OpenGL Normalised Device Coordinates (NDC). Let's say we wish to calculate a relative-to-P
    position Q (with images P' and Q' after the screen space transformation) in NDC such that the vector P'Q' maintains
    a constant screen space proportion of screen height. For a square aspect ratio nothing needs to be changed, just
    calculate PQ as normal. For an aspect ratio > 1, i.e. where the screen width is greater than the screen height, PQ
    needs to be scaled in the X-axis direction.
    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'
    | / /
    @@ -19,10 +20,13 @@ needs to be scaled in the X-axis direction.
    P/P'_/_ _ _ _ _ _ _ _ _ _
    |
    For a square aspect ratio Q = Q'.
    For a square aspect ratio, Q = Q'.

    The process that stops the distortion:
    1. Calculate the relative vector PQ as normal as though a square aspect ratio was in place.
    2. Scale PQ in the X-axis direction by `1/(aspect ratio)`.
    3. Add PQ to P to get Q.
    4. The X-axis scaling from step 2 is ultimately undone by the NDC to screen space transformation applied by OpenGL.

    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.
  2. fospathi revised this gist May 27, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ndc_aspect_distortion.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ Consider a point P in OpenGL Normalised Device Coordinates (NDC). Let's say we w
    position Q (with images P' and Q' after the screen space transformation) in NDC such that the vector P'Q' maintains
    a constant screen space proportion of screen height. For a square aspect ratio nothing needs to be changed, just
    calculate PQ as normal. For an aspect ratio > 1, i.e. where the screen width is greater than the screen height, PQ
    needs to be scaled in the X-axis direction so that P'Q' maintains a constant proportion of screen height.
    needs to be scaled in the X-axis direction.

    Q Q'
    | / /
  3. fospathi revised this gist May 27, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ndc_aspect_distortion.md
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,6 @@ needs to be scaled in the X-axis direction so that P'Q' maintains a constant pro

    The process that stops the distortion:
    1. Calculate the relative vector PQ as normal as though a square aspect ratio was in place.
    2. Scale PQ in the X-axis direction by 1/(aspect ratio).
    2. Scale PQ in the X-axis direction by `1/(aspect ratio)`.
    3. Add PQ to P to get Q.
    4. The X scaling from step 2 is ultimately undone by the NDC to screen space transformation applied by OpenGL.
    4. The X-axis scaling from step 2 is ultimately undone by the NDC to screen space transformation applied by OpenGL.
  4. fospathi revised this gist May 27, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ndc_aspect_distortion.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    (:pushpin: NDC means OpenGL Normalised Device Coordinates.)

    #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 space when the NDC to screen space transformation is
    applied. The distortion occurs because a square/NDC cube is being mapped to a screen rectangle.
  5. fospathi created this gist May 27, 2020.
    28 changes: 28 additions & 0 deletions ndc_aspect_distortion.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    (:pushpin: NDC means OpenGL Normalised Device Coordinates.)

    #Aspect distortion of relative vectors composed in NDC space

    The distortion affects relative vectors calculated in NDC space when the NDC to screen space transformation is
    applied. The distortion occurs because a square/NDC cube is being mapped to a screen rectangle.

    Consider a point P in OpenGL Normalised Device Coordinates (NDC). Let's say we wish to calculate a relative-to-P
    position Q (with images P' and Q' after the screen space transformation) in NDC such that the vector P'Q' maintains
    a constant screen space proportion of screen height. For a square aspect ratio nothing needs to be changed, just
    calculate PQ as normal. For an aspect ratio > 1, i.e. where the screen width is greater than the screen height, PQ
    needs to be scaled in the X-axis direction so that P'Q' maintains a constant proportion of screen height.

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

    The process that stops the distortion:
    1. Calculate the relative vector PQ as normal as though a square aspect ratio was in place.
    2. Scale PQ in the X-axis direction by 1/(aspect ratio).
    3. Add PQ to P to get Q.
    4. The X scaling from step 2 is ultimately undone by the NDC to screen space transformation applied by OpenGL.