curtainsbacked

06-01-2017, 06: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);

}

//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);

}