PDA

View Full Version : Do I need to compensate for my screen aspect ratio?



curtainsbacked
06-01-2017, 07:20 PM
I am trying to make an stl viewer. I managed to get the vertex to display and some level of shading. But when I display a cube, It looks squashed (width >Height), I suspect it's the projection matrix. But I can't figure out what is my mistake. I am using Trimesh to import my stl and manipulate it.



//finds bounding sphere
m->need_bsphere();

float diameter = m->bsphere.r*2;
float xcenter = m->bsphere.center[0];
float ycenter = m->bsphere.center[1];
float zcenter = m->bsphere.center[2];

float zNear = 1.0;
float zFar = zNear + 5*diameter;

float left = xcenter-diameter;
float right = xcenter+diameter;
float bot = ycenter-diameter;
float top = ycenter + diameter;

//Compute Orthogonal projection matrix
Projection = glm::ortho(left,right,bot,top,zNear,zFar);
//Compute camera position matrix
View = glm::lookAt(glm::vec3(xcenter,ycenter,2*diameter), glm::vec3(xcenter,ycenter,zcenter), glm::vec3(0.0f,1.0f,0.0f));

//Model set at origin
Model = glm::mat4(1.0f);

mvp = Projection * View * Model;





#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;

uniform mat4 MVP;

void main(){

gl_Position = MVP*vec4(vertexPosition_modelspace,1);
}

GClements
06-01-2017, 07:39 PM
I am trying to make an stl viewer. I managed to get the vertex to display and some level of shading. But when I display a cube, It looks squashed (width >Height), I suspect it's the projection matrix.
Yup. The ratio of right-left to bot-top should match the aspect ratio of the window. Ideally, it should match the physical aspect ratio (i.e. the ratio of physical width/height in millimetres), but using the ratio of sizes in pixels is usually close enough.

If you want to fit a given rectangle to the window while preserving the aspect ratio, you have a number of options, including:


Fit the width exactly, calculate the height based upon width and aspect ratio.
Fit the height exactly, calculate the width based upon height and aspect ratio.
Calculate the ratio of the areas, calculate the width and height based upon scale factor and aspect ratio.
Calculate the scale factor which fits the width exactly, calculate the scale factor which fits the height exactly, choose the smaller scale factor, calculate the width and height based upon scale factor and aspect ratio.
As above, but choose the larger scale factor.

curtainsbacked
06-01-2017, 09:23 PM
Yup. The ratio of right-left to bot-top should match the aspect ratio of the window. Ideally, it should match the physical aspect ratio (i.e. the ratio of physical width/height in millimetres), but using the ratio of sizes in pixels is usually close enough.

If you want to fit a given rectangle to the window while preserving the aspect ratio, you have a number of options, including:


Fit the width exactly, calculate the height based upon width and aspect ratio.
Fit the height exactly, calculate the width based upon height and aspect ratio.
Calculate the ratio of the areas, calculate the width and height based upon scale factor and aspect ratio.
Calculate the scale factor which fits the width exactly, calculate the scale factor which fits the height exactly, choose the smaller scale factor, calculate the width and height based upon scale factor and aspect ratio.
As above, but choose the larger scale factor.


Hi thanks for the response.

I added the following code:



float aspect = 16/9;
//Compute Orthogonal projection matrix
Projection = glm::ortho(left*aspect,right*aspect,bot,top,zNear, zFar);


For simplicity, the openGl screen size is to 300*16/9:300. Which gives me an aspect ratio of 16:9. Since my width/height start off as 1, I scale the left-right value so that the ratio becomes 16:9. But unfortunately, my cube still stays squashed. Did I do this correctly? how can I make the cube side equal again? Do I have to multiply by a scaling matrix again or is it already taken care for by the projection matrix constructor (glm:: ortho) ?