Matheus Ianzer
Matheus Ianzer

Reputation: 465

How to properly rotate and scale an object in OpenGL?

I'm trying to build the model matrix every frame, for that I'm creating a translation, rotation, and scale matrix and multiplying it. But I can't seem to figure it how to build the rotation matrix and scale it properly.

Here's what I'm doing:

glm::mat4 scale = glm::scale(mat4(1.0f), my_models[i].myscale);
glm::mat4 rotateM(1.0); 
glm:mat4 translate = glm::translate(mat4(1.0f), my_models[i].initialPos);

rotateM = mat4_cast(my_models[i].Quat); 
rotateM = glm::rotate(rotateM, (float) my_models[i].angle * t, my_models[i].animation_axis[0]);

my_models[i].modelMatrix = translate * rotateM *scale;
my_models[i].Quat = quat_cast(my_models[i].modelMatrix); 

In the Constructor I'm using:

quat Quat = glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 1.f, 0.f));

If my_models[i].myscale exactly 1.0f it rotates just fine, but if it is bigger the object keeps growing and rotates weirdly. Quaternions are very new to me, so I'm assuming I'm messing up there.

What am I missing? Are there simpler ways to construct the models rotation matrix? if so, what information should I be saving?

Edit: As jparima suggested, the following fixed my problem.

glm::mat4 scale = glm::scale(mat4(1.0f), my_models[i].myscale);
glm::mat4 rotateM(1.0); 
glm::mat4 translate = glm::translate(mat4(1.0f), my_models[i].initialPos);

my_models[i].Quat = rotate(my_models[i].Quat, my_models[i].angle * t, my_models[i].animation_axis[0]);

rotateM = mat4_cast(my_models[i].Quat); 

my_models[i].modelMatrix = translate * rotateM * scale;

Upvotes: 0

Views: 963

Answers (1)

jparimaa
jparimaa

Reputation: 2044

From the GLM quaternion.hpp for quat_cast it says

Converts a pure rotation 4 * 4 matrix to a quaternion.

You are setting the whole model matrix there which has also scale and translation. In fact, I don't know why you are converting a matrix to quaternion at all. Therefore you could remove the last line (quat_cast) and update the quaternion directly if you want to apply rotations.

Upvotes: 2

Related Questions