【问题标题】:why is the transformation matrices behaving differently为什么转换矩阵的行为不同
【发布时间】:2020-05-30 02:45:15
【问题描述】:

缩放对象时我得到不同的结果。

对象有四个不同的 glm::vec3 值

 1) Position , Rotation , Scaling , Center Point

这是对象的变换矩阵

   TransformationMatrix = PositionMatrix() * RotationMatrix() * ScalingMatrix();

旋转和缩放矩阵是这样的。

glm::vec3 pivotVector(pivotx, pivoty, pivotz);
glm::mat4 TransPivot = glm::translate(glm::mat4x4(1.0f), pivotVector);
glm::mat4 TransPivotInverse = glm::translate(glm::mat4x4(1.0f), -pivotVector);
glm::mat4 TransformationScale = glm::scale(glm::mat4(1.0), glm::vec3(scax, scay, scaz));
return   TransPivot * TransformationScale * TransPivotInverse;

第一种情况。

我将矩形对象移动到以 x 为单位的 200 个单位。

比我缩放位置 x = 0.0 的组

所以矩形对象的最终矩阵是

 finalMatrix = rectangleTransformationMatrix * groupTransformationMatrix

结果符合我的预期。矩形缩放并向屏幕中心移动。

现在如果我用三个容器做同样的事情。

在这里我将组容器移动到 200 并缩放位于位置 0.0 的顶部容器

   finalMatrix = rectangleTransformationMatrix * groupTransformationMatrix * TopTransformationMatrix

矩形在自己的位置缩放,就好像屏幕的中心点也移动了 200 个单位。

如果我将 -200 个单位添加到顶部容器的轴心点 x 上,我会得到预期的结果。

矩形向屏幕中心移动并缩放。

如果有人能解释一下为什么我需要在顶部容器的中心点添加 -200 个单位。而在第一种情况下,我不需要向缩放容器的枢轴点添加任何值。

当两个操作本质上相同时。

/////////////////////////////////////// ///////////////////////////////////////// //////////////////////p>

第一种情况

Rectangle - > position( x = 200 , y = 0, z = 0) , scaling( 1.0 , 1.0 , 1.0 ) , Rotation( 0.0 , 0.0 , 0.0 )
glm::mat4 PositionMatrix = glm::position( // fill the values);
glm::mat4 ScalingMatrix = glm::scaling( // fill the values);
glm::mat4 RotationMatrix = glm::rotate( // fill the values);
RectangleMatrix = PositionMatrix() * RotationMtrix() * ScalingMatrix();

组的矩阵

 froup - > position( x = 0.0 , y = 0, z = 0) , scaling( 0.5 , 1.0 , 1.0 ) , Rotation( 0.0 , 0.0 , 0.0 )
groupMatrix = PositionMatrix() * RotationMtrix() * ScalingMatrix();

最终结果 finalMatrix = 矩形矩阵 * 组矩阵

/////////////////////////////////////// ///////////////////////////////////////// //////////////////////p>

第二种情况

Rectangle - > position( x = 0 , y = 0, z = 0) , scaling( 1.0 , 1.0 , 1.0 ) , Rotation( 0.0 , 0.0 , 0.0 )
glm::mat4 PositionMatrix = glm::position( // fill the values);
glm::mat4 ScalingMatrix = glm::scaling( // fill the values);
glm::mat4 RotationMatrix = glm::rotate( // fill the values);
RectangleMatrix = PositionMatrix() * RotationMtrix() * ScalingMatrix();

组的矩阵

 group - > position( x = 200.0 , y = 0, z = 0) , scaling( 1.0 , 1.0 , 1.0 ) , Rotation( 0.0 , 0.0 , 0.0 )
groupMatrix = PositionMatrix() * RotationMtrix() * ScalingMatrix();

Top 的矩阵

 Top - > position( x = 0.0 , y = 0, z = 0) , scaling( 0.5 , 1.0 , 1.0 ) , Rotation( 0.0 , 0.0 , 0.0 )
TopMatrix = PositionMatrix() * RotationMtrix() * ScalingMatrix();

最终结果 finalMatrix = RectangleMatrix * groupMatrix * TopMatrix

【问题讨论】:

  • 如果groupTransformationMatrix 包含缩放,那么TopTransformationMatrix 的翻译也会缩放
  • @Rabbid76 我可以请你详细解释一下,为什么在第一种情况下没有发生。
  • @Rabbid76 groupTransformationMatrix = positionMatrix() * rotationMatrix() * scalingMatrix() ,但所有轴的缩放值为 1.0。
  • 您的问题不清楚。看不懂groupTransformationMatrix TopTransformationMatrix的内容。在第一种情况下,根本没有TopTransformationMatrix。注意,如果M7 = M1 * M2 * M3M8 = M4 * M5 * M6,那么M7 * M8 = M1 * M2 * M3 * M4 * M5 * M6。将转换写在一行中以查看差异。
  • @Rabiib76 谢谢,我会用两个矩阵的内容更新我的问题。

标签: opengl matrix glm-math


【解决方案1】:

矩阵运算不是Commutativescale * translatetranslate * scale不一样

如果你有 200 的平移和 0.2 的比例,那么

translate(200) * scale(0.2)

将对象缩放 0.2 并平移 200。但是

scale(0.2) * translate(200)

将对象缩放 0.2 并平移 40 (0.2*200)。

如果你有 2 个矩阵:

groupMatrix = PositionMatrix() * RotationMtrix() * ScalingMatrix();
TopMatrix = PositionMatrix() * RotationMtrix() * ScalingMatrix();

那么groupMatrix * TopMatrix就等于

groupPositionMatrix * groupRotationMtrix * groupScalingMatrix * topPositionMatrix * topRotationMtrix * topScalingMatrix

如果比例尺分别编码为groupScalingMatrixtopScalingMatrix,则结果不同,翻译编码为groupPositionMatrixtopPositionMatrix

【讨论】:

  • 非常感谢,我现在明白为什么我会得到不同的结果。如果我想相对于屏幕中心缩放 Rectangle 对象,我应该怎么做?
  • 目前我正在将组容器位置的倒数添加到顶部容器的枢轴上,这是正确的方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多