Skip to content

Instantly share code, notes, and snippets.

@rishabh-battulwar
Last active April 29, 2024 04:04
Show Gist options
  • Save rishabh-battulwar/fe8e237c779ed3b2c186bfbd41cee60e to your computer and use it in GitHub Desktop.
Save rishabh-battulwar/fe8e237c779ed3b2c186bfbd41cee60e to your computer and use it in GitHub Desktop.
Math and 3D Graphics

Metal

NDC
Point(-1, -1) in NDC is located at the the bottom left corner (Y up)

Viewport coordinate For writing into attachment or reading from attachment or copy/blit between attachments, we use framebuffer coordiante to specify the location.
The origin(0, 0) is located at the top-left corner (Y down).

Texture Coordinate
When we upload texture into memory or sample from texture, we use texture coordinate. The origin(0, 0) is located at the top-left corner (Y down).

D3D12 and Metal

NDC: +Y is up. Point(-1, -1) is at the bottom left corner. Framebuffer coordinate: +Y is down. Origin(0, 0) is at the top left corner. Texture coordinate: +Y is down. Origin(0, 0) is at the top left corner.

OpenGL, OpenGL ES and WebGL

NDC: +Y is up. Point(-1, -1) is at the bottom left corner. Framebuffer coordinate: +Y is up. Origin(0, 0) is at the bottom left corner. Texture coordinate: +Y is up. Origin(0, 0) is at the bottom left corner.

https://amytabb.com/ts/2019_06_28_images/opengl_transformations.png

USD

Maps the lower left-hand corner of the image as the (0, 0) uv coordinate. More specifically, for the four vertices of a quadrilateral, in order, the st coordinates should be [ (0, 0), (1, 0), (1, 1), (0, 1) ] .

This mapping is shared by MaterialX and MDL, though unfortunately not glTf, so when converting between glTf and USD, texture coordinates must be "t flipped" (i.e. tFinal = 1.0 - t) before being consumed by the texture reader.

As an implementation detail, because most image/texture reading packages (including OpenImageIO) consider the upper-left corner of an image to be uv (0, 0), the image-reader abstraction in USD that serves as an interface to OIIO and other image readers flips the layout of the image bottom-to-top before making it available to shading consumers

OpenGL

OpenGL is right handed in object space and world space.

But in window space (aka screen space) we are suddenly left handed.

Vulkan

NDC: +Y is down. Point(-1, -1) is at the top left corner. See the description about “PointCoord” in Vulkan 1.1 spec. Framebuffer coordinate: +Y is down. Origin(0, 0) is at the top left corner. See the description about “VkViewport” and “FragCoord” in Vulkan 1.1 spec. But we can flip the viewport coordinate via a negative viewport height value. Texture coordinate: +Y is down. Origin(0, 0) is at the top left corner.

Links

https://stackoverflow.com/questions/58250404/metal-and-coordinate-system-mapping https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html#UsdPreviewSurfaceProposal-TextureCoordinateOrientationinUSD https://stackoverflow.com/questions/4124041/is-opengl-coordinate-system-left-handed-or-right-handed https://fruty.io/2019/08/29/augmented-reality-with-opencv-and-opengl-the-tricky-projection-matrix/ https://amytabb.com/ts/2019_06_28/ https://medium.com/comerge/what-are-the-coordinates-225f1ec0dd78 https://miro.medium.com/max/2000/1*NmzBmBk5SMUULxk8hcOsdw.png

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