Patsch

08-12-2017, 07:12 AM

!!!!!!!!!!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.

//main()

while(true){

model.rotate(0.0001, glm::vec3(1, 0, 0));

this_thread.sleep(x);

}

//GModel.h

glm::mat4 model;

glm::vec3 translation = {0,0,0};

glm::vec3 scale = { 1,1,1 };

glm::mat4 rotation;

//GModel.cpp

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;

modelMatrix();

}

void GModel::modelMatrix() {

model = glm::translate(model, translation);

model = glm::scale(model, scale);

model = rotation*model;

std::cout << glm::to_string(rotation) << std::endl;

}

//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.

//main()

while(true){

model.rotate(0.0001, glm::vec3(1, 0, 0));

this_thread.sleep(x);

}

//GModel.h

glm::mat4 model;

glm::vec3 translation = {0,0,0};

glm::vec3 scale = { 1,1,1 };

glm::mat4 rotation;

//GModel.cpp

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;

modelMatrix();

}

void GModel::modelMatrix() {

model = glm::translate(model, translation);

model = glm::scale(model, scale);

model = rotation*model;

std::cout << glm::to_string(rotation) << std::endl;

}