0
votes

So I met some strange things when I deal with vulkan coordinate system. I'm used to the opengl coordinate system, so when I create the viewport in vulkan, I did

VkViewport viewport = {};
viewport.width = 800;
viewport.height = -600;
viewport.x = 0.0f;
viewport.y = 600;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;

By that, I flip the y axis, so I assume positive X-axis is to right of the screen, positive Y-axis is to the up of the screen, and positive Z-axis is pointing into the screen. Am I correct? But then when I drew a simple square, using four vertices,

(150, 50, 0.0), (50.0, 50, 0.0), (50.0, -50, 0.0), (150.0, -50, 0.0)

with model transformation matrix as identity, and view matrix as

glm::lookAt(glm::vec3(0, 0, -500), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));

and projection matrix

glm::perspective(glm::radians(45.0f), m_swapchainExtent.width / (float)m_swapchainExtent.height, 0.1f, 1000000.0f);

I got a square that is actually on the left side of the image. How to explain this, or just the positive X-axis actually pointing to the left?

1

1 Answers

2
votes

This link discusses the differences between the Vulkan and OpenGL coordinate systems. In summary, the Z range for Vulkan is in the range [0, 1] rather than [-1, 1], and the Y axis is flipped, but the X axis is the same.

If you're going to attempt to use a negative viewport range, you must make sure you've either requested Vulkan 1.1 or higher, or are requiring the KHR_VK_maintainance1 extension, otherwise specifying a negative viewport size is invalid.

I would suggest you start off with no matrices at all, and describe your cube in normalized device coordinates, i.e. in the range of -1 to 1. So you could make your square vertex coordinates something like

{ 0.25, 0.25, 0.0 }
{ 0.75, 0.25, 0.0 }
{ 0.75, -0.25, 0.0 }
{ 0.25, -0.25, 0.0 }

Render that (although it won't be a square unless your window aspect ratio is 1.0) and see if it ends up where you expect (on the right, in the middle vertically). If it does, then your problem lies with one of your matrix transformations, not with the Vulkan API.