I am trying to rotate my model around the global axis. This is what I have:
I created a method that would process the user input. If the user presses A or D, the model will rotate 90 degrees to the left or to the right around the Z axis. If the user presses W or S, it will rotate 90 degrees forward or backward around the X axis. Finally, it the user presses Q or E, it will rotate around the Y axis to the left or to the right. But if I rotate on the x axis and then try to rotate on the y axis, it will rotate depending on the local axis of the model, not around the global axis.
Can someone help me fix this?
The following is my code to process the inputs for my model:
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) {
// reposition camera
heartModel1 = true;
heartModelX -= 30.0f;
heartWallX -= 30.0f;
heartModelZ += 30.0f;
heartWallZ += 30.0f;
}
if (heartModel1) {
// Rotate model
// D -> Rotate Right
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
heartAngleZ -= 90.0f;
}
// A -> Rotate left
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
heartAngleZ += 90.0f;
}
// S -> Rotate back
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
heartAngleX += 90.0f;
}
// W -> Rotate forward
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
heartAngleX -= 90.0f;
}
// Q -> Rotate left on Y axis
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
heartAngleY += 90.0f;
}
}
The following is my model:
glm::mat4 modelHeart = glm::mat4(1.0f);
modelHeart = glm::translate(modelHeart, glm::vec3(heartModelX + 30, heartModelY, heartModelZ - 30));
modelHeart = glm::rotate(modelHeart, glm::radians(heartAngleX), glm::vec3(1.0f, 0.0, 0.0)); // Rotate on X axis
modelHeart = glm::rotate(modelHeart, glm::radians(heartAngleY), glm::vec3(0.0, 1.0f, 0.0)); // Rotate on Y axis
modelHeart = glm::rotate(modelHeart, glm::radians(heartAngleZ), glm::vec3(0.0, 0.0, 1.0f)); // Rotate on Z axis
modelHeart = glm::scale(modelHeart, glm::vec3(heartModelScale, heartModelScale, heartModelScale));