如何正确设置自定义索引为通用顶点属性数据?

问题描述 投票:2回答:1

我试图在一次绘制调用中渲染多个文本标签。为此,我将每个标签的矩阵传递到一个统一的数组中。对于文本网格的每个顶点,我都传递了一个 "labelIndex "属性,以便从数组中选择正确的矩阵。

但是当我在RenderDoc中检查VS输入时,每个顶点的labelIndex都是0,好像它没有被初始化。在设置labelIndex顶点属性时,我缺少什么?

uint labelIdx = 0;
std::vector<uint> labelIndices;
for (auto& label : Labels)
{
  ... 
  for (unsigned int i = 0; i < wcslen(label.Text); i++)
  {
    ...
    labelIndices.push_back(labelIdx);
    labelIndices.push_back(labelIdx);
    labelIndices.push_back(labelIdx);
    labelIndices.push_back(labelIdx);
  }      
  labelIdx++;
}
glGenBuffers(1, &LabelIndexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, LabelIndexBufferID);
glBufferData(GL_ARRAY_BUFFER, labelIndices.size() * sizeof(uint), &labelIndices[0], GL_STATIC_DRAW);

glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

...

// 2nd attribute buffer : Label indices
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, LabelIndexBufferID);
glVertexAttribIPointer(1, 1, GL_UNSIGNED_INT, 0, (void*)0);

// element buffer
glGenBuffers(1, &ElementBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ElementBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint), &indices[0], GL_DYNAMIC_DRAW);

glBindVertexArray(0);

渲染。

glUseProgram(ShaderID);
...
glUniformMatrix4fv(ParentMatrixLocation, 1, GL_FALSE, glm::value_ptr(Renderer::VP));
glUniformMatrix4fv(LabelMatricesLocation, Labels.size(), GL_FALSE, glm::value_ptr(LabelMatrices.get()[0]));

glBindVertexArray(VertexArrayID);

glDrawElements(
    GL_TRIANGLES,
    CharacterCount * 6,
    GL_UNSIGNED_INT,
    (void*)0
);

glBindVertexArray(0);

顶点着色器

#version 430 core

layout(location = 0) in vec4 PositionUV;
layout(location = 1) in uint LabelIndex;

uniform mat4 ParentMatrix;
uniform mat4 LabelMatrices[200];
out vec2 UV;

void main()
{
    gl_Position = ParentMatrix * LabelMatrices[LabelIndex] * vec4(PositionUV.xy, 0, 1);
    UV = PositionUV.zw;
}

编辑。

我不知道这里发生了什么。当我得到缓冲区后填充它。

uint* data = NULL;
data = new uint[labelIndices.size()];
for (int i = 0; i < labelIndices.size(); i++)
{
    data[i] = 0;
}
glGetBufferSubData(GL_ARRAY_BUFFER, 0, labelIndices.size() * sizeof(TextVertex), data);

我看了看 data 当我将LabelIndex初始化为例如7,并且在循环中不递增时,LabelIndex在VS输入中会正确地具有7的值。这很奇怪。

c++ glsl opengl-4
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.