清除 OpenGL 着色器中的属性位置

Clearing the attribute locations in OpenGL Shader

有没有办法覆盖或清除 OpenGL 中的属性位置? 例如(我正在使用 lwjgl)我呈现这样的东西:

public void render(int vaoID, int vertexCount, int shaderProgramID){
    GL30.glBindVertexArray(vaoID);

    GL20.glBindAttribLocation(shaderProgramID, 0, "position");
    GL20.glBindAttribLocation(shaderProgramID, 1, "normal");

    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);

    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertexCount);

    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);

    GL30.glBindVertexArray(0);
}

然后我想 运行 具有相同 shaderProgramID

的下一个代码
public void render(int vaoID, int vertexCount, int shaderProgramID){
    GL30.glBindVertexArray(vaoID);

    //this previously was position
    GL20.glBindAttribLocation(shaderProgramID, 0, "normal");
    //and this was the normal
    GL20.glBindAttribLocation(shaderProgramID, 1, "position");

    GL20.glEnableVertexAttribArray(0);
    GL20.glEnableVertexAttribArray(1);

    GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertexCount);

    GL20.glDisableVertexAttribArray(0);
    GL20.glDisableVertexAttribArray(1);

    GL30.glBindVertexArray(0);
}

如您所见,我更改了以下代码:

GL20.glBindAttribLocation(shaderProgramID, 0, "position");
GL20.glBindAttribLocation(shaderProgramID, 1, "normal");

对此:

GL20.glBindAttribLocation(shaderProgramID, 0, "normal");
GL20.glBindAttribLocation(shaderProgramID, 1, "position");

但是当我运行这两个代码时,

GL20.glGetAttribLocation(programID, "position");

returns 0 而不是 1

有没有办法清除之前绑定的位置,以便我可以绑定新的位置?

您必须在绑定属性位置后重新link您的程序。这在 glBindAttribLocation (...) 的文档中概述如下:

Name

glBindAttribLocation — Associates a generic vertex attribute index with a named attribute variable

C Specification

void glBindAttribLocation( GLuint program, GLuint index, const GLchar *name);

描述

[...]

Attribute variable name-to-generic attribute index bindings for a program object can be explicitly assigned at any time by calling glBindAttribLocation. Attribute bindings do not go into effect until glLinkProgram is called. After a program object has been linked successfully, the index values for generic attributes remain fixed (and their values can be queried) until the next link command occurs.

Any attribute binding that occurs after the program object has been linked will not take effect until the next time the program object is linked.