Skip to content

Instantly share code, notes, and snippets.

@maxymania
Last active July 13, 2025 18:19
Show Gist options
  • Save maxymania/f4245f6f9af2c61c28dab29c22267d02 to your computer and use it in GitHub Desktop.
Save maxymania/f4245f6f9af2c61c28dab29c22267d02 to your computer and use it in GitHub Desktop.
Vulkan Projection Matrix from OpenGL Projection Matrix (C++ with glm)
// Public domain.
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
/*
Adjustment matrix, that transforms coordinates from OpenGL NDC to Vulkan NDC.
*/
inline glm::mat4 gl2vk_matrix()
{
/*
( 1 0 0 0 )
( 0 -1 0 0 )
( 0 0 .5 .5 )
( 0 0 0 1 )
To create a Vulkan projection matrix Q from an OpenGL projection matrix P
compute Q = P * A
where A is the matrix above!
*/
glm::mat4 identity(1);
identity[1][1] = -1;
identity[2][2] = 0.5;
identity[3][2] = 0.5;
return identity;
}
inline glm::mat4 infinitePerspectiveVK(float aspect)
{
return gl2vk_matrix() * glm::infinitePerspective(glm::radians(90.0f),aspect,.1f);
}
inline glm::mat4 perspectiveVK(float aspect)
{
return gl2vk_matrix() * glm::perspective(glm::radians(90.0f),aspect,.1f, 1000.f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment