如何绑定GLSL中的统一位置?

Joe*_*200 6 java opengl glsl lwjgl

我试图将统一变量绑定到某个位置,但我目前无法弄清楚如何:

要绑定属性(例如顶点/普通数据),我使用以下代码:

Override
protected void bindAttributes() {
    super.bindAttribute(1, "position");
    super.bindAttribute(2, "textureCoords");
    super.bindAttribute(3, "normal");
}
protected void bindAttribute(int attribute, String variableName){
    GL20.glBindAttribLocation(programID, attribute, variableName);
}
Run Code Online (Sandbox Code Playgroud)

我想对统一变量做同样的事情.

目前,我正在获取自动分配的位置,如下所示:

public int location_transformationMatrix;
public int location_lightPosition;
public int location_lightColour;

@Override
public void getAllUniformLocations(){
    location_transformationMatrix = super.getUniformLocation("transformationMatrix");
    location_lightPosition = super.getUniformLocation("lightPosition");
    location_lightColour = super.getUniformLocation("lightColour");
}
Run Code Online (Sandbox Code Playgroud)

然而,由于我随后绑定了属性,这最多会导致数据丢失,例如照明和法线,最糟糕的是,大约有60%的人试图播放它.

BDL*_*BDL 3

从 OpenGL 4.3 开始,可以使用布局限定符设置着色器制服:

layout(location = 2) uniform mat4 modelToWorldMatrix;
Run Code Online (Sandbox Code Playgroud)

例如,会将制服的位置 n 绑定modelToWorldMatrix到位置 2。有关更多详细信息,请查看此处

在 4.3 之前(或者更好的说法是没有 ARB_explicit_uniform_location 扩展),没有办法修复制服位置。