2

I'm trying to set up an orthographic view matrix using glm and pass it to glsl. this poster has the same problem, I want to be able to render pixels drawn further away from the camera but they are being clipped when they are anything but 1.0 distance.

The MVP matrix is set up like so:

// Setup MVP Matrix
glm::mat4 model = glm::mat4(1.0);

model = glm::translate(model, glm::vec3(sprite->getPositionX(), sprite->getPositionY(), 0.0f));
model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-sprite->getOffsetX(), -sprite->getOffsetY(), 0.0f));
//model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // Unused for now

glm::mat4 view = glm::lookAt(glm::vec3(0,0,0), glm::vec3(0,0,-1), glm::vec3(0,1,0));
glm::mat4 projection = glm::ortho(0.0f, 800.0f, 600.0f, 0.0f, 1.0f, 1000.0f);

glm::mat4 mvp = projection * view * model;

// Set mpv matrix in shader
GLint mvpLoc = glGetUniformLocation(this->m_shader, "u_mvpMatrix");

glProgramUniformMatrix4fv(this->m_shader, mvpLoc, 1, GL_FALSE, &mvp[0][0]);

I can see the quad being rendered with the translation and rotation applied but when the verticies are anywhere but -1.0f on the z axis they are not drawn when I expect it to draw anything from -1.0 to -1000.0 units away as specified in the call to glm::ortho. Anybody know how this can be achieved?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Obi-Dan
  • 113
  • 1
  • 8
  • For one thing: you are applying scaling, rotation and translation in the wrong order. GLM is column-major and performs post-multiplication. The correct order should be translate, rotate, scale. Now, since the values used for the scale operation effectively produce a no-op and the translate does as well, it hardly matters - but you should be aware of the correct order to do these things anyway. – Andon M. Coleman Mar 09 '14 at 23:10
  • I have changed those to the right order after I started rotating the quad I realised the mistake there but changing the z-axis still gives me problems, I've updated the new code which also translates again after rotating to rotate the quad around the center. What do you mean by no-op? – Obi-Dan Mar 09 '14 at 23:24
  • By no-op, I mean that translating **0.0** units and scaling by a factor of **1.0** in all directions is effectively the same thing as multiplying by an identity matrix. Thus, no real operation occurs (no-op). – Andon M. Coleman Mar 09 '14 at 23:30
  • Oh I see, it was just there ready for when I use them, will leave it out until it comes of use. – Obi-Dan Mar 09 '14 at 23:33
  • I have it working but I'm unsure what fixed it, the only thing I can think of that I did differently was use floats instead of ints as parameters for the vectors etc. although changing the z position for my rendered quad does not send it forward or back when drawn, is this expected? – Obi-Dan Mar 10 '14 at 02:50

0 Answers0