2

我正在开发一个 OpenGL 应用程序,并试图使 ShaderPrograms 和 VAO 相互独立。说独立我的意思是 ShaderProgram 和 VAO 都可以有不同的属性集。

在创建新的 ShaderProgram 时,我将已知属性列表显式绑定到预定义位置,即使着色器不使用它们。

  BindAttributeLocation(program, StandardAttribute.Position, PositionAttributeName);
  BindAttributeLocation(program, StandardAttribute.Color, ColorAttributeName);
  BindAttributeLocation(program, StandardAttribute.Normal, NormalAttributeName);
  BindAttributeLocation(program, StandardAttribute.TexCoord, TexCoordAttributeName);
  BindAttributeLocation(program, StandardAttribute.Tangent, TangentAttributeName);

我正在尝试对 VAO 做同样的事情,但是如果网格不包含属性数据,我想设置默认值:

vao.Bind();
if(mesh.Color == null) {
    DisableVertexAttribArray(attribute);
    VertexAttrib4f(attribute, 1, 1, 1, 1);
}
else {
    EnableVertexAttribArray(attribute);
    buffer.Bind();
    BufferData(mesh.Color);
    VertexAttribPointer(attribute, ...);
}

这段代码似乎正在工作(到目前为止),但我不确定这是否是正确的方法。VertexAttrib4f 在哪里存储数据:在 VAO 中,还是上下文全局状态,我需要在每次绘制调用后重置它?

4

2 回答 2

4

当前属性值不是VAO 状态的一部分。规范中确认这一点的最清晰的声明是在glGetVertexAttrib()(强调添加)的描述中:

请注意,除了CURRENT_VERTEX_ATTRIB 之外的所有查询都返回存储在当前绑定的顶点数组对象中的值

CURRENT_VERTEX_ATTRIB您设置的值在哪里glVertexAttrib4f()

这意味着这些值是全局上下文状态。

于 2015-04-18T21:30:56.587 回答
3

Okay, I found good article about this: opengl.org

A vertex shader can read an attribute that is not currently enabled (via glEnableVertexAttribArray​). The value that it gets is defined by special context state, which is not part of the VAO.

Because the attribute is defined by context state, it is constant over the course of a single draw call. Each attribute index has a separate value. Warning: Every time you issue a drawing command with an array enabled, the corresponding context attribute values become undefined. So if you want to, for example, use the non-array attribute index 3 after previously using an array in index 3, you need to repeatedly reset it to a known value.

The initial value for these is a floating-point (0.0, 0.0, 0.0, 1.0)​.

于 2015-04-19T09:01:14.187 回答