Coordinates

I see many posts online discussing whether Vulkan uses right-handed or left-handed coordinates system. But the truth is no where in the Vulkan specification are the words “right hand” or “right-hand” found. Vulkan simply doesn't define a right-handed or left-handed coordinates. Instead, it defines a series of transformations that convert points in clip coordinates to normalized device coordinates (NDC), then to the final framebuffer coordinates which is defined as: “a coordinate system in which adjacent pixels’ coordinates differ by 1 in x and/or y, with (0,0) in the upper left corner and pixel centers at half-integers.”

vulkan framebuffer coordinates

Normalized device coordinates is defined as: “a coordinate space after perspective division is applied to clip coordinates, and before the viewport transformation converts them to framebuffer coordinates.” Below is from Vulkan specification 33.9. Controlling the Viewport on how to convert from NDC to framebuffer coordinates.

The viewport transformation is determined by the selected viewport’s width and height in pixels, and , respectively, and its center (, ) (also in pixels), as well as its depth range min and max determining a depth range scale value and a depth range bias value (defined below). The vertex’s framebuffer coordinates (, ) and depth are given by

Note that no “left handed/right handed” is mentioned. The spec only gives a set of equations.

Because and , we can conclude and . Note, is positive, so goes in the same direction as , downwords.

normalized device coordinates

What about z axis? VkViewport gives more details on that, particularly, on the depth range:

where negativeOneToOne is defined inside the struct VkPipelineViewportDepthClipControlCreateInfoEXT.

Also mentioned are the following two notes:

Despite their names, minDepth can be less than, equal to, or greater than maxDepth.

If the VK_EXT_depth_range_unrestricted extension is not enabled, minDepth must be between 0.0 and 1.0, inclusive

Expand and when negativeOneToOne!=VK_TRUE, we get

from which we get . Similarly, if we expand and when negativeOneToOne=VK_TRUE, we get . But which direction does +z axis points to, towards the viewer or away from the viewer? To know that we need to decide another setting depthCompareOp in VkPipelineDepthStencilStateCreateInfo. Assume minDepth=1 and maxDepth=0, then is mapped to . If depthCompareOp=VK_COMPARE_OP_LESS, vertices with smaller value are in front, therefore +z axis should point towards the viewer, if depthCompareOp=VK_COMPARE_OP_GREATER, away.

NDC

Other combinations of minDepth, maxDepth and depthCompareOp can change +z axis's direction. Because we get to choose the values of minDepth, maxDepth and depthCompareOp, the direction of +z axis is largely irrelevant as long as we correctly transform to .

To transform points from clip coordinates () to NDC (), Vulkan spec 33.7 Coordinate Transformations says:

Because every element of , , is divided by the same , we cannot change the direction of +z axis without changing the direction of the other two axes, so clip coordinates must have the same “handed-ness” as NDC.

Perspective Projection Matrix

Now the task is to construct a perspective matrix that transforms points in eye/camera coordinates to clip coordinates so that after the perspective division the points in NDC have the ranges: .

Assume we have a left-handed coordinates (different from clip coordinates) for eye/camera space, and the eye space's +z axis goes through the center of the near plane.

perspective frustum side and top views

are half-width and half-height of the viewport, and are the z values of the near and far planes.

To project () onto the near plane, we can use ratio of similar triangles to get

A projected point's x coordinate need to be mapped to [−1, 1] in NDC. Because in our case +y axis points upwrds in eye coordinates (opposite of +y axis in NDC), its y coordinate in eye space need to be mapped to [1, −1] in NDC.

scaling and to −1,+1

Expand and in terms of and , we get:

To transform from clip coordinates to NDC, both and need to be divided by the same . Base on this, we can deduce and , . We now can construct the perspective projection matrix partially.

To get the third row, we first observe that all points on a plane perpendicular to the near plane should have the same , therefore is independent of and . Now we have:

Because is mapped to , we get:

Finally we have our perspective projection matrix:

Now we need to carefully choose values for minDepth, maxDepth and depthCompareOp to make depth test work correctly. Remember, Vulkan defines the following equation to map to when negativeOneToOne!=VK_TRUE:

  • If minDepth=0 and maxDepth=1, we get , smaller (closer to the near plane) is mapped to smaller , so we should set depthCompareDepth=VK_COMPARE_OP_LESS.
  • If minDepth=1 and maxDepth=0, we get , smaller (closer to the near plane) is mapped to greater , so we should set depthCompareDepth=VK_COMPARE_OP_GREATER.

The point of this exercise is to show you that no matter in which “handed-ness” of coordinate system is your 3D scene, you can construct a perspective matrix that transforms it to the final framebuffer coordinates required by Vulkan (with the correct minDepth, maxDepth and depthCompareOp).

Reverse Z and Infinite Far Plane

The befinits of using reversed z for depth and infinity for far plane can be found many places online, the following is just for my own memory.

Instead of mapping to , we map it to .

Note, since z is reversed in NDC, vertices with greater values are closer to the near plane. If we set minDepth=0 and maxDepth=1, is mapped to , we should set depthCompareOp=VK_COMPARE_OP_GREATER.

Winding

According to Vulkan spec Rasterization Polygons,

The first step of polygon rasterization is to determine whether the triangle is back-facing or front-facing. This determination is made based on the sign of the (clipped or unclipped) polygon’s area computed in framebuffer coordinates.

Simplified formula to calculate the signed area:

where and are the x,y coordinates of the ith vertex of a triangle. If we take two triangle edges and , and as z component of the result of cross product of and , we get .

The cross product of +x axis and +y axis is +z axis. In the framebuffer coordinates, because the +y axis points downwards, the winding from +x axis to +y axis is clockwise, therefore triangles with clockwise winding will have positive area. However, notice that there is a minus sign in , which inverses the previous statement and we get triangles with counter-clockwise winding have positive area. This is why VkFrontFace says

  • VK_FRONT_FACE_COUNTER_CLOCKWISE specifies that a triangle with positive area is considered front-facing.
  • VK_FRONT_FACE_CLOCKWISE specifies that a triangle with negative area is considered front-facing.

References: