【问题标题】:Clearing the attribute locations in OpenGL Shader清除 OpenGL Shader 中的属性位置
【发布时间】:2015-08-29 11:02:28
【问题描述】:

有没有办法覆盖或清除 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");

返回 0 而不是 1

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

【问题讨论】:

    标签: java opengl shader lwjgl vao


    【解决方案1】:

    您必须在绑定属性位置后重新链接您的程序。 glBindAttribLocation (...) 的文档中对此进行了概述,如下所示:

    姓名

    glBindAttribLocation — 将通用顶点属性索引与命名属性变量相关联

    C 规范

    void glBindAttribLocation( GLuint 程序, 胶水指数, const GLchar *name);

    说明

    [...]

    可以通过调用glBindAttribLocation 随时显式分配程序对象的属性变量名称到通用属性索引绑定。 在调用glLinkProgram 之前,属性绑定不会生效。 成功链接程序对象后,通用属性的索引值保持固定(并且它们的值可以被查询)直到下一个链接命令发生。

    在程序对象被链接后发生的任何属性绑定,直到下次程序对象被链接时才会生效。

    【讨论】:

    • 感谢您的回答,一切正常。唯一的问题是每次运行 glLinkProgram 时我的统一变量都会被清除。有没有办法在重置属性位置时保留它们?
    • @user370001:规范未定义该行为。我会说不。您的制服的值可能保持不变,也可能会发生变化,无法控制重新链接后程序中的数据会发生什么。作为一般规则,您无论如何都不应该频繁更改属性绑定位置 - 您最好重构一些代码以避免不得不这样做。
    • 我想尽可能快,所以我会想办法做到这一点。不管怎样,谢谢你的回答。
    • 把你的制服放在一个由缓冲区对象支持的制服块中。然后它们与程序分开存在(并且可以由多个程序共享),您只需在重新链接程序时再次调用 glUniformBlockBinding。
    猜你喜欢
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多