0
votes

I am learning modern opengl(3.3), and atm I am creating my own mvp matrices. I just have some weird problem, when I try to rotate the world, my camera does not seem to rotate around the center. Here is an image of a triangle centered at the origin, while rotating. http://postimg.org/image/5l4k4cjqz/

These are my matrices:

glm::mat4 model = glm::mat4(1.0);
glm::mat4 view = glm::lookAt( glm::vec3(0.0, 0.0, 5.0),
                              glm::vec3(0.0, 0.0, 0.0), 
                              glm::vec3(0.0, 1.0, 0.0) );
glm::mat4 projection = glm::perspective(45.0f, 1024.0f / 768.0f, 0.1f, 100.0f);
glm::mat4 xRotation = glm::rotate( glm::mat4(1.0f), this->xAngle, glm::vec3(1.0, 0.0, 0.0) );
glm::mat4 yRotation = glm::rotate( glm::mat4(1.0f), this->yAngle, glm::vec3(0.0, 1.0, 0.0) );
glm::mat4 zRotation = glm::rotate( glm::mat4(1.0f), this->zAngle, glm::vec3(0.0, 0.0, 1.0) );
glm::mat4 mvp = projection * view * model * xRotation * yRotation * zRotation;

When I remove the projection matrix from the mvp matrix, it will rotate around the center.

PS: Is there a better way to do the rotation, can I put it in the view matrix?

1
What do you mean by "rotate your world?" - Nicol Bolas
Well I am not rotating the model matrix, so ain't I rotating the world with the *Rotation matrices? - user2246190
No, you're not. It's not clear what effect you're trying to achieve here. - Nicol Bolas
Technically, you're not rotating the model or the world matrix, you're rotating the object itself, then apply the model matrix. Since model==1 in your case it doesn't matter though. What are the coordinates of your triangle? Typically, with Euler rotation the rotation order is xyz, so you might want to reverse how you apply the rotation matrices. - Andreas Haferburg
Sorry, I am a beginner in graphics programming. I wan't to be able to move around in the world. So I should be able to 'look' around. Thats what I am trying to do with the rotation matrices. @AndreasHaferburg: Real triangleVerts[] = { -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 0.0, 1.0, 0.0, - user2246190

1 Answers

1
votes

If the camera is located at c, and you want to rotate it around the pivot point p, you have to first translate the camera by d = p - c, then rotate, then translate it back by -d. I'm not familiar with glm, so here's just pseudo code.

viewMatrix = translationMatrix(-d) * rotationMatrix * translationMatrix(d)