如何在 OpenGL 着色器中使用行优先? [英] How to use row-major in OpenGL shader?

查看:33
本文介绍了如何在 OpenGL 着色器中使用行优先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OpenGL 站点上看到了这个:

OpenGL 着色语言属性变量可以是 mat2、mat3 或 mat4 类型.这些类型的属性可以使用 glVertexAttrib 入口点加载.矩阵必须按列主要顺序加载到连续的通用属性槽中,每个通用属性槽中有一列矩阵.

OpenGL Shading Language attribute variables are allowed to be of type mat2, mat3, or mat4. Attributes of these types may be loaded using the glVertexAttrib entry points. Matrices must be loaded into successive generic attribute slots in column major order, with one column of the matrix in each generic attribute slot.

我一直在使用基于row-major的向量库.也就是说,4x4 矩阵的 16 个元素在结构体中的布局如下:

I have been successively using a vector library that is based on row-major. That is, the 16 elements of 4x4 matrix are laid out like this in a struct:

vec4 x;
vec4 y;
vec4 z;
vec4 w;

其中每个 vec4 都有组件 x, y, z, w,因此内容的线性顺序是行优先的,因为第一行占据了前 4 个位置在内存中,矩阵数学是这样处理的,例如这个旋转变换:

Where each vec4 has components x, y, z, w, thus the linear order of the contents is row-major since the first row occupies the first 4 positions in memory, and matrix math is handled as such, for example this rotate transform:

 static Matrix4<T> RotateZ(T degrees)
{
    T radians = degrees * 3.14159f / 180.0f;
    T s = std::sin(radians);
    T c = std::cos(radians);

    Matrix4 m;
    m.x.x =  c; m.x.y = s; m.x.z = 0; m.x.w = 0;
    m.y.x = -s; m.y.y = c; m.y.z = 0; m.y.w = 0;
    m.z.x =  0; m.z.y = 0; m.z.z = 1; m.z.w = 0;
    m.w.x =  0; m.w.y = 0; m.w.z = 0; m.w.w = 1;
    return m;
}

该库以行为主,以在编写客户端代码时支持从左到右的计算,这对许多程序员来说是很自然的.但我不明白的是,这些行主矩阵按原样发送到着色器,根据上述内容,预计列主矩阵.

The library is row-major to support left-to-right computations when writing the client-side code, which is natural for a lot of programmers. But what I don't understand is that these row-major matrices are getting sent as-is to the shaders, which according to the above, expect column-major.

我知道着色器从右到左执行计算.但是行主矩阵不应该被转置,以便在被送入着色器之前它是列主矩阵吗?我没有这样做过,但它工作得很好.

I understand that the shader performs calculations right-to-left. But shouldn't a row-major matrix be transposed so it is column-major before being fed into a shader? I have not been doing this, yet it is working just fine.

推荐答案

就 OpenGL 而言,从您的问题来看,矩阵的内存布局是否采用行优先形式并不明显.如果矩阵的最后一个成员 (w) 包含矩阵所表示的变换的平移分量,则它与列主布局没有区别.

It isn't obvious from your question whether the memory layout of your matrix is in row-major form as far as OpenGL is concerned. If your last member (w) of the matrix contains the translation component of the transform the matrix is representing, then it is indistinguishable from a column-major layout.

如果您在进行矩阵向量乘法时与操作数的顺序一致,则可以在 GLSL 中使用行主矩阵.

If you are consistent with the order of operands when doing matrix-vector multiplies, you can work with row-major matrices in GLSL.

如果您的着色器代码有如下所示的乘法:

If your shader code has multiplications which look like this:

vec4 v_t = vector * matrix_row_major

这与:

vec4 v_t = matrix_column_major * vector

其中matrix_row_majormatrix_column_major

这篇关于如何在 OpenGL 着色器中使用行优先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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