Skip to content

Instantly share code, notes, and snippets.

@christophercrouzet
Created August 16, 2014 03:03
Show Gist options
  • Save christophercrouzet/1fea72d0d88e5a0dca16 to your computer and use it in GitHub Desktop.
Save christophercrouzet/1fea72d0d88e5a0dca16 to your computer and use it in GitHub Desktop.
OpenGL projection matrix to fit the content to the window while preserving the aspect ratio
int shorterSide = std::min(width, height);
GLfloat marginX = 1.0f + GLfloat(width - shorterSide) / (GLfloat)shorterSide;
GLfloat marginY = 1.0f + GLfloat(height - shorterSide) / (GLfloat)shorterSide;
glm::mat4 projection = glm::ortho(-marginX, marginX, -marginY, marginY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment