So I am doing a simple FPS camera with openGL. I store the camera as 4 vector, a position vector, a lookAt vector, an up vector and a right vector. When the user presses a key, I create a rotation matrix that rotates around the up vector 10 degree then multiply the three vectors that specify the orientation of the camera with it. With these new values I create a new view matrix from glm::lookAt. However, the result I get is very weird. When I press the button to rotate, the object the camera is initially pointing at dissapears, no matter how small I set the rotation. After pressing the rotate button several more time, it appears again, then if i press more it dissappears once more.
I feel like the code is not wrong, but my understanding of the math behind it and therefore the implementation is wrong. Can anyone explain to me this weird behavior? Below is my code.

look = glm::vec3(glm::rotate(theta, axis) * glm::vec4(look, 1.0f)); // theta set at 2 degree
right = glm::vec3(glm::rotate(theta, axis) * glm::vec4(right, 1.0f));
up = glm::vec3(glm::rotate(theta, axis) * glm::vec4(up, 1.0f));
glm::mat4 cameraMat = glm::lookAt(eyePos, eyePos+camera.look, camera.up);
  • $\begingroup$ Are you sure you're specifiying your angle in degrees? Modern versions of GLM use radians everywhere and the docs are not always clear on this. $\endgroup$ – russ Mar 16 '18 at 4:57
  • $\begingroup$ @russ so GLM uses radians? I thought it uses degree. I will test with radians and update the result if it indeed fixes the problem $\endgroup$ – Manh Nguyen Huu Mar 16 '18 at 5:08
  • $\begingroup$ Depends on version. Older versions were inconsistent, you could define GLM_FORCE_RADIANS before including it to make it use radians everywhere. Since version 0.9.6 this has become the default. From the doc - "Finally, here is a list of all the functions that could use degrees in GLM that requires radians in GLM 0.9.6: rotate (matrices and quaternions), perspective, perspectiveFov, infinitePerspective, tweakedInfinitePerspective, roll, pitch, yaw, angle, angleAxis, polar, euclidean, rotateNormalizedAxis, rotateX, rotateY, rotateZ and orientedAngle." $\endgroup$ – russ Mar 16 '18 at 5:30
  • $\begingroup$ @russ You are indeed correct. Thanks very much. Please create an answer so that I can accept it. $\endgroup$ – Manh Nguyen Huu Mar 16 '18 at 12:10

Promoting my comment into an answer since it identified the problem...

The issue is confusion between degrees and radians for the angle parameter. GLM, up to version 0.9.5, was very inconsistent in how it dealt with angles, accepting degrees for some functions and radians for others. This behaviour could be overridden with #define GLM_FORCE_RADIANS before including the header.

In version 0.9.6, radians everywhere became the default. Unfortunately, if you Google 'glm rotate' at the time of writing, the top result is the 0.9.4 doc page, which still describes the parameters as taking degrees, so you need to be aware of which version you are using and its default behaviour in this regard. If you still want to use degrees for your angle variables, you can convert them with glm::radians(angle) before passing them as arguments.


Your Answer

