【问题标题】:Vertex shader with array inputs具有数组输入的顶点着色器
【发布时间】:2012-11-24 03:55:07
【问题描述】:

给定一个看起来像这样的顶点着色器

#version 400 compatibility

const int max_valence = 6;

in int valence;
in vec3 patch_data[1 + 2 * max_valence];

...

将数据映射到正确的顶点属性的正确方法是什么?我正在尝试使用 VBO,但我不知道如何传递这么大的值。glVertexAttribPointer 最多需要一个大小为 4 的向量。将顶点属性放入着色器的正确方法是什么?

【问题讨论】:

    标签: opengl glsl shader vbo vertex-shader


    【解决方案1】:

    Arrays of vertex attributes 在 OpenGL 中是合法的。但是,数组的每个成员都占用一个 单独的 属性索引。它们是连续分配的,从您给patch_data 的任何属性索引开始。由于您使用的是 GLSL 4.00,因此您还应该使用 layout(location = #) 明确指定属性位置。

    如果你这样做:

    layout(location = 1) in vec3 patch_data[1 + 2 * max_valence];
    

    那么patch_data将覆盖从1到1 + (1 + 2 * max_valence)的半开范围内的所有属性索引。

    从 OpenGL 方面来看,每个数组条目都是一个单独的属性。因此,您需要为每个单独的数组索引单独调用glVertexAttribPointer

    因此,如果您在内存中的数组数据看起来像一个由 13 个 vec3 组成的数组,并且紧密排列,那么您需要这样做:

    for(int attrib = 0; attrib < 1 + (2 * max_valence); ++attrib)
    {
      glVertexAttribPointer(attrib + 1, //Attribute index starts at 1.
        3, //Each attribute is a vec3.
        GL_FLOAT, //Change as appropriate to the data you're passing.
        GL_FALSE, //Change as appropriate to the data you're passing.
        sizeof(float) * 3 * (1 + (2 * max_valence)), //Assuming that these array attributes aren't interleaved with anything else.
        reinterpret_cast<void*>(baseOffset + (attrib * 3 * sizeof(float))) //Where baseOffset is the start of the array data in the buffer object.
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-14
      • 2013-11-23
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      相关资源
      最近更新 更多