gambit20088
gambit20088

Reputation: 321

OpenGL - rotating around 2 axes using glm

I have created 2 cylinders that i wish to rotate using the following code:

glm::vec3 randVec3 = { 1.0f, 0.0f, 0.0f };
glm::vec3 randVec4 = { 0.0f, 1.0f, 0.0f };
d_tfms[i] = glm::rotate(90.0f, randVec3 );
d_tfms[i] = glm::rotate(10.0f, randVec4);

d_tfms[i] is a 4x4 matrix which is then being transferred to the vertex shader as the ModelView matrix.

Before any rotation, this is how my cylinders look like: (ignore the other shapes, the cylinders here appear as hollow 2D circles)

enter image description here

After the first rotation (90 degrees in the x-axis):

enter image description here

After the second rotation (10 degrees in the y-axis):

enter image description here

As you can see, those rotations don't make sense. In particular, if you look at the dark blue cylinder in the first image vs. the second, it seems like it doesn't rotate around its own local x-axis.

What could be the problem?

Upvotes: 0

Views: 1795

Answers (2)

Drout
Drout

Reputation: 335

Ok, answering your question about how to mix 2 rotations, just multiply them (Note: matrix multiplication is Not commutative, so matrixA * matrixB is not the same as matrixB * matrixA): d_tfms[i] = glm::rotate(10.0f, randVec4) * glm::rotate(90.0f, randVec3 );

ps, you may provide the 1st parameter to both functions, which is (in this case) an identity matrix (glm::mat4(1.0)) ie: d_tfms[i] = glm::rotate(glm::mat4(1.0), 10.0f, randVec4) * glm::rotate(glm::mat4(1.0), 90.0f, randVec3 );

Upvotes: 1

Robert
Robert

Reputation: 21

d_tfms[i] = glm::rotate(10.0f, randVec4);

This doesn't work for me... do you have the latest version of glm ?

The glm::rotate function has 3 parameters :

  • a 4x4 matrix which could be a unit matrix or a transpose matrix(remember the multiply order);
  • angle of the rotation;
  • a vec3 used to tell which axis you want to rotate;

try to use(at least to be sure if you rotate accordingly):

tfms[i] = glm::rotate(tfms[i],glm::radians(90.0f),randVec4);

Upvotes: 0

Related Questions