Search code examples
graphicscoordinate-systemsvulkan

Vulkan right handed coordinate system become Left handed


Problem:

Vulkan right handed coordinate system became left handed coordinate system after applying projection matrix. How can I make it consistent with Vulkan coordinate system?

Details:

I know that Vulkan is right handed coordinate system where

  • X+ points toward right
  • Y+ points toward down
  • Z+ points toward inside the screen

I've this line in the vertex shader: https://github.com/AndreaCatania/HelloVulkan/blob/master/shaders/shader.vert#L23

gl_Position = scene.cameraProjection * scene.cameraView * meshUBO.model * vec4(vertexPosition, 1.0);

At this point: https://github.com/AndreaCatania/HelloVulkan/blob/master/main.cpp#L62-L68 I'm defining the position of camera at center of scene and the position of box at (4, 4, -10) World space

The result is this:

enter image description here

As you can see in the picture above I'm getting Z- that point inside the screen but it should be positive.

Is it expected and I need to add something more or I did something wrong?

Useful part of code:

Projection calculation: https://github.com/AndreaCatania/HelloVulkan/blob/master/VisualServer.cpp#L88-L98

void Camera::reloadProjection(){
    projection = glm::perspectiveRH_ZO(FOV, aspect, near, far);
    isProjectionDirty = false;
}

Camera UBO fill: https://github.com/AndreaCatania/HelloVulkan/blob/master/VisualServer.cpp#L403-L414

    SceneUniformBufferObject sceneUBO = {};
    sceneUBO.cameraView = camera.transform;
    sceneUBO.cameraProjection = camera.getProjection();

Solution

  • I do not use or know Vulcan but perspective projection matrix (at lest in OpenGL) is looking in the Z- direction which inverts one axis of your coordinate system. That inverts the winding rule of the coordinate system.

    If you want to preserve original winding than just invert Z axis vector in the matrix for more info see:

    So just scale the Z axis by -1 either by some analogy to glScale(1.0,1.0,-1.0); or by direct matrix cells access.