Fix stretching/squashing with non-square window

This commit is contained in:
Epicalert 2021-03-17 01:01:21 +08:00
parent f2d92979d0
commit 3220795821
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD
4 changed files with 7 additions and 2 deletions

View file

@ -1,8 +1,8 @@
# Before 1.0 # Before 1.0
## graphics ## graphics
- prevent stretching from window aspect ratio
- smoothing - smoothing
- framerate counter
## docs ## docs
- model format documentation - model format documentation

View file

@ -24,11 +24,14 @@
GLuint shader; //standard shader program used for all elements GLuint shader; //standard shader program used for all elements
GLuint transUniform; //location of the "transMatrix" transformation matrix uniform in the shader GLuint transUniform; //location of the "transMatrix" transformation matrix uniform in the shader
float windowAspectRatio;
//parts of the model (see modelpart.hpp) //parts of the model (see modelpart.hpp)
Model* model; Model* model;
void display () { void display () {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
windowAspectRatio = glutGet(GLUT_WINDOW_WIDTH) / (float)glutGet(GLUT_WINDOW_HEIGHT);
model->draw(); model->draw();

View file

@ -12,6 +12,7 @@
#include <cv.hpp> #include <cv.hpp>
extern GLuint transUniform; extern GLuint transUniform;
extern float windowAspectRatio;
void initGraphics (); void initGraphics ();

View file

@ -41,7 +41,8 @@ void ModelPart::setFollowTarget(std::string followTarget) {
} }
void ModelPart::setTransform(glm::vec2 position, float rotation, float scale) { 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::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::scale(transMatrix, glm::vec3(1,1,1) + (scale - 1 + glm::vec3(scaleOffset, 0.0f)) * scaleFactor);
transMatrix = glm::translate(transMatrix, glm::vec3(-origin, 0.0f)); transMatrix = glm::translate(transMatrix, glm::vec3(-origin, 0.0f));