使用行主要在OpenGL着色器 [英] Using row-major in OpenGL shader

查看:142
本文介绍了使用行主要在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;
}

该库是行的主要编写客户端code,这是很自然的很多程序员的时候,支持左到右计算。但我不明白的是,这些行为主的矩阵越来越被作为,是对着色器,根据上述其中,预计列重大。

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)中包含的翻译成分变换矩阵重新presenting,那么它是从列优先布局没有什么区别。

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.

如果您的着色器code的乘法看起来是这样的:

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_major matrix_column_major

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

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