Android OpenGL Matrix特性:先应用后面的,再应用前面的

在开发Android OpenGL应用时,发现Matrix似乎不是按照代码中出现的次序应用的,而是反过来的。

比如说,我想构造这样一个Matrix,先旋转一个角度,再平移宽高的一半,最后缩放以归一化。那么应当先出现缩放的代码,在出现平移的代码,最后进行旋转:

float[] vMatrix = new float[16];
Matrix.setIdentityM(vMatrix , 0);
Matrix.scaleM(vMatrix, 0, 2.0f / H, 2.0f / W, 1);
Matrix.translateM(vMatrix, 0, -H / 2, -W / 2, 0);
Matrix.rotateM(vMatrix, 0, ang, 0, 0, 1);

与直觉有很大不同。
查看rotateM实现:

public static void rotateM(float[] rm, int rmOffset,
        float[] m, int mOffset,
        float a, float x, float y, float z) {
    synchronized(sTemp) {
        setRotateM(sTemp, 0, a, x, y, z);
        multiplyMM(rm, rmOffset, m, mOffset, sTemp, 0);
    }
}
public static native void multiplyMM(float[] result, int resultOffset,
        float[] lhs, int lhsOffset,
        float[] rhs, int rhsOffset);

查看multiplyMM的注释:

Multiplies two 4x4 matrices together and stores the result in a third 4x4
matrix. In matrix notation: result = lhs x rhs. Due to the way
matrix multiplication works, the result matrix will have the same
effect as first multiplying by the rhs matrix, then multiplying by
the lhs matrix. This is the opposite of what you might expect.

将两个4x4的矩阵相乘,结果存储在第三个4x4的矩阵中。用矩阵符号描述就是:result = lhs x rhs。由于矩阵乘法的特性,结果矩阵的效果等同于先乘以rhs矩阵,然后乘以lhs矩阵。这与你可能的预期相反。

旋转操作中,sTemp对应形参rhs,它放在右边,先起作用。

实际上,这是由于新出现的矩阵是放在当前矩阵的右侧进行相乘所导致的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值