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

时间:2015-10-25 11:09:32

标签: 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);
}

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

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

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");
}

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

1 个答案:

答案 0 :(得分:2)

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

layout(location = 2) uniform mat4 modelToWorldMatrix;
例如,

将统一modelToWorldMatrix的locationn绑定到位置2.有关详细信息,请查看here

4.3之前(或更好地说没有ARB_explicit_uniform_location扩展名),无法修复制服位置。