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

查看:92
本文介绍了如何在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.

我已经连续使用了基于行优先的向量库.也就是说,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天全站免登陆