首页 文章

SceneKit 的 SCNMatrix4 是存储为列还是 row-major?

提问于
浏览
2

我感到困惑的原因是SCNMatrix4from Apple 上的文档:

SceneKit 使用矩阵表示坐标空间转换,而坐标转换又可以表示 three-dimensional 空间中对象的组合位置,旋转或方向以及比例。 SceneKit 矩阵结构的顺序为 row-major2,因此适合传递给接受矩阵参数的着色器程序或 OpenGL API。

由于 OpenGL 使用 column-major 转换矩阵,因此这似乎是矛盾的!

通常,有关此主题的 Apple 文档很差:

  • GLKMatrix4根本没有谈论列与行的主要内容,但是从网上所有讨论中我都可以肯定 GLKMatrix 是简单明了的 column-major。

  • simd_float4x4 没有讨论该主题,但是至少变量名清楚地表明它存储为列:

来自simd/float.h

typedef struct { simd_float4 columns[4]; } simd_float4x4;

在线上有关 SCNMatrix4 的一些讨论似乎是在说 SCNMatrix4 与 GLKMatrix4 不同,但是查看 SCNMatrix4 的代码可以得到一些提示,表明它可能只是 column-order:

/* Returns a transform that translates by '(tx, ty, tz)':
 * m' =  [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1]. */
NS_INLINE SCNMatrix4 SCNMatrix4MakeTranslation(float tx, float ty, float tz) {
    SCNMatrix4 m = SCNMatrix4Identity;
    m.m41 = tx;
    m.m42 = ty;
    m.m43 = tz;
    return m;
}

那么SCNMatrix4 row-major(将翻译存储在m14m24m34中)还是 column-major(将翻译存储在m41m42m43中)?

1 回答

  • 2

    SCNMatrix4GLKMatrix4一样将翻译存储在m41m42m43中。这个小操场证实了它的定义。

    import SceneKit
    
    let translation = SCNMatrix4MakeTranslation(1, 2, 3)
    
    translation.m41
    // 1
    translation.m42
    // 2
    translation.m43
    // 3
    

    我不知道为什么这在文档中是错误的,可能只是一个错误。

相关问题