代码之家  ›  专栏  ›  技术社区  ›  user3178756

GLSL纹理布局

  •  1
  • user3178756  · 技术社区  · 7 年前

    矩阵的布局(行主视图与列主视图)如何影响其创建的GLSL纹理?对该纹理的访问是否在着色器中更改?如果是列主矩阵,我应该首先将矩阵更改为行主矩阵,然后将其作为纹理上载到GPU吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rabbid76    7 年前

    看见 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) );
    
       .....      
    }