为何变换矩阵的行为不同

问题描述 投票:0回答:1

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

对象具有四个不同的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;

在第一种情况下。

enter image description here

我将矩形对象以x移至200个单位。

比我缩放在位置x = 0.0的组

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

 finalMatrix = rectangleTransformationMatrix * groupTransformationMatrix

结果是我期望的。矩形缩放并移向屏幕中心。

enter image description here

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

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

   finalMatrix = rectangleTransformationMatrix * groupTransformationMatrix * TopTransformationMatrix

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

enter image description here

如果我将-200单位添加到顶部容器的枢轴点x上,则得到的结果与预期的一样。

矩形移向屏幕中心并缩放的位置。

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

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

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////

第一种情况

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

opengl matrix glm-math
1个回答
0
投票

您能告诉我矩形,组,顶部的位置,旋转度,比例尺中的每一个吗? (您最后一次尝试)

您是否检查过设置Rectangle的位置(0,0,0)?我认为您的期望是正确的,但您误以为是。

© www.soinside.com 2019 - 2024. All rights reserved.