为什么变换矩阵表现不同
why is the transformation matrices behaving differently
我在缩放对象时得到不同的结果。
对象有四个不同的 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
结果我 expected.The 矩形缩放并向屏幕中心移动。
现在如果我用三个容器做同样的事情。
在这里我将组容器移动到 200 并缩放位于位置 0.0 的顶部容器
finalMatrix = rectangleTransformationMatrix * groupTransformationMatrix * TopTransformationMatrix
矩形在它自己的位置缩放,就好像屏幕的中心点也移动了 200 个单位。
如果我将 -200 个单位添加到顶部容器的枢轴点 x 上,我得到的结果与我预期的一样。
矩形向屏幕中心移动并缩放。
如果有人能解释一下为什么我需要向顶部的中心点添加 -200 个单位 container.Whereas 在第一种情况下我不需要向缩放的轴心点添加任何值容器。
当两个操作本质上相同时。
////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////
第一个案例
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 = RectangleMatrix * groupMatrix
////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////
第二种情况
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 - > 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
矩阵运算不是Commutative。 scale * translate
与 translate * 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
如果比例编码为 groupScalingMatrix
或 topScalingMatrix
,则结果不同,翻译分别编码为 groupPositionMatrix
或 topPositionMatrix
。
我在缩放对象时得到不同的结果。
对象有四个不同的 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
结果我 expected.The 矩形缩放并向屏幕中心移动。
现在如果我用三个容器做同样的事情。
在这里我将组容器移动到 200 并缩放位于位置 0.0 的顶部容器
finalMatrix = rectangleTransformationMatrix * groupTransformationMatrix * TopTransformationMatrix
矩形在它自己的位置缩放,就好像屏幕的中心点也移动了 200 个单位。
如果我将 -200 个单位添加到顶部容器的枢轴点 x 上,我得到的结果与我预期的一样。
矩形向屏幕中心移动并缩放。
如果有人能解释一下为什么我需要向顶部的中心点添加 -200 个单位 container.Whereas 在第一种情况下我不需要向缩放的轴心点添加任何值容器。
当两个操作本质上相同时。
////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////
第一个案例
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 = RectangleMatrix * groupMatrix
////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////
第二种情况
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 - > 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
矩阵运算不是Commutative。 scale * translate
与 translate * 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
如果比例编码为 groupScalingMatrix
或 topScalingMatrix
,则结果不同,翻译分别编码为 groupPositionMatrix
或 topPositionMatrix
。