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.”
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
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 negativeOneToOne!=VK_TRUE, we get
from which we get negativeOneToOne=VK_TRUE, we get depthCompareOp in
VkPipelineDepthStencilStateCreateInfo.
Assume minDepth=1 and maxDepth=0, then depthCompareOp=VK_COMPARE_OP_LESS, vertices with smaller depthCompareOp=VK_COMPARE_OP_GREATER, away.
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 transform points from clip coordinates (
Because every element of
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.
To project (
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.
Expand
To transform from clip coordinates to NDC, both
To get the third row, we first observe that all points on a plane perpendicular to the near plane
should have the same
Because
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 negativeOneToOne!=VK_TRUE:
- If
minDepth=0andmaxDepth=1, we get , smaller (closer to the near plane) is mapped to smaller , so we should setdepthCompareDepth=VK_COMPARE_OP_LESS. - If
minDepth=1andmaxDepth=0, we get , smaller (closer to the near plane) is mapped to greater , so we should setdepthCompareDepth=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
Note, since z is reversed in NDC, vertices with greater minDepth=0 and maxDepth=1, 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
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
- 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:
- List of Vulkan depth related topics
- Visualizing Depth Precision - Nathan Reed
- OpenGL Projection Matrix - Song Ho
- The new Vulkan Coordinate System - Matthew Wellings




