bvs
bvs

Reputation: 350

Centering the pivot of a GL object that has already been transformed around another pivot?

I'm looking for the best way to center and offset a rotation pivot on an object using openGL shaders. There are differences in 3d apps in how they handle this.


I have an object which I set translation to (0,2,2), rotation to (90,0,0) and move the pivot to (5,2,0) and then rotate (or not) arbitrarily. Then I want to "center the pivot" on the object to the translated bb center

I've tried variations of this modelMatrix without success in all scenarios where pivRT is the difference to the BBcenter: modelM = translationM * pivRT * glm::inverse(pivotM) * rotationM * scaleM * pivotM;

In Maya

I "center the pivot" on the object and no values visible to the user change. In other apps such as blender the translation changes when you center the pivot - in fact your objects translation becomes the pivots translation. Reading up on how this works, in the xform command or MTransformationMatrix class, Maya has the transformation matrix as

[Sp]x[S]x[Sh]x[Sp]x[St]x[Rp]x[Ro]x[R]x[Rp]x[Rt]x[T]

with Rt being the "Rotate pivot translation" matrix which is translation introduced to preserve exisiting rotate transformations when moving pivot. This is used to prevent the object from moving when the objects pivot point is not at the origin and the pivot is moved. [Rt]

I believe this rotate pivot translation matrix works and is more intuitive than in blender where the translation values change but I have trouble implementing either.

Upvotes: 3

Views: 1445

Answers (1)

Jon Simpkins
Jon Simpkins

Reputation: 152

If I follow your example correctly, this would be how to:

  1. Move the pivot to (5,2,0)
  2. Rotate the object about the new pivot (let's say 2 radians about the z-axis)
  3. Recenter the pivot on the object
  4. Rotate the object (about the new centered pivot) by 90 degrees about the x-axis
  5. Translate the object by (0,2,2)

With glm, this would be the code for the model matrix:

glm::mat4 Step1 = glm::translate(glm::mat4(1.0), glm::vec3(5,2,0));
glm::mat4 Step2 = glm::rotate(Step1, 2.0, glm::vec3(0,0,1));
glm::mat4 Step3 = glm::translate(Step2, glm::vec3(-5,-2,0));
glm::mat4 Step4 = glm::rotate(Step3, 1.57, glm::vec3(1,0,0));
glm::mat4 Step5 = glm::translate(Step4, glm::vec3(0,2,2) + glm::vec3(5,2,0));

Step 3 in the list is technically accomplished in two parts: translating the object before the second rotation, and then by translating it back again in the final translation step.

Upvotes: 3

Related Questions