In OpenGL (using shaders), I want to implement a system that will take the user's mouse as input, and then rotate the view about the origin of the rendered scene. So, I need to compute the view matrix (assume the model matrix is identity) that represents this rotation. If the view were to rotate around its own origin, then I could do this by applying a simple rotation operation to the view matrix. However, I don't want to do this -- I want to rotate about the origin of the world coordinate system. So, given a rotation angle "theta", how can I compute this matrix? Thanks!

  • \$\begingroup\$ In which language is this? \$\endgroup\$
    – tkausl
    Commented Jul 30, 2015 at 12:53
  • \$\begingroup\$ I am writing OpenGL code in C++. \$\endgroup\$ Commented Jul 30, 2015 at 12:54

1 Answer 1


I would recommend you use the mat-library glm and something like the following:

double theta = glm::radians(90);
glm::vec3 camera(cos(theta), 0, sin(theta));
camera *= 5; // To get some space between camera and object
glm::mat4 matrix = glm::lookAt(camera, glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 1.0, 0,0));

This should give you the result you're looking for.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .