【问题标题】:GLSL texture layoutGLSL 纹理布局
【发布时间】:2018-02-17 05:44:57
【问题描述】:

矩阵的布局(行优先 vs 列优先)如何影响它创建的 GLSL 纹理?着色器中对该纹理的访问是否会发生变化?如果是列优先矩阵,我应该先将矩阵更改为行优先,然后将其作为纹理上传到 GPU?

【问题讨论】:

  • @Rabbid76 我将此矩阵作为纹理上传,然后我将在着色器中访问它,我认为访问部分有点混乱。不知道贴图创建的时候内存访问会是什么样子
  • @user3178756:向我们展示一些代码,以便我们了解您在说什么。

标签: opengl glsl textures


【解决方案1】:

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) );

   .....      
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    相关资源
    最近更新 更多