Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Rotating a model matrix by a quaternion using glm, weird results?

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2016

    Rotating a model matrix by a quaternion using glm, weird results?

    !!!!!!!!!!SOLVED!!!!!!!!!!!!!!!! (feel free to delete, didnt find the option to do it myself)

    //Basically the problem was the order of multiplication, and some multiplications which just were not necessary at all.

    Hey everyone!
    I got stuck at the rotation of my model matrix. While the rotation for the direction _vector_ of my camera works, the rotation for my model _matrix_ does not.
    Below are the two relevant functions in my model class. GModel::rotate(float, glm::vec3) takes in the angle and the axis of rotation, converts it in a quaternion, transfers the quaternion to a rotation matrix and is multiplied with the rotation matrix of the model (as far as I can tell in the correct order). By calling GModel::modelMartrix() the model matrix for the model is calculated by applying transformation, scale and rotation.
    Yet what happens on screen is that the model only flickers, when continuously calling the rotate function, no rotation can be seen.

    Here is what the cout returns for the values of the rotation matrix, when continuously calling rotate(0.001, glm::vec3(1,0, 0));
    [-1 0.00000x 0 0]
    [0.0000x -1 0 0]
    [0 0 1 0]
    [0 0 0 1]

    and some calls later:
    [1 0.00000x 0 0]
    [0.0000x 1 0 0]
    [0 0 1 0]
    [0 0 0 1]

    So basically, it looks like a rotation around the z-axis, right? But regarding that the [0][1] and [1][0] floats are only insignificantly changed, it looks like floating point rounding mistakes to me. So basically the two top rows change their sign, whats probably not the behaviour that properly rotates. Can anyone point out, what I am doing wrong here? Do I miss something when converting quaternions or multiplying matrices? When I use a very similar function to rotate a vector by what the cast_mat4(quaternion) returns, I get the proper results.

    Code :
    model.rotate(0.0001, glm::vec3(1, 0, 0));
    	glm::mat4 model;
    	glm::vec3 translation = {0,0,0};
    	glm::vec3 scale = { 1,1,1 };
    	glm::mat4 rotation;
    void GModel::rotate(float angle, glm::vec3 axis) {
    		angle = angle* 3.141592 / 180; //rad
    		float sinHalfAngle = sin(angle / 2);
    		float cosHalfAngle = cos(angle / 2);
    		float rX = axis.x*sinHalfAngle;
    		float rY = axis.y*sinHalfAngle;
    		float rZ = axis.z*sinHalfAngle;
    		float rW = cosHalfAngle;
    		glm::quat rota(rX, rY, rZ, rW);
    		rotation = glm::mat4_cast(rota)*rotation;
    void GModel::modelMatrix() {
    	model = glm::translate(model, translation);
    	model = glm::scale(model, scale);
    	model = rotation*model;
    	std::cout << glm::to_string(rotation) << std::endl;
    Last edited by Patsch; 08-12-2017 at 09:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts