见The OpenGL Shading Language 4.6, 5.4.2 Vector and Matrix Constructors, page 101:
要通过指定向量或标量来初始化矩阵,组件按列主要顺序分配给矩阵元素。
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) 存储在二维 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) );
.....
}