From 3220795821e393db89a1a263a670d50f2b4583f4 Mon Sep 17 00:00:00 2001 From: Epicalert Date: Wed, 17 Mar 2021 01:01:21 +0800 Subject: [PATCH] Fix stretching/squashing with non-square window --- TODO.md | 2 +- src/graphics.cpp | 3 +++ src/graphics.hpp | 1 + src/modelpart.cpp | 3 ++- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 58881fa..26cb262 100644 --- a/TODO.md +++ b/TODO.md @@ -1,8 +1,8 @@ # Before 1.0 ## graphics -- prevent stretching from window aspect ratio - smoothing +- framerate counter ## docs - model format documentation diff --git a/src/graphics.cpp b/src/graphics.cpp index 6730c6e..baba755 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -24,11 +24,14 @@ GLuint shader; //standard shader program used for all elements GLuint transUniform; //location of the "transMatrix" transformation matrix uniform in the shader +float windowAspectRatio; + //parts of the model (see modelpart.hpp) Model* model; void display () { glClear(GL_COLOR_BUFFER_BIT); + windowAspectRatio = glutGet(GLUT_WINDOW_WIDTH) / (float)glutGet(GLUT_WINDOW_HEIGHT); model->draw(); diff --git a/src/graphics.hpp b/src/graphics.hpp index 549e11b..875a95b 100644 --- a/src/graphics.hpp +++ b/src/graphics.hpp @@ -12,6 +12,7 @@ #include extern GLuint transUniform; +extern float windowAspectRatio; void initGraphics (); diff --git a/src/modelpart.cpp b/src/modelpart.cpp index d46db46..d4343fe 100644 --- a/src/modelpart.cpp +++ b/src/modelpart.cpp @@ -41,7 +41,8 @@ void ModelPart::setFollowTarget(std::string followTarget) { } void ModelPart::setTransform(glm::vec2 position, float rotation, float scale) { - transMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(position.x, -position.y, 0.0f) + glm::vec3(posOffset, 0.0f)); + transMatrix = glm::ortho(-windowAspectRatio, windowAspectRatio, -1.0f, 1.0f); + transMatrix = glm::translate(transMatrix, glm::vec3(position.x, -position.y, 0.0f) + glm::vec3(posOffset, 0.0f)); transMatrix = glm::rotate(transMatrix, rotation * rotFactor, glm::vec3(0.0f, 0.0f, 1.0f)); transMatrix = glm::scale(transMatrix, glm::vec3(1,1,1) + (scale - 1 + glm::vec3(scaleOffset, 0.0f)) * scaleFactor); transMatrix = glm::translate(transMatrix, glm::vec3(-origin, 0.0f));