访问“列主”中的类/联合数据格式 C++

发布于 2024-12-20 08:47:32 字数 1402 浏览 2 评论 0原文

最近我了解了数据类型的 Union 存储说明符 - 它特别适合 OpenGL / DirectX 转换矩阵使用,因为我不想继续使用已弃用的 gluLookAt、gluOrtho 和固定功能管道的各种 MatrixStack 函数。这太棒了,我最近在创建 4x4 基本变换矩阵类时获得了很多乐趣,其内部数据是联合包装的,允许以不同的方式访问矩阵内的数据。问题是当我想创建一种按列聚合/访问数据的方法,而不必创建一些奇怪的

float* Column(int index);

函数时。我知道 C++ 以行优先形式存储数据,那么如何提供列访问?有???在我下面的矩阵数据声明中,我迷失了方向。

//#define ROW_EFFIN_MAJOR
union {
    struct {
#ifdef ROW_EFFIN_MAJOR
// Eat one of several nasty things, OpenGL :|
        float m11, m12, m13, m14,
                m21, m22, m23, m24,
                m31, m32, m33, m34,
                m41, m42, m43, m44;
#else
        float m11, m21, m31, m41,
                m12, m22, m32, m42,
                m13, m23, m33, m43,
                m14, m24, m34, m44;
#endif
    };
    float m[16];
    // Use at your own risk: m[1] COLUMNMAJOR is not the same as m[1] ROWMAJOR
    // Rows!
    union MatrixRow {
        struct {
            float r1, r2, r3, r4;
        };
        float r[4];
    } row[4];
    // .... Columns...?
    union MatrixColumn {
        // TODO:
        // Set Union
        // Column Access ?
        // ???
        // Profit
    } col[???];
};

任何建议、提示、技巧,甚至帮助都会很好。理想情况下,我想通过 col[0].c1 或仅 col[0].c 访问列(c 是整个列的 float[4])。然而,如果这个理想是为了列访问的更大正义而被扼杀(尽管如此,倾向于非函数,因为我可以使用特殊的结构和函数来完成它。它只是不太方便并且比访问慢一点)直接通过数据成员获取数据)。

我花了相当多的时间试图用 google-fu 来解决这个问题,但我不知道如何建立一个不连续的联合。

Recently I've learned about the Union storage specifier for data types - it's particularly for OpenGL / DirectX transformation matrix use, since I don't want to keep using the deprecated gluLookAt, gluOrtho, and various MatrixStack functions of the fixed-function pipeline. It's been fantastic, and I recently had lots of fun creating the 4x4 basic Transformation Matrix Class whose internal data is union-packed to allow different ways of accessing the data within the Matrix. The problem is when I want to create a way of aggregating / accessing the data by its columns, without having to make some weird

float* Column(int index);

function. I know C++ stores its data in row-major form, so how do I provide column access? There's ??? in my below matrix data declaration as to where I'm lost.

//#define ROW_EFFIN_MAJOR
union {
    struct {
#ifdef ROW_EFFIN_MAJOR
// Eat one of several nasty things, OpenGL :|
        float m11, m12, m13, m14,
                m21, m22, m23, m24,
                m31, m32, m33, m34,
                m41, m42, m43, m44;
#else
        float m11, m21, m31, m41,
                m12, m22, m32, m42,
                m13, m23, m33, m43,
                m14, m24, m34, m44;
#endif
    };
    float m[16];
    // Use at your own risk: m[1] COLUMNMAJOR is not the same as m[1] ROWMAJOR
    // Rows!
    union MatrixRow {
        struct {
            float r1, r2, r3, r4;
        };
        float r[4];
    } row[4];
    // .... Columns...?
    union MatrixColumn {
        // TODO:
        // Set Union
        // Column Access ?
        // ???
        // Profit
    } col[???];
};

Any advice, tips, tricks, or even help would be nice. Ideally, I'd like to access the colums by col[0].c1, or just col[0].c (c being a float[4] of the whole column). However, if this ideal is to be slain for the greater justice of column access (though, leaning towards not-a-function, because I can do it with a special struct and a function. It's just less convenient and a little slower than accessing the data directly via a data member).

I've spent a fair amount of time trying to bash this with google-fu, but I can't figure out how to make a non-contiguous union.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-12-27 08:47:32

正如 Marcelo 所指出的,C++ 既不是行优先也不是列优先。它正是且仅是您放入其中的内容。如果您将 float 数组中的前四个值称为“列”,那就没问题了。如果你称其为“行”,那也没关系。

一般来说,您不应该在约定之间来回切换。不要开发可以“两者兼而有之”的结构;只需选择一边并坚持下去即可。如果您将矩阵设置为行优先,那么它们就是行优先。当您将它们上传到 OpenGL 时,您可以手动转置它们,也可以仅传递 GL_TRUE 作为 glUniformMatrix

更多详细信息可用

As pointed out by Marcelo, C++ is neither row-major nor column-major. It is exactly and only what you put into it. If you call the first four values in a float array a "column", it's fine with that. If you call it a "row", it's fine with that too.

In general, you should not switch back and forth between conventions. Don't develop structs that can do "both"; just pick a side and stick with it. If you make your matrices row-major, then they're row-major. When you upload them to OpenGL, you can either manually transpose them or just pass GL_TRUE for the "transpose" parameter of glUniformMatrix.

More details are available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文