Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind uniform locations in GLSL?

I am attempting to bind the uniform variables to a certain location, but I currently can't work out how:

To bind attributes (so for example vertex/normal data), I use the following code:

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

I would like to do the same sort of thing for uniform variables.

Currently, I am getting the automatically-assigned locations like this:

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

However, since I am then binding the attributes, this is, at best, resulting in the loss of data such as lighting and normals, and at worst, is crashing the game for about 60% of people who try to play it.

like image 794
Joehot200 Avatar asked Oct 25 '15 11:10

Joehot200


1 Answers

Shader uniforms can, starting from OpenGL 4.3, be set by using layout qualifiers:

layout(location = 2) uniform mat4 modelToWorldMatrix;

will for example bind the locationn of the uniform modelToWorldMatrix to location 2. For more details have a look here.

Before 4.3 (or better to say without the ARB_explicit_uniform_location extension) there was no way to fix a uniforms location.

like image 55
BDL Avatar answered Oct 10 '22 09:10

BDL