I have something I don’t understand during the rotation of a 3D object.
The idea is that I want to rotate my object around the X and Y axes. Initially, at the initial position of the object, I can rotate it around X and around Y normally. As soon as I rotate my object 90 degrees around X, I can no longer rotate it around Y (it rotates around Z instead)! I tried the same thing by first rotating it 90 degrees around Y, then I rotate it around X and I see that it rotates around x !
Is this a matter of relative axis? But if that’s the case, it should be the same in the second case, right?
const char* vertexShaderSource = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoord;
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoord = aTexCoord;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
)";
void OpenGlWidget::paintGL()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, mPushButton->texture);
glBindVertexArray(mPushButton->vao);
glm::mat4 model = glm::rotate(glm::mat4(1.0f), glm::radians(m_rotationAngleX), glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(m_rotationAngleY), glm::vec3(0.0f, 1.0f, 0.0f));
glUniformMatrix4fv(glGetUniformLocation(m_shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawElements(GL_TRIANGLES, mPushButton->indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void OpenGlWidget::mouseMoveEvent(QMouseEvent* event)
{
if (event->buttons() & Qt::LeftButton) {
int deltaX = event->x() - m_lastMousePos.x();
int deltaY = event->y() - m_lastMousePos.y();
m_lastMousePos = event->pos();
m_rotationAngleX += deltaY * 0.5f;
m_rotationAngleY += deltaX * 0.5f;
update();
}
}