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

6

我正在尝试将uniform变量绑定到特定位置,但目前还无法解决:

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

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个回答

3

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

layout(location = 2) uniform mat4 modelToWorldMatrix;

比如,将uniform变量modelToWorldMatrix的位置绑定到位置2。更多详情请查看这里

在4.3之前(或者更准确地说,在没有ARB_explicit_uniform_location扩展的情况下),没有办法固定一个uniform变量的位置。


如果我在着色器顶部使用#version 130,这仍然有效吗? - Joehot200
不行。正如我说的,需要OpenGL 4.3或更高版本。这意味着#version 430。或者你有可用的特定扩展。 - BDL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接