Reputation: 385
Now I'm doing the following transformations on the object:
//create model matrix
model_matrix = glm::mat4(1.0f);
//create view matrix
view_matrix = glm::mat4(1.0f);
//create projection matrix
projection_matrix = glm::mat4(1.0f);
//setting up model
model_matrix = glm::translate(model_matrix, glm::vec3(0, 0, -2.5));
//x_rot and y_rot are global variables.
y_rot += y_rotation_speed_in_degrees;
x_rot += x_rotation_speed_in_degrees;
model_matrix = glm::rotate(model_matrix, glm::radians(y_rot), glm::vec3(0, 1, 0));
model_matrix = glm::rotate(model_matrix, glm::radians(x_rot), glm::vec3(1, 0, 0));
//setting up projection
projection_matrix = glm::perspective(45.0f, (float)window_width / (float)window_height, 0.1f, 100.0f);
replication_matrix = projection_matrix * view_matrix * model_matrix;
For rotation, I set (x/y) _rotation_speed_in_degrees.
Although the rotation around the Y-axis(2), rotation on 3 occurred around the local X-axis.
I expect to see this
Why is this happening and how to make all the rotations around the global axes?
I'm trying to do this:
I declare the model and rotate matrix in the header file
glm::mat4 model_matrix = glm::mat4(1.0f);
glm::mat4 rotation_matrix = glm::mat4(1.0f);
And I do the following in my drawing cycle:
//create model matrix
model_matrix = glm::mat4(1.0f);
//create view matrix
view_matrix = glm::mat4(1.0f);
//create projection matrix
projection_matrix = glm::mat4(1.0f);
rotation_matrix = glm::rotate(rotation_matrix, glm::radians(y_rotation_speed_in_degrees), glm::vec3(0, 1, 0));
rotation_matrix = glm::rotate(rotation_matrix, glm::radians(x_rotation_speed_in_degrees), glm::vec3(1, 0, 0));
model_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, -2.5)) * rotation_matrix;
//setting up projection
projection_matrix = glm::perspective(45.0f, (float)window_width / (float)window_height, 0.1f, 100.0f);
replication_matrix = projection_matrix * view_matrix * model_matrix;
And this is what I get:
Turns 2 and 3 around along the local axes, but not the global ones.
Upvotes: 1
Views: 450
Reputation: 210908
Why is this happening [...]
The matrix multiplication is not Commutative. The order matters.
If you want to rotate the object step by step, based on the previous rotations, you must store the model matrix and apply the new rotations to the stored model matrix. You need to keep the model matrix beyond frames instead of summarizing the angles of rotation.
Before the application loop, create an initial roation matrix:
rotation_matrix = glm::mat4(1.0f);
Apply the new rotation in the application loop and calculate the model matrix:
glm::mat4 rm_x = glm::rotate(glm::mat4(1.0f),
glm::radians(y_rotation_speed_in_degrees), glm::vec3(0, 1, 0));
glm::mat4 rm_y = glm::rotate(glm::mat4(1.0f),
glm::radians(x_rotation_speed_in_degrees), glm::vec3(1, 0, 0));
glm::mat4 rotation_matrix = rm_x * rm_y * rotation_matrix;
model_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, -2.5)) * rotation_matrix;
Upvotes: 1