从模型视图矩阵中提取比例矩阵 [英] extracting scale matrix from modelview matrix

查看:200
本文介绍了从模型视图矩阵中提取比例矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从模型视图矩阵中提取比例矩阵?现在,我要花每一句的长度,但是当比例为负数时,它会失败. 这是我的代码:

how do we extract scale matrix from model view matrix? Right now I am taking length of each coloumn, but it fails when the scale is negative. here is my code:

  float xs =
            matrix[0][0] * matrix[0][1] * matrix[0][2] * matrix[0][3] < 0 ?
                    -1 : 1;
    float ys =
            matrix[1][0] * matrix[1][1] * matrix[1][2] * matrix[1][3] < 0 ?
                    -1 : 1;
    float zs =
            matrix[2][0] * matrix[2][1] * matrix[2][2] * matrix[2][3] < 0 ?
                    -1 : 1;


    glm::vec3 new_scale;
    new_scale.x =  xs* glm::sqrt(
                    matrix[0][0] * matrix[0][0] + matrix[0][1] * matrix[0][1]
                            + matrix[0][2] * matrix[0][2]);
    new_scale.y =  ys* glm::sqrt(
                    matrix[1][0] * matrix[1][0] + matrix[1][1] * matrix[1][1]
                            + matrix[1][2] * matrix[1][2]);
    new_scale.z = zs* glm::sqrt(
                    matrix[2][0] * matrix[2][0] + matrix[2][1] * matrix[2][1]
                            + matrix[2][2] * matrix[2][2]);

例如:

float []mat={0.032254f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, -0.0052254f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, 0.4332254f, 0.000000f, 0.000000f, 0.000000f, 0.000000f, 1.000000f};

推荐答案

请参见均匀变换矩阵.提取比例的方式是可以的.那么标志呢?如果您的变换包含旋转,则当前的方法将不起作用...

see homogenous transformation matrices. The way you are extracting scale is OK. So what about the sign? Your current approach will not work if your transformation contains rotations ...

另一个问题是,您不知道哪个比例尺取反,哪个不取反,这是因为如果否定单轴,则取反其他任何一个并旋转以匹配位置都可以获得相同的结果.如果否定2根轴,则会得到具有不同旋转度的原始矩阵.

Another problem is that you can not know which scale is negated and which not because if you negate single axis you can obtain the same result if you negate any other one and rotate to match the position. If you negate 2 axises you get the original matrix with different rotation.

您能做的最好的事情就是检测矩阵是否反转了1轴或3轴:

The best you can do is detect if your matrix has inverted 1 or 3 axis:

  1. 为原始未失真矩阵创建符号表

例如单位矩阵,但是如果您有不同的起点,请使用

for example unit matrix but if you have different starting point use that

sz0=dot(cross(X0,Y0),Z0);
sy0=dot(cross(Z0,X0),Y0);
sx0=dot(cross(Y0,Z0),X0);

其中X0,Y0,Z0是从起点矩阵中提取的轴矢量

where X0,Y0,Z0 are extracted axises vectors from your start point matrix

计算当前矩阵的符号

sz1=dot(cross(X1,Y1),Z1);
sy1=dot(cross(Z1,X1),Y1);
sx1=dot(cross(Y1,Z1),X1);

其中X1,Y1,Z1是从实际矩阵中提取的轴矢量

where X1,Y1,Z1 are extracted axises vectors from your actual matrix

比较符号并推断哪些轴比例为负

如果(sx0*sx1<0)||(sy0*sy1<0)||(sz0*sz1<0),则一个或所有3个轴都取反,但是您不知道哪一个...而且所有3个符号比较都应具有相同的结果.

if (sx0*sx1<0)||(sy0*sy1<0)||(sz0*sz1<0) then one or all 3 axises are negated but you can not know which ... Also all 3 sign comparisons should have the same result.

[edit1]澄清

  • X=(matrix[0][0],matrix[0][1],matrix[0][2])
  • dot(a,b)=a.x*b.x+a.y*b.y+a.z*b.z是向量的标量乘法(点积)
  • c=cross(a,b) ... c.x=a.y*b.z+a.z*b.y c.y=a.z*b.x+a.x*b.z c.z=a.x*b.y+a.y*b.x是向量乘法(叉积)
  • X=(matrix[0][0],matrix[0][1],matrix[0][2])
  • dot(a,b)=a.x*b.x+a.y*b.y+a.z*b.z is scalar multiplication of vectors (dot product)
  • c=cross(a,b) ... c.x=a.y*b.z+a.z*b.y c.y=a.z*b.x+a.x*b.z c.z=a.x*b.y+a.y*b.x is vector multiplication (cross product)

因此,当您计算两个向量的叉形时,您将获得垂直于两个操作数的向量.由于矩阵轴矢量应垂直乘以2个轴,因此得到了第三个.如果原始轴和计算出的第三轴方向相同,则点积只是比较...这种方式在旋转时不变

So when you compute cross of two vectors you get vector perpendicular to both operands. As matrix axis vectors should be perpendicular by multipliyng 2 axises you get the third. The dot product just compare if the original and computed third axis are in the same direction ... This way is invariant on rotations

这篇关于从模型视图矩阵中提取比例矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆