3

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.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
bvs
  • 340
  • 5
  • 20
  • I really don't know what you're actually ask about. – derhass Jul 27 '14 at 13:44
  • For example I have a "cube" in Maya or Blender thats been rotated 90 in x around a pivot thats been moved to the side. Now I want to "center the pivot" so that the pivot is in the center of the rotated cube but the attributes (Translation, rotation, scale) in the "channel box" stay the same. This is how it works in all 3d apps. However, I cant do it with the above modelM. How? – bvs Jul 27 '14 at 14:15
  • That does not really mean anything. The "pivot point" seems to be an arbitrary point you choose, but which has no real meaning for the transformation of the vertices of the object. The model matrix will transform the vertices from object space to world space, and when you want that an object to appear at exactly the same position and orientation and scale in world space, the model matrix must not change at all (aprart form some invariances allowed due to homogeneous space, but we can ignore that here). So whatever your pivot point represents here at all is totally irrelevant. – derhass Jul 27 '14 at 14:40
  • And that's why I don't understand the question - maybe I got it wrong, but I really fail to see waht you're after here. Your code also does not explain much, you define some matrixes which arent further used, and you multiply some unknown matrices together, so it is totally unclear what you are trying to achieve here. – derhass Jul 27 '14 at 14:43
  • Go into Maya, create a "torus", press "D" and drag the pivot somewhere - say to (5, 2, 0). Then Change the rotation value of the "torus" to say (90,0,0). Now, go to Modify->Center Pivot and notice how the pivot is in the center of the torus, but nothing in the Channel Box has changed and the torus is in the same spot. This is **_not_ possible** with the above ModelM I have. I need to replicate this behavior since its standard for all 3d modelling apps. – bvs Jul 27 '14 at 14:46
  • I don't have maya, but I have blender. And it does exactly what one would expect: you have some global transformation decomposited into translation, rotation and scale. And of course this does not change when you change the pivot point for, say, a rotation. Why would it? The transformations do accumulate, so if you add another rotation with a new pivot element, it affects the following transformations - and that is _exactly_ why I don't understand your question: changing the pivot point alone does not change the model matrix at all. – derhass Jul 27 '14 at 14:57
  • You're not being clear. It changes. I spent hours yesterday looking at blender code and they clearly have different cases and special matricies that they multiply by in setting the pivot. They have hundreds of lines based solely on changing the pivot. I mean, I spent a long time looking at this yesterday and now you're just telling me "Why would it"? – bvs Jul 27 '14 at 15:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58105/discussion-between-derhass-and-waaitt). – derhass Jul 27 '14 at 15:04

1 Answers1

3

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.

Jon Simpkins
  • 152
  • 1
  • 10