GLSL 纹理布局
GLSL texture layout
矩阵的布局(行优先与列优先)如何影响它创建的 GLSL 纹理?对该纹理的访问是否在着色器中发生变化?如果是列优先矩阵,我应该先将矩阵更改为行优先矩阵,然后将其作为纹理上传到 GPU 吗?
参见The OpenGL Shading Language 4.6, 5.4.2 Vector and Matrix Constructors, page 101:
To initialize a matrix by specifying vectors or scalars, the components are assigned to the matrix elements in column-major order.
mat4(float, float, float, float, // first column
float, float, float, float, // second column
float, float, float, float, // third column
float, float, float, float); // fourth column
这意味着如果您将矩阵 (mat4
) 存储在 2 维 4*N、RGBA 纹理的行中,如下所示:
0 1 2 3
mat4 m0 m0[0].xyzw m0[1].xyzw m0[2].xyzw m0[3].xyzw
mat4 m1 m1[0].xyzw m1[1].xyzw m1[2].xyzw m1[3].xyzw
mat4 m2 m2[0].xyzw m2[1].xyzw m2[2].xyzw m2[3].xyzw
mat4 m3 .....
mat4 matArray[N]; // possibly std::vector<glm::mat4>( N );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 4, N, 0, GL_RGBA, GL_FLOAT, matArray);
然后您可以像这样从着色器中的纹理读取矩阵:
uniform sampler2D matSampler;
void main()
{
mat4 m0 = mat4(
texelFetch( matSampler, ivec(0, 0), 0),
texelFetch( matSampler, ivec(1, 0), 0),
texelFetch( matSampler, ivec(2, 0), 0),
texelFetch( matSampler, ivec(3, 0), 0) );
mat4 m1 = mat4(
texelFetch( matSampler, ivec(0, 1), 0),
texelFetch( matSampler, ivec(1, 1), 0),
texelFetch( matSampler, ivec(2, 1), 0),
texelFetch( matSampler, ivec(3, 1), 0) );
.....
}
矩阵的布局(行优先与列优先)如何影响它创建的 GLSL 纹理?对该纹理的访问是否在着色器中发生变化?如果是列优先矩阵,我应该先将矩阵更改为行优先矩阵,然后将其作为纹理上传到 GPU 吗?
参见The OpenGL Shading Language 4.6, 5.4.2 Vector and Matrix Constructors, page 101:
To initialize a matrix by specifying vectors or scalars, the components are assigned to the matrix elements in column-major order.
mat4(float, float, float, float, // first column float, float, float, float, // second column float, float, float, float, // third column float, float, float, float); // fourth column
这意味着如果您将矩阵 (mat4
) 存储在 2 维 4*N、RGBA 纹理的行中,如下所示:
0 1 2 3
mat4 m0 m0[0].xyzw m0[1].xyzw m0[2].xyzw m0[3].xyzw
mat4 m1 m1[0].xyzw m1[1].xyzw m1[2].xyzw m1[3].xyzw
mat4 m2 m2[0].xyzw m2[1].xyzw m2[2].xyzw m2[3].xyzw
mat4 m3 .....
mat4 matArray[N]; // possibly std::vector<glm::mat4>( N );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 4, N, 0, GL_RGBA, GL_FLOAT, matArray);
然后您可以像这样从着色器中的纹理读取矩阵:
uniform sampler2D matSampler;
void main()
{
mat4 m0 = mat4(
texelFetch( matSampler, ivec(0, 0), 0),
texelFetch( matSampler, ivec(1, 0), 0),
texelFetch( matSampler, ivec(2, 0), 0),
texelFetch( matSampler, ivec(3, 0), 0) );
mat4 m1 = mat4(
texelFetch( matSampler, ivec(0, 1), 0),
texelFetch( matSampler, ivec(1, 1), 0),
texelFetch( matSampler, ivec(2, 1), 0),
texelFetch( matSampler, ivec(3, 1), 0) );
.....
}