I’m having trouble getting my projection matrix to work in Vulkan. I’m using glm 0.9.7.4. (GLM_FORCE_DEPTH_ZERO_TO_ONE is not enabled.)
This is what my scene looks like when using the same projection matrix I’ve used for OpenGL:
Since OpenGL uses a left-handed system and Vulkan uses a right-handed system, this is the expected result.
The view and projection matrices for this scenes are set up like this:
glm::vec3 pos{1203.77f,-127.22f,141.378f};
glm::vec3 forward{0.908188f,-0.185572f,0.375178f};
glm::vec3 up{0.f,1.f,0.f};
auto v = glm::lookAt(pos,pos +forward,up);
auto p = glm::perspective(glm::radians(75.f),1024.f /768.f,1.f,32768.f);
[...]
auto mvp = p *v *m; // MVP is being sent to shader, no additional transformations are done
To get the right orientation, I scaled the projection matrix by -1 on the y-axis (And flipped the face winding order):
glm::vec3 pos{1203.77f,-127.22f,141.378f};
glm::vec3 forward{0.908188f,-0.185572f,0.375178f};
glm::vec3 up{0.f,1.f,0.f};
auto v = glm::lookAt(pos,pos +forward,up);
auto p = glm::perspective(glm::radians(75.f),1024.f /768.f,1.f,32768.f);
p = glm::scale(p,glm::vec3{1.f,-1.f,1.f});
This kind of works, however with an odd side-effect:
With front-face culling nothing is rendered at all.
With back-face culling about half of the objects are rendered:
With no culling enabled, all objects are rendered correctly:
This doesn’t really make any sense to make, and happens regardless of whether I’m using clockwise or counter-clockwise face winding order.
After some searching I’ve stumbled over this page which goes into a bit more detail about the differences between OpenGL and Vulkan. The matrix at the end of the article is supposed to do all of the correct conversions (Including depth), so I gave that a try:
glm::vec3 pos{1203.77f,-127.22f,141.378f};
glm::vec3 forward{0.908188f,-0.185572f,0.375178f};
glm::vec3 up{0.f,1.f,0.f};
auto v = glm::lookAt(pos,pos +forward,up);
auto p = glm::perspective(glm::radians(75.f),1024.f /768.f,1.f,32768.f);
const glm::mat4 clip{
1.f,0.f,0.f,0.f,
0.f,-1.f,0.f,0.f,
0.f,0.f,0.5f,0.5f,
0.f,0.f,0.f,1.f
};
p = clip *p;
Doing that results in the exact same culling issues (Somehow due to the inverted y-component I’m guessing), and incorrect fov:
Why does that affect my fov at all, and is there anything else I need to take into account?